Android development Operation case Sharing!

Source: Internet
Author: User

Android development Operation case Sharing!

Android Lrecyclerview operation case Sharing-implementation of the drop-down refresh, swipe to the bottom of the automatic loading, have been trying to write down this open source project www.itxdl.cn, but a variety of reasons did not take time, today or in the work gap write down this blog, share with you.

Brief introduction

Lrecyclerview is a recyclerview that supports Addheaderview, Addfooterview, pull-down refreshes, and page-loaded data.

It expands on the Recyclerview control, adding Headerview, Footerview to Recyclerview, and does not require any modifications to your adapter.

Key Features

Drop-down refresh, swipe to the bottom to automatically load the next page of data;

can easily add header and footer;

The head drop-down style can be customized;

With item click and Long press event.

Network Error loading failed click Footer to re-request data;

Footerview can be dynamically assigned to different states (load, load failed, slide to the bottom, etc.).

Thank

If I look farther than others, it is because I stand on the shoulders of giants. (Newton)

This open source control is based on the Headerandfooterrecyclerview Open source project and has been expanded on the basis of the original. Thanks to Brother Lian Education (www.itxdl.cn) for sharing

Gradle

Step 1. Add Jitpack warehouse dependencies to your root Build.gradle file.

allprojects {

repositories {

Jcenter ()

Maven {URL "Https://jitpack.io"}

}

}

Step 2. Add Lrecyclerview dependencies to your model's Build.gradle file.

Compile ' com.github.jdsjlzx:lrecyclerview:1.0.0 '

Use

Add Headerview, Footerview

Mdataadapter = new DataAdapter (this);

Mdataadapter.setdata (dataList);

Mheaderandfooterrecyclerviewadapter = new Headerandfooterrecyclerviewadapter (this, mdataadapter);

Mrecyclerview.setadapter (Mheaderandfooterrecyclerviewadapter);

Mrecyclerview.setlayoutmanager (New Linearlayoutmanager (this));

Add a Headerview

Recyclerviewutils.setheaderview (Mrecyclerview, New Sampleheader (this));

Add a Footerview

Recyclerviewutils.setfooterview (Mrecyclerview, New Samplefooter (this));

Attention:

Mheaderandfooterrecyclerviewadapter = new Headerandfooterrecyclerviewadapter (this, mdataadapter);

Headerandfooterrecyclerviewadapter provides a number of practical functions, the user does not care about its implementation, just the time to construct their own mdataadapter in the form of parameters to pass in.

Drop-down to refresh and load more

For the convenience of everyone, the method that needs to be used is uniformly encapsulated into the interface Lscrolllistener.

Mrecyclerview.setlscrolllistener (New Lrecyclerview.lscrolllistener () {

@Override

public void Onrefresh () {

}

@Override

public void Onscrollup () {

}

@Override

public void Onscrolldown () {

}

@Override

public void Onbottom () {

}

@Override

public void onscrolled (int distancex, int distancey) {

}

});

The Lscrolllistener implements the Nrefresh (), Onscrollup (), Onscrolldown (), Onbottom (), and onscrolled five events as follows:

void Onrefresh ();//pull down to refresh

