Android development operation case sharing!

Android development operation case sharing!
Android LRecyclerView operation case sharing - realize pull-down refresh, slide to the bottom to automatically load, I always wanted to take the time to write this open source project www.lampbrother.net, but for various reasons, I didn't have time, today I still take advantage of the work gap I wrote this blog to share with you all.

Introduction
LRecyclerView is a RecyclerView that supports addHeaderView, addFooterView, pull-down refresh, and paging data loading.
It extends the RecyclerView control, adds HeaderView and FooterView to RecyclerView, and does not require any modification to your Adapter.

Main functions
Pull down to refresh, slide to the bottom to automatically load the data on the next page;
you can easily add Header and Footer;
the header drop-down style can be customized;
with item click and long press events.
Network error Loading failed Click Footer to re-request data;
FooterView can be dynamically assigned different states (loading, loading failed, sliding to the bottom, etc.).

Thanks
If I see farther than others, it's 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 original basis. Thanks to Brother Brother Education (www.lampbrother.net) for sharing

Gradle
Step 1. Add the JitPack repository dependency to your root build.gradle file.

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
Step 2. 在你的model的build.gradle文件中增加LRecyclerView依赖。

compile 'com.github.jdsjlzx:LRecyclerView:1.0.0'
使用

添加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));
Note:
mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(this, mDataAdapter);
HeaderAndFooterRecyclerViewAdapter provides some useful functions , users don't need to care about its implementation, just pass in their own mDataAdapter in the form of parameters when constructing.

Pull down to refresh and load more
For everyone's convenience, the required methods are 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) {
            }
        });
LScrollListener实现了nRefresh()、onScrollUp()、onScrollDown()、onBottom()、onScrolled五个事件,如下所示:

void onRefresh();//pull down to refresh
void onScrollUp();//scroll down to up
void onScrollDown();//scroll from up to down
void onBottom();//load next page
void onScrolled(int distanceX, int distanceY);// moving state,you can get the move distance
onRefresh()——RecyclerView pull-down refresh event;
onScrollUp()——RecyclerView sliding up listener event;
onScrollDown()——RecyclerView sliding down listener event;
onBottom()——RecyclerView sliding to the bottom listener event;
onScrollDown( )——RecyclerView is scrolling listening events;
loading more (loading data on the next page)

As can be seen from the introduction of LScrollListener above, to achieve loading more, just process it in the onBottom() interface.


Pull-down refresh
In order to achieve the pull-down refresh effect with Listview, this project does not use the SwipeRefreshLayout control, but implements the refresh effect in the head of the custom RecyclerView.

The pull-down refresh effect here draws on the open source library: AVLoadingIndicatorView

mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
How many effects the AVLoadingIndicatorView library has, LRecyclerView supports as many pull-down refresh effects, of course, you can also customize Define the drop down effect.

Pull-down refresh logic processing:

As can be seen from the introduction of LScrollListener above, the pull-down refresh can only be processed in the onRefresh() interface.

Loading network exception handling If the network is abnormal or disconnected when

loading data, LRecyclerView provides you with a reloading mechanism.

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 processing event when we click the Footer at the bottom, obviously We are still doing re-request data operations here.

Click event and long press event

handling are described in the blog of senior Hongyang:

Click and LongClick

But a very depressing place is that the system does not provide ClickListener and LongClickListener.
However, we can also add it ourselves, but there will be more code.
There are many ways to implement it. You can monitor and judge gestures through mRecyclerView.addOnItemTouchListener. Of course, you can also provide callbacks yourself through the adapter. Here we choose the latter and the former, everyone is interested in implementing it yourself.

God Hongyang chose the latter, and LRecyclerView chose the former in the early days. After practice, it would be better to implement click events in the adapter.

Let's see how to use it first:
mHeaderAndFooterRecyclerViewAdapter.setOnItemClickLitener(new OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
The principle is to implement viewHolder .itemView's click and long press events. I won't post it because of too much code.

viewHolder.itemView is inherent in RecyclerView.Adapter, no additional definition is required.

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

introducing LRecyclerView, there seems to be something missing. By the way, that is the adapter.

For the convenience of everyone, share a packaged adapter.

public class ListBaseAdapter<T extends Entity> extends RecyclerView.Adapter {
    protected Context mContext;
    protected int mScreenWidth;





    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();
    }
}

ListBaseAdapter uses generics, which is simple and convenient, and eliminates forced type conversion.

Use as follows:

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);
            }
        }
    }
Although the ListBaseAdapter is not powerful, it is very convenient to use.

Conclusion
LRecyclerView is convenient and simple to use. No matter how many Header and Footer you add, you don't have to worry about the position, except for convenience or convenience. Let's use it!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326604816&siteId=291194637