android框架之PullToRefresh下拉刷新

蓝蓝的天 2019-06-20

Github项目地址点我

框架简介

android-PullToRefresh框架使用起来很简单,它支持下拉刷新也支持上拉加载。支持的控件包括:

  • ListView

  • ExpandableListView

  • GridView

  • WebView

  • ScrollView

  • HorizontalScrollView

  • ViewPager

代码实例:
先看效果图:
android框架之PullToRefresh下拉刷新
XML布局文件中:

<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/pull_to_refresh_listview"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" />

Java文件中声明并使用

public class MainActivity extends Activity {

    PullToRefreshListView pullToRefreshView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
        String[] arr = {"1","2","3","4","5"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, arr);
        pullToRefreshView.setAdapter(adapter);
        pullToRefreshView
                .setOnRefreshListener(new OnRefreshListener<ListView>() {
                    @Override
                    public void onRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        // Do work to refresh the list here.
                        new GetDataTask().execute();
                    }
                });

    }

    class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected void onPostExecute(String[] result) {
            pullToRefreshView.onRefreshComplete();//结束刷新
            super.onPostExecute(result);
        }

        @Override
        protected String[] doInBackground(Void... params) {
            try {
                Thread.sleep(2000);//控制刷新两秒
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }
}

相关推荐