Android pull-down refresh ListView -- RTPullListView

Source: Internet
Author: User


You can customize the drop-down display. to modify the top drop-down display, you only need to modify pulllist_head.xml. Modify list_footview.xml to obtain more information at the bottom.
Next we will use it in Activity:
[Java]
/**
* PullListView
* @ Author Ryan
*
*/
Public class RTPullListViewActivity extends Activity {
Private static final int INTERNET_FAILURE =-1;
Private static final int LOAD_SUCCESS = 1;
Private static final int LOAD_MORE_SUCCESS = 3;
Private static final int NO_MORE_INFO = 4;
Private static final int LOAD_NEW_INFO = 5;

Private RTPullListView pullListView;
Private ProgressBar moreProgressBar;

Private List <String> dataList;
Private ArrayAdapter <String> adapter;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
PullListView = (RTPullListView) this. findViewById (R. id. pullListView );

DataList = new ArrayList <String> ();
For (int I = 0; I <5; I ++ ){
DataList. add ("Item data" + I );
}
Adapter = new ArrayAdapter <String> (this, android. R. layout. simple_list_item_1, dataList );
PullListView. setAdapter (adapter );

// Add more buttons at the bottom of the listview (customizable)
LayoutInflater inflater = LayoutInflater. from (this );
View view = inflater. inflate (R. layout. list_footview, null );
RelativeLayout footerView = (RelativeLayout) view. findViewById (R. id. list_footview );
MoreProgressBar = (ProgressBar) view. findViewById (R. id. footer_progress );
PullListView. addFooterView (footerView );

// Refresh the listener from the drop-down list
PullListView. setonRefreshListener (new OnRefreshListener (){

@ Override
Public void onRefresh (){
New Thread (new Runnable (){

@ Override
Public void run (){
Try {
// Simulate data refresh
Thread. sleep (2000 );
DataList. clear ();
For (int I = 0; I <5; I ++ ){
DataList. add ("Item data" + I );
}
MyHandler. sendEmptyMessage (LOAD_NEW_INFO );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}). Start ();
}
});

// Get more listeners
FooterView. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
 
MoreProgressBar. setVisibility (View. VISIBLE );

New Thread (new Runnable (){

@ Override
Public void run (){
Try {
// Simulate data refresh
Thread. sleep (2000 );
For (int I = 0; I <5; I ++ ){
DataList. add ("New item data" + I );
}
MyHandler. sendEmptyMessage (LOAD_MORE_SUCCESS );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}). Start ();
}
});
}

// Result Processing
Private Handler myHandler = new Handler (){
 
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Switch (msg. what ){
Case LOAD_MORE_SUCCESS:
MoreProgressBar. setVisibility (View. GONE );
Adapter. notifyDataSetChanged ();
PullListView. setSelectionfoot ();
Break;
 
Case LOAD_NEW_INFO:
Adapter. notifyDataSetChanged ();
PullListView. onRefreshComplete ();
Break;
Default:
Break;
}
}

};
}

Related Article

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.