Use SmartRefreshLayout to complete the complete example of RecyclerView pull-up refresh and pull-down refresh

Keywords: Android xml encoding github

First, let's take a look at the effect, first update 5 data items from top to bottom, then slide to the bottom and pull up to add 5 data items from the bottom to the top:

We use a third-party library, so we first add it in the gradle configuration file:

    implementation 'com.android.support:recyclerview-v7:25.3.1'
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'

Then write a general RecyclerView, its layout file and item layout are as follows:

<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="vertical"
            android:background="#fff" />

</com.scwang.smartrefresh.layout.SmartRefreshLayout>

item layout, just a textView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="hehe"/>

</RelativeLayout>

The following is our main file, a complete example, which is annotated in great detail. Copy and paste it into your own project to achieve the effect in the dynamic diagram above:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    private RefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new MyAdapter(getDatas());
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initView() {
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        /**
         * Set different head and bottom styles
         */
//        refreshLayout.setRefreshFooter(new ClassicsFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
//        refreshLayout.setRefreshHeader(new BezierRadarHeader(this));
//        refreshLayout.setRefreshHeader(new TwoLevelHeader(this));
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));

        refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));

        //Set Beijing color after style
        refreshLayout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);

        //Set the listener, including top pull-down refresh and bottom slide up refresh
        refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener(){

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mAdapter.refreshData(MoreDatas()); //Pull down to refresh and add data to the interface from top to bottom
                refreshLayout.finishRefresh(1000); //Remember to set this, or turn it all the time
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mAdapter.loadMore(MoreDatas());  //Slide up to refresh and add data to the interface from bottom to top
                refreshLayout.finishLoadMore(1000); //Remember to set this, or turn it all the time
            }
        });

        /**
         *  The one above is a multi listener. It can directly unload multiple listeners into one listener. Of course, it can also be written in the following form. It has the same effect
         */
//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                Mdadapter. Refreshdata (moredata()); / / drop down refresh, data is added to the interface from top to bottom
//                refreshLayout.finishRefresh(1000); / / remember to set this one, otherwise keep turning
//            }
//        });
//
//        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                Mdadapter. Loadmore (moredata()); / / slide up to refresh and add data to the interface from the bottom up
//                Refreshlayout.finishlodmore (1000); / / remember to set this one, otherwise keep turning
//            }
//        });
    }

    //Raw recyclerView data
    private ArrayList<String> getDatas() {
        ArrayList<String> data = new ArrayList<>();
        String temp = " item";
        for(int i = 0; i < 15; i++) {
            data.add(i + temp);
        }
        return data;
    }

    //Refresh the data
    private ArrayList<String> MoreDatas() {
        ArrayList<String> data = new ArrayList<>();
        String temp = "New addition data ";
        for(int i = 0; i < 6; i++) {
            data.add(temp + i);
        }
        return data;
    }

    public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        public ArrayList<String> datas = null;

        public MyAdapter(ArrayList<String> datas) {
            this.datas = datas;
        }

        //Create a new View called by LayoutManager
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
            ViewHolder vh = new ViewHolder(view);
            return vh;
        }

        //Bind data to interface
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.mTextView.setText(datas.get(position));
        }

        //Number of acquired data
        @Override
        public int getItemCount() {
            return datas.size();
        }

        //Pull up at the bottom to refresh, and the data will be displayed directly at the bottom
        public void loadMore(ArrayList<String> strings) {
            datas.addAll(strings);
            notifyDataSetChanged();
        }

        //Pull down at the bottom to refresh, data is added directly from top to bottom, and displayed at the top
        public void refreshData(ArrayList<String> strings) {
            datas.addAll(0, strings);
            notifyDataSetChanged();
//            notifyItemInserted(0); only one item of data can be added at a time
        }

        //Custom ViewHolder, holding all interface elements of each Item
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTextView;
            public ViewHolder(View view){
                super(view);
                mTextView = (TextView) view.findViewById(R.id.text);
            }
        }
    }
}

There are many other uses, please see the example on the official website: https://github.com/scwang90/SmartRefreshLayout

Of course. Learning this library is also very simple. Basically, learning to use the callback function in the library is enough. For example, if you need to pull down and refresh, listen to setOnRefreshListener

If you need to pull up and refresh, listen to setOnLoadMoreListener or use multi-functional listener

setOnMultiPurposeListener rewrites the onRefresh() or onLoadMore() methods to achieve the same effect as above:
//The first writing method: set the listener, including top pull-down refresh and bottom slide up refresh
        refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener(){

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mAdapter.refreshData(MoreDatas()); //Pull down to refresh and add data to the interface from top to bottom
                refreshLayout.finishRefresh(1000); //Remember to set this, or turn it all the time
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mAdapter.loadMore(MoreDatas());  //Slide up to refresh and add data to the interface from bottom to top
                refreshLayout.finishLoadMore(1000); //Remember to set this, or turn it all the time
            }
        });

        /**
         *  The second way of writing:
         *  The one above is a multi listener. It can directly unload multiple listeners into one listener. Of course, it can also be written in the following form. It has the same effect
         */
//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                Mdadapter. Refreshdata (moredata()); / / drop down refresh, data is added to the interface from top to bottom
//                refreshLayout.finishRefresh(1000); / / remember to set this one, otherwise keep turning
//            }
//        });
//
//        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                Mdadapter. Loadmore (moredata()); / / slide up to refresh and add data to the interface from the bottom up
//                Refreshlayout.finishlodmore (1000); / / remember to set this one, otherwise keep turning
//            }
//        });

For an example of this project, please refer to: https://github.com/buder-cp/base_component_learn/tree/master/smart_learn

Posted by paladaxar on Tue, 31 Dec 2019 01:14:42 -0800