PullToRefreshListView pull-down refresh and pull-up loading, pullrefreshlistview
[Statement] reprinted please indicate the source, this article from the fingertip flying blog: http://blog.csdn.net/huntersnail
-- Respect the author. The knowledge is priceless and there is no limit to communication!
Pull-down refresh is often used in Android projects. For inexperienced developers, the first contact is indeed a headache.
There are many frameworks available for reference on the Internet. I use a framework handmark downloaded from GitHub and click to open the link. This is also one of the most widely used frameworks I have seen on the Internet. Of course, many great gods will not care about it and write it out in minutes. I don't want to do anything like that. After all, I still need some skills to write on my own. I think there are many other things that are just like the author. They are just for communication. I hope they will not be enlightened. Next I will post the code to explain how to use it!
1. Import Database
Right-click Project --> click Properties --> click Android --> click Add to Add a library --> OK
2. Activity
Package com. example. refreshdemo; import java. util. arrayList; import java. util. list; import com. handmark. pulltorefresh. library. pullToRefreshBase; import com. handmark. pulltorefresh. library. pullToRefreshBase. mode; import com. handmark. pulltorefresh. library. pullToRefreshListView; import android. OS. asyncTask; import android. OS. bundle; import android. app. activity; import android. graphics. color; import android. view. gravity; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView; import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener2; public class MainActivity extends Activity {private PullToRefreshListView mPtrlv; private List <String> list; private MyAdapter mAdapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mPtrlv = (PullToRefreshListView) findViewById (R. id. ptrlv); // set mode, BOTH: Pull up/down/DISABLED: Cancel pull up/PULL_FROM_END: Pull up/PULL_FROM_START: Pull down only mPtrlv. setMode (Mode. BOTH); list = new ArrayList <String> (); initData (); setListener (); mAdapter = new MyAdapter (); mPtrlv. setAdapter (mAdapter);}/*** initialize analog data */private void initData () {for (int I = 0; I <10; I ++) {list. add ("analog data" + I) ;}} private void setListener () {mPtrlv. setOnRefreshListener (new OnRefreshListener2 <ListView> () {/*** pull-down refresh * if you need to pull down the previous page, process onPullDownToRefresh () in this method */int x = 1; @ Overridepublic void onPullDownToRefresh (PullToRefreshBase <ListView> refreshView) {list. add (0, "pull-down refresh <Previous Page:" + x ++); // notify mAdapter of data change. notifyDataSetChanged (); new mytask(cmd.exe cute ();}/*** pull up and load * if you need to pull down and load the previous page, process onPullUpToRefresh () in this method */int y = 1; @ Overridepublic void onPullUpToRefresh (PullToRefreshBase <ListView> refreshView) {list. add ("pull up and load> next page:" + y ++); // notify mAdapter of data change. notifyDataSetChanged (); new mytask(cmd.exe cute ();}});} /*** custom adapter ** @ author Endoon * 09:23:19 */class MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {return list. size () ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent) {TextView mView = new TextView (getApplicationContext (); mView. setText (list. get (position); mView. setTextColor (Color. RED); mView. setGravity (Gravity. CENTER); return mView;}/*** asynchronously load data * @ author Endoon * 09:23:12 */class MyTask extends AsyncTask <Void, Void, void >{@ Overrideprotected Void doInBackground (Void... params) {return null ;}@ Overrideprotected void onPostExecute (Void result) {super. onPostExecute (result); // Mark the current Refresh as complete. will Reset the UI and hide the Refreshing ViewmPtrlv. onRefreshComplete (); // refresh the UI }};}
3. Xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/ptrlv" android:layout_width="fill_parent" android:layout_height="fill_parent" > </com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>
4,
Source code and Library: Click to open the link
O (∩) O Haha ~ Welcome to all kinds of sputation, contempt, advice, and communication ......
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆Repost, please specify the source☞Blog with flying tips ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.