天天看点

Android 集成了RecyclerView 刷新加载,及侧滑菜单的 PullToRefresh

这两天针对平时项目中所使用的刷新加载,以及侧滑菜单做了一个整理。在 PullToRefresh 框架中添加了 RecyclerView, 以及 使用 RecyclerView 所实现的 侧滑菜单。

下面先看一下效果图:

Android 集成了RecyclerView 刷新加载,及侧滑菜单的 PullToRefresh

因为这里是基于 PullToRefresh 框架,所以我这里只是用一个 ListView 做代表( GridView , ScrollView , WebView 等等,用法基本都和 ListView 一样的), RecyclerView 和 SwipeRecyclerView 两个用法也有点类似,但是还是有点差异。

用法就从 ListView 开始介绍(PullToRefresh 框架的基本用法):

pull_listView = (PullToRefreshListView) findViewById(R.id.pull_listView);
adapter = new ListViewAdapter(list, this);
// 设置刷新加载模式: BOTH 下来刷新和上拉加载  , PULL_FROM_END 上拉加载 , PULL_FROM_START 下拉刷新 ,DISABLED ,禁止刷新和加载
pull_listView.setMode(PullToRefreshBase.Mode.BOTH); 
pull_listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 下拉刷新监听
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(MainActivity.SLEEP_TIME);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    list.clear();
                                    DataModel.initData(list, "ListView");
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(PullListViewActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                                    pull_listView.onRefreshComplete();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 上拉加载监听
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(MainActivity.SLEEP_TIME);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    DataModel.initData(list, "ListView");
                                    adapter.notifyDataSetChanged();
                                    pull_listView.onRefreshComplete();
                                    Toast.makeText(PullListViewActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });

        pull_listView.setAdapter(adapter);
           

这里的 Adapter 就是 平时常用的继承 BaseAdapter 写法。

带刷新加载的 RecyclerView 用法:

pull_recyclerView = (PullToRefreshRecyclerView) findViewById(R.id.pull_recyclerView);

        recyclerView = pull_recyclerView.getRefreshableView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        pull_recyclerView.setMode(PullToRefreshBase.Mode.BOTH);
        pull_recyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                // 下拉刷新监听
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(MainActivity.SLEEP_TIME);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    list.clear();
                                    DataModel.initData(list, "RecyclerView");
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(PullRecyclerViewActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                                    pull_recyclerView.onRefreshComplete();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                // 上拉加载监听
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(MainActivity.SLEEP_TIME);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    DataModel.initData(list, "RecyclerView");
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(PullRecyclerViewActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
                                    pull_recyclerView.onRefreshComplete();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });

        adapter = new RecylerViewAdapter(list);
        recyclerView.setAdapter(adapter);
           

这里就需要注意了 , 关于 RecyclerView 的刷新加载 和它本身的属性设置 是分开来控制的,也就是我们只是通过 PullToRefreshRecyclerView 去控制它的刷新加载,至于 setLayoutManager ,setAdapter 都是需要 通过 getRefreshableView() 去得到 RecyclerView 的引用,才能进一步操作。

最后就是带有侧滑菜单的 RecyclerView 了, 这里要特别感谢一下 严振杰大神 他写的这个是我目前遇到的相对较好的一个侧滑菜单。根据公司项目需求,可能只是需要一个侧滑功能,所以这里我就从他的源码里单独的提出来 右侧侧滑菜单 功能,下面来看一下用法:

首先,也是正常的 实例化,设置刷新加载模式,设置加载监听.

