Android and android Official Website

Source: Internet
Author: User

Android and android Official Website
PullToRefresh

Supports ListView, ExpandableListView, GridView, and WebView.

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

Note: If you want to put it in android studio, you need to export it in eclipse as an android studio project.

Code

Layout:

<LinearLayout 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"              android:orientation="vertical"              tools:context=".MyActivity">    <com.handmark.pulltorefresh.library.PullToRefreshListView        android:id="@+id/lv_main"        android:layout_width="fill_parent"        android:layout_height="fill_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>

Configuration:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_my);        mList = new ArrayList<String>();        for (int i = 0; i < mStr.length; i++) {            mList.add(mStr[i]);        }        mListView = (PullToRefreshListView) findViewById(R.id.lv_main);//        mListView.setOnRefreshListener(new MyRefreshListener());        mListView.setOnRefreshListener(new MyRefreshListener2());        mMyAdapter = new MyAdapter();        mListView.setAdapter(mMyAdapter);        mListView.setMode(PullToRefreshBase.Mode.BOTH);        mListView.getLoadingLayoutProxy().setLastUpdatedLabel("setLastUpdatedLabel");        mListView.getLoadingLayoutProxy().setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));        mListView.getLoadingLayoutProxy().setPullLabel("setPullLabel");        mListView.getLoadingLayoutProxy().setRefreshingLabel("setRefreshingLabel");        mListView.getLoadingLayoutProxy().setReleaseLabel("setReleaseLabel");    }

PullToRefreshBase. Mode. BOTH indicates the pull-down and pull-up refresh modes.

class MyRefreshListener implements PullToRefreshBase.OnRefreshListener {        @Override        public void onRefresh(PullToRefreshBase refreshView) {        }    }    class MyRefreshListener2 implements PullToRefreshBase.OnRefreshListener2 {        @Override        public void onPullDownToRefresh(PullToRefreshBase refreshView) {            mList.clear();            for (int i = 0; i < mStr.length; i++) {                mList.add(mStr[i]);            }            new Thread(new MyRunnable()).start();        }        @Override        public void onPullUpToRefresh(PullToRefreshBase refreshView) {            for (int i = 0; i < mStr.length; i++) {                mList.add(mStr[i]);            }            new Thread(new MyRunnable()).start();        }    }

There are two listeners here. Let's briefly talk about PullToRefreshBase. OnRefreshListener2. Obviously, one is pull-up, and the other is pull-down refresh callback.

Public class MyActivity extends Activity {private PullToRefreshListView mListView; // pulltorefresh control private List <String> mList; // private String for storing data [] mStr = {"0", "1 ", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11 ", "12", "13", "14", "15", "16", "17", "18", "19"}; // data private MyAdapter mMyAdapter; // adapter // a latency, and then process private Handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); switch (msg. what) {case 1: Log. I ("handleMessage", "handleMessage"); mListView. onRefreshComplete (); mMyAdapter. yydatasetchanged (); break ;}}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_my); mList = new ArrayList <String> (); // initialize for (int I = 0; I <mStr. length; I ++) {mList. add (mStr [I]); // prepare data} mListView = (PullToRefreshListView) findViewById (R. id. lv_main); // mListView. setOnRefreshListener (new MyRefreshListener (); mListView. setOnRefreshListener (new MyRefreshListener2 (); // sets the listener mMyAdapter = new MyAdapter (); // initializes the adapter mListView. setAdapter (mMyAdapter); mListView. setMode (PullToRefreshBase. mode. BOTH); // The mode is pull up and pull down mListView. getLoadingLayoutProxy (). setLastUpdatedLabel ("setLastUpdatedLabel"); mListView. getLoadingLayoutProxy (). setLoadingDrawable (getResources (). getDrawable (R. drawable. ic_launcher); // The image mListView when loading. getLoadingLayoutProxy (). setPullLabel ("setPullLabel"); mListView. getLoadingLayoutProxy (). setRefreshingLabel ("setRefreshingLabel"); mListView. getLoadingLayoutProxy (). setReleaseLabel ("setReleaseLabel");} class MyRefreshListener implements PullToRefreshBase. onRefreshListener {@ Override public void onRefresh (PullToRefreshBase refreshView) {}} class MyRefreshListener2 implements PullToRefreshBase. onRefreshListener2 {@ Override public void onPullDownToRefresh (PullToRefreshBase refreshView) {mList. clear (); for (int I = 0; I <mStr. length; I ++) {// Add data mList. add (mStr [I]);} new Thread (new MyRunnable ()). start (); // execution latency} @ Override public void onPullUpToRefresh (PullToRefreshBase refreshView) {for (int I = 0; I <mStr. length; I ++) {// Add data mList. add (mStr [I]);} new Thread (new MyRunnable ()). start (); // execution latency} class MyAdapter extends BaseAdapter {@ Override public int getCount () {return mList. size () ;}@ Override public Object getItem (int I) {return mList. get (I) ;}@ Override public long getItemId (int I) {return I ;}@ Override public View getView (int I, View view, ViewGroup viewGroup) {if (view = null) {view = LayoutInflater. from (MyActivity. this ). inflate (R. layout. item, null); TextView TV = (TextView) view.findViewById(R.id.txt); TV. setText (mList. get (I);} else {TextView TV = (TextView) view.findViewById(R.id.txt); TV. setText (mList. get (I);} return view;} class MyRunnable implements Runnable {@ Override public void run () {try {Thread. sleep (2000); // latency 2 s} catch (InterruptedException e) {e. printStackTrace ();} Log. I ("MyRunnable", "MyRunnable"); mHandler. sendEmptyMessage (1 );}}}
I am the dividing line of tiantiao

 

 

Source code: https://github.com/pinguo-yuyidong/PullToRefresh

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.