Android ListView ExpandableListView GridView WebView ScrollView HorizontalScrollView and so on. Pull down and refresh !, Listview nested gridview

Source: Internet
Author: User

Android ListView ExpandableListView GridView WebView ScrollView HorizontalScrollView and so on. Pull down and refresh !, Listview nested gridview

The first time I wrote a blog, I was a little nervous. If you have any mistakes, please correct them!

As we all know, apps currently have a common function: pull-up loading and pull-down refreshing, which is actually paging.
However, we need to use a third-party JAR "Android-PullToRefresh-master" today ",
The following controls are supported:
ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
Come and start! (Or download the Demo and jar packages)
Jar package and Demo: You can download free credits

Step 1:Create a project: Export demoflush”, import the jarpackage (android-pulltorefresh-master.rar), decompress the package, obtain a folder, and import it to Eclipse as a project ).
Note: An error may be reported after the import. Solution: After the import to the project (Android-PullToRefresh-master) right-click "Properties" and select "Android". Then, you can see "Project Build Taget" and check your SDK! I believe you can do it !)

Step 2:Right-click the project (created in the Demo) and choose "Properties"> "Android"> "Libray"> "add ".
Fortunately, this Demo project is half done!

Step 3:In the Demo project, I wrote code in activity_main.xml (in fact, it is a custom control, but this control is from the jar package just now, so use the complete Class Name). Remember the namespace!

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# FFFFFF" android: orientation = "vertical"> <! -- Xmlns: ptr = "http://schemas.android.com/apk/res-auto" for the namespace that we want to use certain properties in PullToRefresh --> <com. handmark. pulltorefresh. library. pullToRefreshListView xmlns: ptr = "http://schemas.android.com/apk/res-auto" android: id = "@ + id/pull_refresh_list" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: dividerHeight = "4dp" android: fadingEdge = "none" android: fastScrollEnabled = "false" android: footerDividersEnabled = "false" android: headerDividersEnabled = "false" android: smoothScrollbar = "true" ptr: ptrMode = "both"/> </LinearLayout>

Step 4:Findviewbyid: Find myPull_refresh_list and implement the listener interface. Run the project first to make you feel comfortable. Are you satisfied? Continue to look down!

Note that an error may be reported when running the project. You can delete the/bin/res folder under "Android-PullToRefresh-master" and automatically regenerate the folder!

Package com. vip. flushdemo; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. context; import android. OS. asyncTask; import android. OS. bundle; import android. view. menu; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. toast; import com. handmark. pulltorefresh. library. pullToRefreshBase; import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener2; import com. handmark. pulltorefresh. library. pullToRefreshListView; public class MainActivity extends Activity implements attributes <ListView> {private PullToRefreshListView listView1; private List <String> lists = new ArrayList <String> (); // simulate private myaradapter raymyarrayadapter; // data Adapter @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView1 = (PullToRefreshListView) findViewById (R. id. myPull_refresh_list); // bind Adapter myArrayAdapter = new MyArrayAdapter (this, android. r. layout. simple_list_item_1, getData (); listView1.setAdapter (myArrayAdapter); listView1.setOnRefreshListener (this); // remember to bind the listening class} // query int index = 1 in a simulated database; private List <String> getData () {for (int I = 0; I <10; I ++) {lists. add ("+" + index + ""); index ++;} return lists ;} // Adapter private class MyArrayAdapter extends ArrayAdapter <String> {public MyArrayAdapter (Context context, int textViewResourceId, List <String> objects) {super (context, textViewResourceId, objects) ;}} // asynchronously simulate the request data private class GetDataTask extends AsyncTask <Integer, Integer, Integer >{@ Override protected void onProgressUpdate (Integer... values) {super. onProgressUpdate (values); myArrayAdapter. notifyDataSetChanged (); // refresh the Adapter listView1.onRefreshComplete (); // tell it that we have completed Toast in the background data request. makeText (MainActivity. this, "finished", Toast. LENGTH_SHORT ). show () ;}@ Override protected Integer doInBackground (Integer... params) {getData (); // continue refreshing data try {Thread. sleep (2000); // pause for better effect} catch (Exception e) {e. printStackTrace ();} publishProgress (0); // notification front-end thread return 0; }}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public void onPullDownToRefresh (PullToRefreshBase <ListView> refreshView) {Toast. makeText (this, "drop-down", Toast. LENGTH_SHORT ). show (); index = 0; // refresh the data lists. clear (); new getdatatask(cmd.exe cute () ;}@ Override public void onPullUpToRefresh (PullToRefreshBase <ListView> refreshView) {Toast. makeText (this, "Pull up", Toast. LENGTH_SHORT ). show (); new getdatatask(cmd.exe cute ();}}

Congratulations! You have learned how to write a jar package when you are free,
Trigger:
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
The implementation method is similar!

Finally, thank you for your reading. Your support is my greatest motivation!

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.