美文网首页
RecycleView基础使用

RecycleView基础使用

作者: 小_番茄 | 来源:发表于2018-07-17 16:39 被阅读0次

    第一步

    在gradle中引入

    implementation 'com.android.support:recyclerview-v7:27.1.1'
    

    第二步

    在布局文件中引入

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Three_chapter.RecyclerViewActivity">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.constraint.ConstraintLayout>
    

    第三步

    创建adapter

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private List<String> testDatas;
    
        public MyAdapter(List<String> testDatas) {
            this.testDatas = testDatas;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item_layout, null, false);
            final ViewHolder viewHolder = new ViewHolder(inflate);
            // TODO: 2018/7/17 添加点击事件 
    //        整个子item的点击事件  
            viewHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = viewHolder.getAdapterPosition();
                    Toast.makeText(parent.getContext(), "第" + adapterPosition + "子item的点击事件", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = viewHolder.getAdapterPosition();
                    Toast.makeText(parent.getContext(), "第" + adapterPosition + "位置子item里面的一个控件的点击事件", Toast.LENGTH_SHORT).show();
                }
            });
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.textView.setText(testDatas.get(position));
        }
    
        @Override
        public int getItemCount() {
            return testDatas.size();
        }
    
        static class ViewHolder extends RecyclerView.ViewHolder {
            View view;
            TextView textView;
    
            public ViewHolder(View itemView) {
                super(itemView);
                view = itemView;
                textView = itemView.findViewById(R.id.text);
            }
        }
    }
    
    

    第四步

    使用

    public class RecyclerViewActivity extends AppCompatActivity {
    
        private RecyclerView mRecycleview;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycler_view);
            initView();
        }
    
        private void initView() {
    //        实例化RecyclerView
            mRecycleview = ((RecyclerView) findViewById(R.id.recycler));
           /** 创建一个布局排列管理者,LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager
            *  LinearLayoutManager为线性布局,可以横向也可以纵向
            *  GridLayoutManager为网格布局
            *  StaggeredGridLayoutManager瀑布流布局
            *  GridLayoutManager manager1 = new GridLayoutManager(this, 3);
            *  StaggeredGridLayoutManager manager2 = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
            **/
            LinearLayoutManager manager = new LinearLayoutManager(this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
    //        设置布局
            mRecycleview.setLayoutManager(manager);
    //        实例化adapter
            MyAdapter myAdapter = new MyAdapter(initDatas());
    //        设置adapter
            mRecycleview.setAdapter(myAdapter);
        }
    
        private List<String> initDatas() {
            List<String> strings = new ArrayList<>();
            strings.add("234567890");
            strings.add("23456789023456789");
            strings.add("234567890dfg");
            strings.add("234567890wert");
            strings.add("234567890fdg");
            strings.add("234567890afgolploiuytgrf");
            strings.add("234567890fgmkvf");
            strings.add("234567890q67u");
            strings.add("234567890rfgthyjuikokiubvcdvbnmnbgv");
            strings.add("234567890rty7ytvbyujybgv");
            return strings;
        }
    }
    

    相关文章

      网友评论

          本文标题:RecycleView基础使用

          本文链接:https://www.haomeiwen.com/subject/mrlgpftx.html