Android PullToRefresh pull-up and pull-down refresh

Source: Internet
Author: User

Android PullToRefresh pull-up and pull-down refresh

Github link:

Https://github.com/chrisbanes/Android-PullToRefresh

1. Set Listview
PullToRefreshListView defined in the layout File

Xml Code

  1. Android: id = "@ + id/list_view"
  2. Android: layout_width = "match_parent"
  3. Android: layout_height = "match_parent"/>
    2. Set the Mode
    1. PullToRefreshListView mListView = (PullToRefreshListView) findViewById (R. id. list_view );
    2. MListView. setMode (Mode. BOTH); The supported Mode is as follows:
      • Mode. BOTH: supports pulling up and down at the same time
      • Mode. PULL_FROM_START: only Pulling Down is supported.
      • Mode. PULL_FROM_END: only Pulling Up can be pulled Up.3. Listener
        If Mode is set to Mode. BOTH, you need to set the listener OnRefreshListener2 and implement the onPullDownToRefresh () and onPullUpToRefresh () methods.

        If Mode is set to Mode. PULL_FROM_START or Mode. PULL_FROM_END, you must set the listener OnRefreshListener and implement the onRefresh () method. Of course, you can also set it to OnRefreshListener2, but only the onPullDownToRefresh () method is called when Mode. PULL_FROM is called, and only the onPullUpToRefresh () method is called when Mode. PULL_FROM is called.

        4. The specific code is as follows:
        1. Public class MainActivity extends Activity {
        2. Private ArrayList MListItems;
        3. Private PullToRefreshListView mListView;
        4. Private SampleListAdapter mAdapter;
        5. @ Override
        6. Protected void onCreate (Bundle savedInstanceState ){
        7. Super. onCreate (savedInstanceState );
        8. SetContentView (R. layout. sample );
        9. // Set initial data
        10. MListItems = new ArrayList ();
        11. For (int I = 1; I <= 10; I ++ ){
        12. MListItems. add ("Item" + Integer. toString (I ));
        13. }
        14. // Set ListView
        15. MListView = (PullToRefreshListView) findViewById (R. id. list_view );
        16. MAdapter = new SampleListAdapter ();
        17. MListView. setAdapter (mAdapter );
        18. // Set PullToRefresh
        19. MListView. setMode (Mode. BOTH );
        20. MListView. setOnRefreshListener (new OnRefreshListener2 (){
        21. // Pull Down Pulling Down
        22. @ Override
        23. Public void onPullDownToRefresh (PullToRefreshBase RefreshView ){
        24. // Reset data in the drop-down list
        25. MListItems = new ArrayList ();
        26. For (int I = 1; I <= 10; I ++ ){
        27. MListItems. add ("Item" + Integer. toString (I ));
        28. }
        29. MAdapter. notifyDataSetChanged ();
        30. New finishrefresh(cmd.exe cute ();
        31. }
        32. // Pulling Up
        33. @ Override
        34. Public void onPullUpToRefresh (PullToRefreshBase RefreshView ){
        35. // Add options when pulling up
        36. Int count = mListItems. size ();
        37. MListItems. add ("Item" + Integer. toString (++ count ));
        38. MAdapter. notifyDataSetChanged ();
        39. New finishrefresh(cmd.exe cute ();
        40. }
        41. });
        42. }
        43. Private class SampleListAdapter extends BaseAdapter {
        44. @ Override
        45. Public int getCount (){
        46. Return mListItems. size ();
        47. }
        48. @ Override
        49. Public Object getItem (int index ){
        50. Return mListItems. get (index );
        51. }
        52. @ Override
        53. Public long getItemId (int index ){
        54. Return index;
        55. }
        56. @ Override
        57. Public View getView (int index, View view, ViewGroup arg2 ){
        58. If (view = null ){
        59. LayoutInflater inflater = (LayoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE );
        60. View = inflater. inflate (R. layout. list_item, null );
        61. }
        62. TextView textView = (TextView) view. findViewById (R. id. listItemText );
        63. TextView. setText (mListItems. get (index ));
        64. Return view;
        65. }
        66. }
        67. Private class FinishRefresh extends AsyncTask {
        68. @ Override
        69. Protected Void doInBackground (Void... params ){
        70. Return null;
        71. }
        72. @ Override
        73. Protected void onPostExecute (Void result ){
        74. MListView. onRefreshComplete ();
        75. }
        76. }
        77. }

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.