Some time ago, MD design was adopted in the project, so RecyclerView must be used to completely replace ListView. But in the development encountered the need to realize RecyclerView pull up loading, pull down refresh and add Header and Footer and other requirements, the problem is solved in the five open source projects summed up for the convenience of others.Copy the code

First of all, RecyclerView, RecyclerView compared with ListView added a lot of new features:

• ViewHolder mode in Adapter – It is not necessary for the ListView to create a ViewHolder to improve performance. Because ListView does not have a strict ViewHolder design pattern. But when using RecyclerView, Adapter must implement at least one ViewHolder, must follow the ViewHolder design pattern. , custom Item entry – the ListView can only implement vertical linear array list view, in contrast, RecyclerView RecyclerView. Can be set via the LayoutManager to customize the view of different style, such as horizontal scrolling list list or irregular flow of the waterfall. • Item animation – There are no methods or interfaces in the ListView that allow developers to animate items. On the contrary, you can set RecyclerView RecyclerView. ItemAnimator to add animation to entry. • Set up data sources – Various types of Adapter are encapsulated in LisView for different data, such as ArrayAdapter for handling arrays and CursorAdapter for displaying Database results. In contrast, RecyclerView must be customized to implement recyclerView. Adapter and provide it with data collection. • Set the dividing line between items – In the ListView you can set the dividing line between two items by setting the Android: Divider property. If you want to add the effect to RecyclerView, you must use RecyclerView. ItemDecoration, this implementation is not only more flexible, and the style is more rich. , set the click event – exist in the ListView AdapterView. OnItemClickListener interface, used for binding entries click event. But, unfortunately, in RecyclerView does not provide such interfaces, however, provides another interface RcyclerView. OnItemTouchListener, used to response entry touch events. But… , RecyclerView does not have Header and Footer like ListView, so we need to develop our own Header and Foote, in addition to the development of small partners often use PullToRefresh library and temporarily does not support RecyclerView. Like many of the guys around me, I was caught in a difficult situation, so I decided to solve the yuan problems myself and be a hand maker so as not to slow down the project.

Here are some of the best Header, Footer, pull-up load, and pull-down refresh solutions I've found on GitHub:Copy the code

SwipeToLoadLayout- Recommended

GitHub address: github.com/Aspsine/Swi…

SwipeToLoadLayout supports YouTube, Google, JINGdong and many other apps based on RecyclerView pull up loading and pull down refresh style, easy to use do not do not. Without further ado, let's get straight to the picture above:Copy the code

• ListView & the GridView

• RecyclerView(With all kinds of layoutManagers)

• WebView & ScrollView & Other Views

• Google SwipeRefreshLayout style

• jingdong style

• Yalantis Phoenix style

AndroidStudio configuration method

Step 1: Add the JitPack repository in your build.gradle repository {maven {url “jitpack.io”}}

The second part: add dependent libraries dependencies {the compile ‘com. Making. Aspsine: SwipeToLoadLayout: v1.0.2}’

(2) Ultimate Clerview – famous

GitHub address: github.com/cymcsg/Ulti…

UltimateRecyclerView is a well-known open source framework for RecyclerView drop-down refresh, load more, add headers, show or hide toolbars, and many other problems.Copy the code

Features are as follows: • Swipe to refresh (using android. Support. The v4. Widget. SwipeRefreshLayout), Many kinds of animations, Swipe to dismiss Parallax or normal head view • Drag and drop items • Loading more when reach the last item(infinite scrolling) • Custom Views in loading more • Showing or hiding toolbar and floating button when scrolling • Scrollbars • Colorful styles of Swipe to refresh • Sticky header like Instagram • Support different layout in adapter • Loading adapter with animation

The use effect is as follows:

AndroidStudio configuration method

Step 1: Add libraries to your build.gradle

repositories {
    jcenter()
    maven { url "http://dl.bintray.com/jjhesk/maven" }
    }Copy the code

Step 2: Add the dependency libraries

Dependencies {the compile 'com. HKM. Slidingmenulib: libmenu: 0.4.9'}Copy the code

Step 3: Use methods in layout files

Copy the code

(3) IRecyclerView- the most dazzling effect

GitHub address: github.com/Aspsine/IRe…

IRecyclerView supports RecyclerView drop-down refresh, drop-down load, custom Header and Footer.Copy the code

Features are as follows: • pull-to-refresh • pull-to-loadmore • Customize refresh header • Customize loadmore footer • Add multiple header view • Add multiple Footer View

The use effect is as follows:

Refresh the effect

Step 1: Add libraries to your build.gradle

repositories:
allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}Copy the code

Step 2: Add the dependency libraries

Dependencies {the compile 'com. Making. Aspsine: IRecyclerView: hundreds'}Copy the code

Step 3: Use methods in layout files

Copy the code

Step 4: Use it in your Activity/Fragment

