PullToRefreshView pull-down refresh pull-up load, pullrefreshlistview

Source: Internet
Author: User

PullToRefreshView pull-down refresh pull-up load, pullrefreshlistview

I haven't written a blog for a long time. Today I used the pull-down refresh drop-down box to load and summarized the source code. I have understood the pull-down refresh and read it, today, I will not talk about the principle, but about how to use it. In fact, pullToRefreshView is very simple, as long as the preparation is complete.

1. Implementation of pull-up and refresh drop-down loading

1. The pulltoRefreshView open-source framework is basically used to implement pull-up and refresh pull-down loading. There are two implementation methods (1 ). download the jar package from the official website to add to the project gitHub URL https://github.com/chrisbanes/Android-PullToRefresh (2 ). extract the required classes from the source code and put the extracted source code in my own project. I personally prefer this method, because this method can be modified according to the project's needs, I will explain the (2) method.

2. Implementation steps:
Someone has drawn the source code and we can use it directly. If you want to figure out the implementation principles, you can refer to the source code. The source code annotations in the source code are very detailed implementation steps: (1) resources to be imported, the red circle is the class we want to add to the project, including the GridView, ListView, ScrollView, webView we can select as needed. In addition, we need to pay attention to the layout shown in 2 under the resource file and copy picture_dimens.xml and pull_refresh_string.xml under values. After completing the above work, I can modify the animation, text, and so on based on the actual needs of the project. The following describes an example:
We can change the loaded text in pull_refresh_strings.xml. We can change the layout loaded to the bottom in pull_to_load_footer.xml and change the animated images such as (1) In putt_load_header.xml.

3. Source Code

Package com. example. pulltorefreshpractice; import java. text. simpleDateFormat; import java. util. arrays; import java. util. date; import java. util. using list; import java. util. list; import com. example. ui. pullToRefreshBase; import com. example. ui. pullToRefreshBase. onRefreshListener; import com. example. ui. pullToRefreshListView; import android. app. activity; import android. OS. asyncTask; import android. OS. bundle; import android. text. format. dateUtils; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. simpleAdapter; public class MainActivity extends Activity {private PullToRefreshListView ptrlv_test; private parameter list <String> mListItem; private ArrayAdapter <String> mAdapter; private ListView mListView; private boolean isPullDownRefresh = true; // determine whether to pull down or the private int mCurrentIndex is 0; // the position of the current entry is private static final int oneLoadCount = 20; // The number of entries loaded each time. private SimpleDateFormat mDateFormat = new SimpleDateFormat ("MM-dd HH: mm"); // Date Format @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mListItem = new listlist <String> (); mListItem. addAll (Arrays. asList (mStrings ). subList (0, oneLoadCount); // load ptrlv_test = (PullToRefreshListView) findViewById (R. id. ptrlv_test); // set ptrlv_test.setPullLoadEnabled (false) for the current pull-load; // set the pull-down refresh to available values (true); // set ptrlv_test.setPullLoadEnabled (true) for the upload ); // whether to automatically load data when sliding to the bottom. This sentence must be added. Otherwise, ptrlv_test.setScrollLoadEnabled (true) cannot be displayed. // get the real listview, when we set the adapter for the listview or set the onItemClick event, we must use it, instead of ptrlv_te1_listview = ptrlv_test.getRefreshableView (); mCurrentIndex = oneLoadCount; mAdapter = new ArrayAdapter <String>. r. layout. simple_expandable_list_item_1, mListItem); mListView. setAdapter (mAdapter); // set the refresh listener (new OnRefreshListener <ListView> () {@ Overridepublic void onPullDownToRefresh (PullToRefreshBase <ListView> refreshView) {isPullDownRefresh =; new export cute () ;}@ Overridepublic void onPullUpToRefresh (PullToRefreshBase <ListView> refreshView) {isPullDownRefresh = false; new export cute () ;}}); setLastUpdateTime ();} private void setLastUpdateTime () {String text = formatDateTime (System. currentTimeMillis (); ptrlv_test.setLastUpdatedLabel (text);} private String formatDateTime (long time) {if (0 = time) {return "";} return mDateFormat. format (new Date (time);} // asynchronously load data private class getMoreDateTask extends AsyncTask <Void, Void, string []> {@ Overrideprotected String [] doInBackground (Void... params) {try {Thread. sleep (2000); // simulate loading data} catch (InterruptedException e) {e. printStackTrace ();} return mStrings;} @ Overrideprotected void onPostExecute (String [] result) {super. onPostExecute (result); boolean hasMoreData = true; // determines whether to pull up or drop down if (isPullDownRefresh) {mListItem. addFirst ("refresh the added data");} else {// Add onLoadCount entry int start = mCurrentIndex on the original base if the current position + oneLoadCount is loaded on the top; int end = start + oneLoadCount; if (end> = mStrings. length) {end = mStrings. length; hasMoreData = false;} for (int I = start; I <end; I ++) {mListItem. add (mStrings [I]);} mCurrentIndex = end;} mAdapter. notifyDataSetChanged (); reset (); ptrlv_test.onPullUpRefreshComplete (); // transmits the current status of data to ptrlv_test.setHasMoreData (hasMoreData );}} public static final String [] mStrings = {"1", "2", "3", "4", "5", "6", "7 ", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17 ", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27 ", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37 ", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47 ", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57 ", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67 ", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77 ", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87 ", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97 ", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107 ", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117 ", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127 ", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137 ", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147 ", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157 ", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167 ", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177 ", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187 ", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197 ", "198", "199", "200", "201", "202", "203", "204", "205", "206", "207 ", "208", "209", "210", "211", "212", "213", "214", "215", "216", "217 ", "218", "219", "220", "221", "222", "223", "224", "225", "226", "227 ", "228", "229", "230", "231", "232", "233", "234", "235", "236", "237 ", "238", "239", "240", "241", "242", "243", "244", "245", "246", "247 ", "248", "249", "250", "251", "252", "253", "254", "255", "256", "257 ", "258", "259", "260", "261", "262", "263", "264", "265", "266", "267 ", "268", "269", "270", "271", "272", "273", "274", "275", "276", "277 ", "278", "279", "280", "281", "282", "283", "284", "285", "286", "287 ", "288", "289", "290", "291", "292", "293", "294", "295", "296", "297 ", "298", "299", "300 "};}

You can download the source code and save the class and resource files I mentioned above that need to be imported into the project. You can use them next time.

To download the source code, click


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.