Detailed explanation of Android ListView pull-down refresh and pull-up loading implementation (including source code Demo)

I haven't written a blog for a while, I spent a week busy with course design, and I wasted a lot of time doing other things..

Ashamed ashamed.. I have sorted out the use of listview's pull-down refresh and pull-up loading in the past two days. After doing it myself, I feel that I have benefited a lot, and I have gradually accumulated some knowledge points that I did not master before.

 

This article is divided into two parts:

1: Use PullToRefresh to implement pull-down refresh and pull-up loading of listview

 

2: Customize listview to achieve pull-down refresh and pull-up records

     

One: PullToRefresh implementation

       The class library PullToRefresh written by chrisbanes to implement pull-down refresh and pull-up loading is used. The currently supported control types include listview, gridview, scrollview, viewpager, webview and other controls that we often come into contact with.

Download address: https://github.com/chrisbanes/Android-PullToRefresh

 

Pay attention to the import process here: after downloading, import the library package in the Android-PullToRefresh-master class library package and your project into the same workspace, and then import the library package.

The specific use steps are relatively simple, refer to the following code:

 

public class MainActivity extends ListActivity {
 
 
    private LinkedList<String> mItemList;
    private ArrayAdapter<String> adapter;
    private Context context;
    private PullToRefreshListView mPullToRefreshListView;
    
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        initData ();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemList);
        //Initialize the control
        mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list);
        ListView mListView = mPullToRefreshListView.getRefreshableView ();
        mListView.setAdapter(adapter);
       
        
        //Set the pull-to-refresh mode to Mode.Both
        mPullToRefreshListView.setMode(Mode.BOTH);
         
	        //Set the pull-up and pull-down events
	        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
	 
	            @Override
	            public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
	                if (refreshView.isHeaderShown()){
	                    Toast.makeText(context, "Pull to refresh",Toast.LENGTH_SHORT).show();
	                    //Pull down to refresh the business code
	                    
	                }else {
	                    Toast.makeText(context, "Pull up to load more",Toast.LENGTH_SHORT).show();
	                    //Pull up to load more business code
	                }
	                refreshView.postDelayed(new Runnable() {
	                    @Override
	                    public void run() {
	                    	refreshView.onRefreshComplete();
	                    }
	                }, 1000);
	                Toast.makeText(context, "刷新成功",Toast.LENGTH_SHORT).show();
	            }
	        });
    }

private void initData(){
        //Initialization data
        mItemList = new LinkedList<String>();
        mItemList.addAll(Arrays.asList(data));
         
    }
     
    private String[] data  = new String[]{"data1","data2","data3","data4","data5","data6",
            "data1","data2","data3","data4","data5","data6"};
}

 

 

The program that runs after the steps are completed will not stop during pull-up loading and pull-down refresh, because the specific implementation business code has not been written in the onRefresh class:

 

      @Override
            public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
                if (refreshView.isHeaderShown()){
                    Toast.makeText(context, "Pull to refresh",Toast.LENGTH_SHORT).show();
                    //Pull down to refresh the business code
                    
                }else {
                    Toast.makeText(context, "Pull up to load more",Toast.LENGTH_SHORT).show();
                    //Pull up to load more business code
                }

 Here, in order to facilitate the test, a test code is added to it, and the timed stop operation is implemented in the postDelayed() method of the view. The entire event method is as follows:

	        //Set the pull-up and pull-down events
	        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
	 
	            @Override
	            public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
	                if (refreshView.isHeaderShown()){
	                    Toast.makeText(context, "Pull to refresh",Toast.LENGTH_SHORT).show();
	                    //Pull down to refresh the business code
	                    
	                }else {
	                    Toast.makeText(context, "Pull up to load more",Toast.LENGTH_SHORT).show();
	                    //Pull up to load more business code
	                }
	                refreshView.postDelayed(new Runnable() {
	                    @Override
	                    public void run() {
	                    	refreshView.onRefreshComplete();
	                    }
	                }, 1000);
	                Toast.makeText(context, "刷新成功",Toast.LENGTH_SHORT).show();
	            }
	        });

The above is the process of using PullToRefresh to achieve pull-up loading and pull-down refresh. Of course, when we use it, we can't just "use it", just like I recently read an impressive saying: Although the wheels made by others are good, if you don't follow them, you will never be able to build them.

 

 

Two: custom listview implementation

      The specific implementation ideas are:

1. Customize the listview, customize the headerview, add it to the customized listview, and set the headerview to be hidden and invisible by default.

2. In the custom listview, by introducing the OnScrollListener interface, in the overridden method, determine the page number scrolled by the current listview and record it.

3. Customize an onMove method to judge the operation in the moving process and obtain the state of the current user operation state. Rewrite the onTouchEvent() method to judge the four actions of the user: MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP and make corresponding operations (if it is on the first page of the listview and the listview is pulled down and the value of state is "Release drop down" When "operation", the refresh method of mainactivity is called to refresh, so as to realize the function.

 

It should be noted here that the refresh method of calling the activity from the custom mainactivity is to introduce the interface defined in the custom listview through the activity. When the mainactivity is initialized, it is realized by passing its own instance this into the custom listview.

Personally, I think there is another way to achieve this. Create a public static instance object in the mainactivity, and then introduce this object in the listview to call the refresh method of the mainactivity. I don't know which of these two methods is easy to use. God informs~! !

 The following is the source code of the custom listview to achieve pull-down refresh, pull-up loading can be achieved in the same way.

 

 

Guess you like

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