pull_swipe_recyclerView = (PullToRefreshSwipeRecyclerView) findViewById(R.id.pull_swipe_recyclerView);

        pull_swipe_recyclerView.setMode(PullToRefreshBase.Mode.BOTH);
        pull_swipe_recyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<SwipeMenuRecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<SwipeMenuRecyclerView> refreshView) {
                // 下拉刷新监听
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(MainActivity.SLEEP_TIME);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    list.clear();
                                    DataModel.initData(list, "SwipeRecyclerView");
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(PullSwipeRecyclerViewActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                                    pull_swipe_recyclerView.onRefreshComplete();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<SwipeMenuRecyclerView> refreshView) {
                // 上拉加载监听
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(MainActivity.SLEEP_TIME);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    DataModel.initData(list, "SwipeRecyclerView");
                                    adapter.notifyDataSetChanged();
                                    pull_swipe_recyclerView.onRefreshComplete();
                                    Toast.makeText(PullSwipeRecyclerViewActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
           

然后,和我们上面的那个 RecyclerView 一样,先通过 getRefreshableView() 拿到 SwipeMenuRecyclerView , 设置初始化方法:

swipe_recyclerView = pull_swipe_recyclerView.getRefreshableView();
        swipe_recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 添加侧滑菜单
        swipe_recyclerView.setSwipeMenuCreator(creator);
        // 设置侧滑菜单点击事件
        swipe_recyclerView.setSwipeMenuItemClickListener(menuItemClickListener);
           

下面是添加侧滑菜单以及菜单点击事件的方法:

/**
     * 右滑菜单点击事件
     */
    private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() {
        /**
         * Item的菜单被点击的时候调用。
         * @param closeable       closeable. 用来关闭菜单。
         * @param adapterPosition adapterPosition. 这个菜单所在的item在Adapter中position。
         * @param menuPosition    menuPosition. 这个菜单的position。比如你为某个Item创建了2个MenuItem,那么这个position可能是是 0、1,
         */
        @Override
        public void onItemClick(Openable closeable, int adapterPosition, int menuPosition) {
            closeable.smoothCloseMenu();// 关闭被点击的菜单。

            Toast.makeText(PullSwipeRecyclerViewActivity.this, "adapter Position : " + adapterPosition + " , menuposition : " + menuPosition, Toast.LENGTH_SHORT).show();
        }

    };

    /**
     * 创建右滑菜单
     */
    SwipeMenuCreator creator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeRightMenu, int viewType) {
            String str = "";
            int color = ;
            int width = getResources().getDimensionPixelSize(R.dimen.dp_70);
            int widthdel = getResources().getDimensionPixelSize(R.dimen.dp_70);
            // viewType 的值来自于 Adapter 中的getItemViewType 方法;
//            switch (viewType) {
//                case 0:
//                    width = getResources().getDimensionPixelSize(R.dimen.dp_70);
//                    str = "置顶";
//                    color = R.color.c_2ecc71;
//                    widthdel = getResources().getDimensionPixelSize(R.dimen.dp_70);
//                    break;
//                case 1:
//                    width = getResources().getDimensionPixelSize(R.dimen.dp_100);
//                    widthdel = getResources().getDimensionPixelSize(R.dimen.dp_70);
//                    str = "取消置顶";
                    color = R.color.c_ff7a7a;
//                    break;
//                case 3:
//                    width = 0;
//                    widthdel = 0;
//                    break;
//            }
            str = "置顶";
            // 添加右侧的,如果不添加,则右侧不会出现菜单。
            SwipeMenuItem deleteItem = new SwipeMenuItem(PullSwipeRecyclerViewActivity.this)
                    .setBackgroundDrawable(color)
                    .setText(str) // 文字,还可以设置文字颜色,大小等。。
                    .setTextColor(Color.WHITE)
                    .setTextSize()
                    .setWidth(width)
                    .setHeight(getResources().getDimensionPixelSize(R.dimen.dp_70));
            swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。

            SwipeMenuItem addItem = new SwipeMenuItem(PullSwipeRecyclerViewActivity.this)
                    .setBackgroundDrawable(R.color.color_99)
                    .setText("删除")
                    .setTextColor(Color.WHITE)
                    .setTextSize()
                    .setWidth(widthdel)
                    .setHeight(getResources().getDimensionPixelSize(R.dimen.dp_70));
            swipeRightMenu.addMenuItem(addItem); // 添加一个按钮到右侧菜单。

        }
    };
           

上面的一些注释说明,相信大家应该知道怎么去控制了。

接下来要说一下这个 SwipeMenuRecyclerView 的 Adapter 了,上面两个基本上都是其原生的适配器写法,但是这个 Adaper 就需要去 继承 SwipeMenuAdapter ,下面直接贴出我 Demo 中的 侧滑 Adapter :

class SwipeRecyclerViewAdapter extends SwipeMenuAdapter<SwipeRecyclerViewAdapter.ViewHolder> {

        private List<String> list;

        public SwipeRecyclerViewAdapter(List<String> list) {
            this.list = list;
        }

        @Override
        public View onCreateContentView(ViewGroup parent, int viewType) {
            return LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        @Override
        public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
            return new ViewHolder(realContentView);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.textView.setText(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView textView;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(android.R.id.text1);
            }
        }
    }
           

基本的用法也就这些,我都在 PullToRefresh Module 中集成了,有需要的可以直接下载 Demo,然后引用 pulltorefresh Module 就可以了。

GitHub地址

如果你高兴的话,可以顺便给个 Star 哈。

参考链接:

http://blog.csdn.net/xuehuayous/article/details/50387089

http://blog.csdn.net/yanzhenjie1003/article/details/52115566