void Onscrollup (//scroll);

void Onscrolldown (//scroll);

void Onbottom ();//load next page

void onscrolled (int distancex, int distancey);//moving State,you can get the move distance

Onrefresh ()--recyclerview drop-down refresh event;

Onscrollup ()--recyclerview swipe up to listen for events;

Onscrolldown ()--recyclerview swipe down to listen for events;

Onbottom ()--recyclerview The listening events in the bottom part of the slide;

Onscrolldown ()--recyclerview is rolling the listener event;

Load more (load next page data)

As you can see from the Lscrolllistener introduction above, the implementation is more loaded as long as it is handled in the Onbottom () interface.

Drop-down Refresh

To achieve the drop-down refresh effect with the ListView, this project does not use the Swiperefreshlayout control, but instead refreshes the refresh effect in the custom Recyclerview header.

The drop-down refresh effect here draws on the Open Source Library: Avloadingindicatorview

Mrecyclerview.setrefreshprogressstyle (Progressstyle.ballspinfadeloader);

Mrecyclerview.setarrowimageview (R.drawable.iconfont_downgrey);

Avloadingindicatorview Library has how much effect, lrecyclerview support how many pull-down refresh effect, of course you can also customize the drop-down effect.

Drop-down refresh logic processing:

As can be seen from the Lscrolllistener introduction above, the implementation of pull-down refreshes as long as the Onrefresh () interface is processed.

Load Network exception handling

Lrecyclerview provides you with a mechanism to reload the data if the network is out of order or if it is disconnected.

The network exception error code is handled as follows:

Recyclerviewstateutils.setfooterviewstate (Getactivity (), Mrecyclerview, GetPageSize (), LoadingFooter.State.NetWorkError, Mfooterclick);

Private View.onclicklistener Mfooterclick = new View.onclicklistener () {

@Override

public void OnClick (View v) {

Recyclerviewstateutils.setfooterviewstate (Getactivity (), Mrecyclerview, GetPageSize (), LoadingFooter.State.Loading, NULL);

RequestData ();

}

};

The above mfooterclick is the logical handling event when we click on the footer at the bottom, and it is clear that we are still doing the re-request data operation here.

Click events and long-press event handling

In the blog of Hongyang seniors, there are the following descriptions:

Click and Longclick

However, a very depressing place is that the system does not provide Clicklistener and Longclicklistener.

But we can add it ourselves, but it will be more code.

The way to achieve more, you can go through the Mrecyclerview.addonitemtouchlistener to listen and then to judge gestures, of course you can also through the adapter to provide callbacks, here we choose the latter, the former way, we are interested in their own to achieve.

Hongyang the Great God chose the latter, Lrecyclerview early selected the former, through the practice summary, in the adapter to achieve a click event will be better.

First look at how to use:

Mheaderandfooterrecyclerviewadapter.setonitemclicklitener (New Onitemclicklitener () {

@Override

public void Onitemclick (view view, int position) {

}

@Override

public void Onitemlongclick (view view, int position) {

}

});

The principle is to achieve viewholder.itemview clicks and long press events. It is not posted because of too much code.

Viewholder.itemview is a recyclerview.adapter in itself, without additional definition.

The source code is as follows:

public static abstract class Viewholder {

Public final View Itemview;

int mposition = no_position;

int moldposition = no_position;

Long mitemid = no_id;

int mitemviewtype = Invalid_type;

int mprelayoutposition = no_position;

Set blank view (Setemptyview)

Mrecyclerview.setemptyview (view);

Note the layout file:

<relativelayout

Xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

>

<com.cundong.recyclerview.lrecyclerview

Android:id= "@+id/list"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"/>

<include

Android:id= "@+id/empty_view"

layout= "@layout/layout_empty"

android:visibility= "Gone"/>

</RelativeLayout>

Share

After the introduction of Lrecyclerview, there seems to be less what, by the way, that is adapter.

To make it easy for everyone to use, share a package of adapter.

public class Listbaseadapter<t extends entity> extends Recyclerview.adapter {

protected Context Mcontext;

protected int mscreenwidth;

public void setscreenwidth (int width) {

Mscreenwidth = width;

}

protected arraylist<t> mdatalist = new arraylist<> ();

@Override

Public Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {

return null;

}

@Override

public void Onbindviewholder (recyclerview.viewholder holder, int position) {

}

@Override

public int GetItemCount () {

return Mdatalist.size ();

}

Public list<t> getdatalist () {

return mdatalist;

}

public void Setdatalist (collection<t> list) {

This.mDataList.clear ();

This.mDataList.addAll (list);

Notifydatasetchanged ();

}

public void AddAll (collection<t> list) {

int lastIndex = This.mDataList.size ();

if (This.mDataList.addAll (list)) {

Notifyitemrangeinserted (LastIndex, List.size ());

}

}

public void Clear () {

Mdatalist.clear ();

Notifydatasetchanged ();

}

}

The listbaseadapter uses generics, which is simple and convenient, eliminating forced-type conversions.

Use the following:

Private class DataAdapter extends listbaseadapter<itemmodel>{

Private Layoutinflater Mlayoutinflater;

Public DataAdapter (Context context) {

Mlayoutinflater = Layoutinflater.from (context);

Mcontext = context;

}

@Override

Public Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {

return new Viewholder (Mlayoutinflater.inflate (R.layout.sample_item_text, parent, false));

}

@Override

public void Onbindviewholder (recyclerview.viewholder holder, int position) {

Itemmodel item = mdatalist.get (position);

Viewholder Viewholder = (viewholder) holder;

ViewHolder.textView.setText (Item.title);

}

Private class Viewholder extends Recyclerview.viewholder {

Private TextView TextView;

Public Viewholder (View Itemview) {

Super (Itemview);

TextView = (TextView) Itemview.findviewbyid (R.id.info_text);

}

}

}

Listbaseadapter Although the function is not powerful, but the use is very convenient.

Conclusion

Lrecyclerview easy to use, no matter how many headers and footer you add, you do not have to worry about position problems, in addition to convenience or convenience. Let's use them!

Android development Operation case Sharing!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.