IRecyclerView iRecyclerView = (IRecyclerView) findViewById(R.id.iRecyclerView); iRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // an custom footer view, you can customize it yourself. LoadMoreFooterView loadMoreFooterView = (LoadMoreFooterView) iRecyclerView.getLoadMoreFooterView(); // you can also add header and footer like this // note: header and refresh header are different, footer and load more footer are different too. iRecyclerView.addHeaderView(headerView); iRecyclerView.addFooterView(footerView); // adapter ImageAdapter mAdapter = new ImageAdapter(); // note: here use setIAdapter(...) method not setAdapter(...) iRecyclerView.setIAdapter(mAdapter); iRecyclerView.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { } }); iRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(View loadMoreView) { } }); // set auto refreshing iRecyclerView.post(new Runnable() { @Override public void run() { iRecyclerView.setRefreshing(true); }}); // stop refreshing iRecyclerView.setRefreshing(false);Copy the code

(4) PullLoadMoreRecyclerView- most complete properties

GitHub address: github.com/WuXiaolong/…

PullLoadMoreRecyclerView implements RecyclerView pull-down refresh and pull-up loading more as well as RecyclerView linear, grid, waterfall flow effects.Copy the code

The renderings are as follows:

Use the method build.gradle file

Dependencies {the compile 'com. Wuxiaolong. Pullloadmorerecyclerview: library: 1.0.4'}Copy the code

XML reference

Copy the code

Set a linear layout

 mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView)
 mPullLoadMoreRecyclerView.setLinearLayout()Copy the code

Setting up the grid layout

mPullLoadMoreRecyclerView.setGridLayout(2); // The argument is the number of columnsCopy the code

Set up staggered grid layout, namely waterfall flow effect

mPullLoadMoreRecyclerView.setStaggeredGridLayout(2); // The argument is the number of columnsCopy the code

Binding adapter

mRecyclerViewAdapter = new RecyclerViewAdapter(); mPullLoadMoreRecyclerView.setAdapter(mRecyclerViewAdapter); public class RecyclerViewAdapter extends RecyclerView.Adapter { public RecyclerViewAdapter() { } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { } @Override public int getItemCount() { return 0; } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); }}}Copy the code

Call the drop-down refresh and load more

mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {               

            }
        });Copy the code

Refresh the end

mPullLoadMoreRecyclerView.setPullLoadMoreCompleted()Copy the code

No drop-down refresh required

mPullLoadMoreRecyclerView.setPullRefreshEnable(false);Copy the code

No need for a pull-up refresh

mPullLoadMoreRecyclerView.setPushRefreshEnable(false);Copy the code

Set pull-up refresh text

mPullLoadMoreRecyclerView.setFooterViewText("loading")Copy the code

Set the drop-down refresh color

 mPullLoadMoreRecyclerView.setColorSchemeResources(android.R.color.holo_red_dark,android.R.color.holo_blue_dark)Copy the code

Rapid Top

 mPullLoadMoreRecyclerView.scrollToTop()Copy the code

(5) HeaderAndFooterRecyclerView – encapsulation is perfect

GitHub address: github.com/cundong/Hea…

HeaderAndFooterRecyclerView support addHeaderView, addFooterView, paging load RecyclerView solution. It can expand RecyclerView control (through recyclerView. Adapter), to RecyclerView increase HeaderView, FooterView, and do not need to do any modification to your specific business logic Adapter. Also, by changing the FooterView State, you can dynamically assign different states to the FooterView (loading, loading failed, sliding to bottom, etc.). Can realize RecyclerView paging load data Loading/TheEnd NetWorkError effect.Copy the code

• add HeaderView, FooterView mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter (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));
Copy the code

• LinearLayout GridLayout/StaggeredGridLayout layout RecyclerView paging load mRecyclerView. AddOnScrollListener (mOnScrollListener); private EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {

@Override public void onLoadNextPage(View view) { super.onLoadNextPage(view); LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView); if(state == LoadingFooter.State.Loading) { Log.d("@Cundong", "the state is Loading, just wait.." ); return; } mCurrentCounter = mDataList.size(); if (mCurrentCounter < TOTAL_COUNTER) { // loading more RecyclerViewStateUtils.setFooterViewState(EndlessLinearLayoutActivity.this, mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null); requestData(); } else { //the end RecyclerViewStateUtils.setFooterViewState(EndlessLinearLayoutActivity.this, mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null); }}};Copy the code

Matters needing attention

If you already use RecyclerViewUtils. SetHeaderView (mRecyclerView, view); Added HeaderView for RecyclerView, The ViewHolder class calls to getAdapterPosition() and getLayoutPosition() will return values that are affected by the addition of the Header (the position returned is the real position+headerCounter).  As a result, In this case, please use RecyclerViewUtils. GetAdapterPosition (mRecyclerView, ViewHolder. This), RecyclerViewUtils. GetLayoutPosition (mRecyclerView, ViewHolder. This) to replace the two methods. Effect:Copy the code

• Add HeaderView and FooterView

• Support page loading LinearLayout layout RecyclerView

• Support page loading GridLayout RecyclerView

• Support page loading StaggeredGridLayout layout RecyclerView

• GridLayout RecyclerView when pages fail to load

The above is GitHub good RecyclerView open source framework, I hope to bring help to the development of small partners, but also thanks to these authors to provide such a good thing!

Android development advanced technical exchange QQ group: 108721298