知乎下拉刷新SwipeRefreshLayout下拉刷新用法类似知乎下拉刷新

首先我们需要导入support v4的包,然后选择SwipeRefreshLayout,放到布局中。

在layout布局中,以listview举例,以以下格式使用SwipeRefreshLayout。

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/SwipeRefresh" >
        <ListView
            android:dividerHeight="0.5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/newsListView" />

    </android.support.v4.widget.SwipeRefreshLayout>

在java文件中

    private List<Map<String, Object>> list = new ArrayList<>();

    private SwipeRefreshLayout swipeRefresh;

        newsListView = (ListView) view.findViewById(R.id.newsListView);
        swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.SwipeRefresh);

        swipeRefresh.setColorSchemeResources(R.color.colorAccent);//设置圆形旋转图标中的箭头的颜色
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {  //下滑listview时回调
                newsListView.setAdapter(null); //不显示listview
                list.clear(); //list集合清空,
                new newsThread().start(); //启动线程加载数据
            }
        });


    private class newsThread extends Thread {
        @Override
        public void run() {
            super.run();
            setNewsData("范冰冰");
            String path = getNewsDataUrl(params); //获取接口地址,不要照抄,这是自己写的方法
            data = GetInternetJson(path, "GET"); //获取json数据,不要照抄,这是自己写的方法
            resolutionJson(data); //解析json添加到list集合中
            Message msg = new Message();
            msg.what = 0x12;
            mHandler.sendMessage(msg);
        }
    }


    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x12) {
                swipeRefresh.setRefreshing(false); //关闭刷新
                MyAdapter adapter = new MyAdapter(getActivity(), list); //自定义baseAdapter,传入list集合
                newsListView.setAdapter(adapter);
            }
            return false;
        }
    });




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页