[Android] custom ListView: Pull up to load more

Source: Internet
Author: User

[Android] custom ListView: Pull up to load more

Pull up and refresh, that is, when the ListView is rolled to the bottom and the pull continues, a prompt is displayed indicating that the data is being loaded. The prompt will disappear and the new data will appear later.

Here, I provide an idea: the ListView built-in method has the method of adding the tail layout. In this way, when we listen to the pull to the end, the tail layout appears and new data is loaded, after loading, the data in the ListView will be updated, and the data will automatically hide the tail layout. With this repetition, more functions can be loaded by pulling up.

Now, let's start thinking about how to implement it step by step:

A) create a custom ListView

B) add a bottom layout for the ListView.

C) Add a bottom listener for ListView

D) implement the user operations to be performed when the ListView slides to the bottom

 

A) create a MyPullUpListView class, inherit the ListView, and implement its constructor again:

 

public MyPullUpListView(Context context) {super(context);}public MyPullUpListView(Context context, AttributeSet attrs) {super(context, attrs);}

 

B) first create a tail layout in its layout, for example:

 

 
     
  
 


 

The code for adding the layout at the bottom is:

 

/*** Initialize the Bottom page */public void initBottomView () {if (footerView = null) {footerView = LayoutInflater. from (this. context ). inflate (R. layout. listview_loadbar, null);} addFooterView (footerView );}

 

C) Add a slide listener to the ListView to implement the OnScrollListener interface and implement the following methods:

 

Public void onScrollStateChanged (AbsListView view, int scrollState) {// when sliding to the bottom, if (scrollState = OnScrollListener. SCROLL_STATE_IDLE & firstVisibleItem! = 0) {}} public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {this. firstVisibleItem = firstVisibleItem; if (footerView! = Null) {// determines whether a visual Item can be fully displayed on the current page if (visibleItemCount = totalItemCount) {// removeFooterView (footerView); footerView. setVisibility (View. GONE); // hide the bottom layout} else {// addFooterView (footerView); footerView. setVisibility (View. VISIBLE); // display the bottom layout }}}


 

D) To implement the user operation to be performed when the ListView slides to the bottom, a callback interface is required:

 

/*** Pull and refresh the callback listener of the ListView ** @ author xiejinxiong **/public interface MyPullUpListViewCallBack {void scrollBottomState ();}

Where the callback interface is used:

 

 

Public void onScrollStateChanged (AbsListView view, int scrollState) {// when sliding to the bottom, if (scrollState = OnScrollListener. SCROLL_STATE_IDLE & firstVisibleItem! = 0) {myPullUpListViewCallBack. scrollBottomState ();}}

The general implementation code is as shown above. Because it still requires a bit of format, the following shows its format:

 

 

myListView = (MyPullUpListView) this.findViewById(R.id.mylist);myListView.initBottomView();myListView.setAdapter(listViewAdapter);myListView.setMyPullUpListViewCallBack(new MyPullUpListViewCallBack() {public void scrollBottomState() {// TODO Auto-generated method stub····}});

:

 

For the convenience of the learner's reference, the complete custom ListView code is attached below:

 

Package com. xiaoyan. xiaoyanlibrary. common. widget. listview; import android. content. context; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. listView; import com. xiaoyan. xiaoyanlibrary. r;/*** pull up and refresh ListView ** @ author xiejinxiong **/public class MyPullU PListView extends ListView implements OnScrollListener {/** the loading page is displayed at the bottom */private View footerView = null;/** storage Context */private context Context; /** pull and refresh the callback listener of the ListView */private MyPullUpListViewCallBack myPullUpListViewCallBack;/** record the value of the first Item */private int firstVisibleItem; public MyPullUpListView (Context context) {super (context); this. context = context; initListView ();} public MyPullUpListView (Context Context, AttributeSet attrs) {super (context, attrs); this. context = context; initListView ();}/*** initialize ListView */private void initListView () {// set the sliding listener setOnScrollListener (this) for ListView ); // remove the bottom split line setFooterDividersEnabled (false);}/*** initialize the Bottom page */public void initBottomView () {if (footerView = null) {footerView = LayoutInflater. from (this. context ). inflate (R. layout. listview_loadbar, null);} addFooterVie W (footerView);} public void onScrollStateChanged (AbsListView view, int scrollState) {// if (scrollState = OnScrollListener. SCROLL_STATE_IDLE & firstVisibleItem when sliding to the bottom! = 0) {response. scrollBottomState () ;}} public void onScroll (AbsListView view, int preview, int visibleItemCount, int totalItemCount) {this. firstVisibleItem = firstVisibleItem; if (footerView! = Null) {// determines whether a visual Item can be fully displayed on the current page if (visibleItemCount = totalItemCount) {// removeFooterView (footerView); footerView. setVisibility (View. GONE); // hide the bottom layout} else {// addFooterView (footerView); footerView. setVisibility (View. VISIBLE); // display the bottom layout }}} public void setMyPullUpListViewCallBack (MyPullUpListViewCallBack myPullUpListViewCallBack) {this. myPullUpListViewCallBack = myPullUpListViewCallBack;}/*** pull the callback listener of the refreshed ListView ** @ author xiejinxiong **/public interface MyPullUpListViewCallBack {void scrollBottomState ();}}


 

 

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.