• RecycleView的简单使用


    简介

    该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView、GridView、瀑布流布局。

    有了ListView、GridView为什么还需要RecyclerView这样的控件呢?整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现酷炫的效果。

    提供的几个关键方法有:

    1. setLayoutManager:设置布局方式

    2. setAdapter:设置Item的Adapter

    3. setItemAnimator:设置Item的动画效果

    4. addItemDecoration:分割线的样式

    实现ListView功能

    public class RecycleActivity extends AppCompatActivity {
        @BindView(R.id.recycle_view)
        RecyclerView recycleView;
        private List<String> mDatas = new ArrayList<>();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycle);
            ButterKnife.bind(this);
    
            initData();
    
            recycleView.setAdapter(new MyAdapter(mDatas));
            recycleView.setLayoutManager(new LinearLayoutManager(this));
            DividerItemDecoration dividerItemDecorationV = new DividerItemDecoration(
                    RecycleActivity.this, DividerItemDecoration.VERTICAL);
            recycleView.addItemDecoration(dividerItemDecorationV);
        }
    
        private void initData() {
            for (int i = 0; i < 100; i++) {
                mDatas.add(String.valueOf(i));
            }
        }
    
        private static class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView tv;
    
            public MyViewHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.holder_view_tv);
            }
        }
    
        private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
            private List<String> mDatas;
    
            public MyAdapter(List<String> datas) {
                mDatas = datas;
            }
    
            @Override
            public int getItemCount() {
                return mDatas.size();
            }
    
            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                holder.tv.setText(mDatas.get(position));
            }
    
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_view, parent, false);
                MyViewHolder myViewHolder = new MyViewHolder(itemView);
                return myViewHolder;
            }
        }
    }
    
    

    运行截图

    实现GridView功能

    public class RecycleActivity extends AppCompatActivity {
        @BindView(R.id.recycle_view)
        RecyclerView recycleView;
        private List<String> mDatas = new ArrayList<>();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycle);
            ButterKnife.bind(this);
    
            initData();
    
            recycleView.setAdapter(new MyAdapter(mDatas));
            recycleView.setLayoutManager(new GridLayoutManager(this, 3));
            DividerItemDecoration dividerItemDecorationV = new DividerItemDecoration(
                    RecycleActivity.this, DividerItemDecoration.VERTICAL);
            DividerItemDecoration dividerItemDecorationH = new DividerItemDecoration(
                    RecycleActivity.this, DividerItemDecoration.HORIZONTAL);
            recycleView.addItemDecoration(dividerItemDecorationV);
            recycleView.addItemDecoration(dividerItemDecorationH);
        }
    
        private void initData() {
            for (int i = 0; i < 100; i++) {
                mDatas.add(String.valueOf(i));
            }
        }
    
        private static class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView tv;
    
            public MyViewHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.holder_view_tv);
            }
        }
    
        private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
            private List<String> mDatas;
    
            public MyAdapter(List<String> datas) {
                mDatas = datas;
            }
    
            @Override
            public int getItemCount() {
                return mDatas.size();
            }
    
            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                holder.tv.setText(mDatas.get(position));
            }
    
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_view, parent, false);
                MyViewHolder myViewHolder = new MyViewHolder(itemView);
                return myViewHolder;
            }
        }
    }
    

    运行截图

    实现瀑布流

    public class RecycleActivity extends AppCompatActivity {
        @BindView(R.id.recycle_view)
        RecyclerView recycleView;
        private List<String> mDatas = new ArrayList<>();
        private List<Integer> mHeights = new ArrayList<>();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycle);
            ButterKnife.bind(this);
    
            initData();
    
            recycleView.setAdapter(new MyAdapter(mDatas, mHeights));
            StaggeredGridLayoutManager staggeredGridLayoutManager
                    = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
            recycleView.setLayoutManager(staggeredGridLayoutManager);
        }
    
        private void initData() {
            for (int i = 0; i < 100; i++) {
                mDatas.add(String.valueOf(i));
                mHeights.add((int) (100 + Math.random() * 300));
            }
        }
    
        private static class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView tv;
    
            public MyViewHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.holder_view_tv);
            }
        }
    
        private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
            private List<String> mDatas;
            private List<Integer> mHeights;
    
            public MyAdapter(List<String> datas, List<Integer> heights) {
                mDatas = datas;
                mHeights = heights;
            }
    
            @Override
            public int getItemCount() {
                return mDatas.size();
            }
    
            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                holder.tv.setText(mDatas.get(position));
                ViewGroup.LayoutParams params = holder.tv.getLayoutParams();
                params.height =  mHeights.get(position);
                holder.tv.setLayoutParams(params);
            }
    
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_view, parent, false);
                MyViewHolder myViewHolder = new MyViewHolder(itemView);
                return myViewHolder;
            }
        }
    }
    

    运行截图

  • 相关阅读:
    请求格式
    表格操作laytpl
    layui的弹出框
    layui表格
    js概念
    栈,队列
    数据结构--线性数据结构
    Symbol详情: 在不支持es6的浏览器上,通过Babel转译
    jquery中用bootstrap中的表单验证及提交
    layui的layer报错 layer is not defined at checkUse
  • 原文地址:https://www.cnblogs.com/jyx140521/p/6806923.html
Copyright © 2020-2023  润新知