美文网首页
Android-RecyclerView的简单使用

Android-RecyclerView的简单使用

作者: 晴天ccc | 来源:发表于2019-06-06 09:56 被阅读0次

    目录

    • 前言
    • 简单使用
      ---- 添加依赖
      ---- 创建MainActivity的布局文件
      ---- 创建Item的布局文件
      ---- 创建Person实体类
      ---- 创建MyAdapter适配器类
      ---- 在MainActivity中调用
      ---- 为Item添加点击事件

    前言

    RecyclerView是在Android5.0版本作为support-v7的一个组件出现,高效替代了最初的ListView等列表组件。

    简单使用

    build.gradle中添加依赖

     implementation 'androidx.recyclerview:recyclerview:1.1.0'
    

    创建layout_recyclerview的XML布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    

    创建layout_recyclerview_item的XML布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layoutView"
        android:layout_width="match_parent"
        android:layout_height="50dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">
    
            <ImageView
                android:id="@+id/leftImgView"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginStart="20dp"
                android:src="@mipmap/mine_ic_message" />
    
            <TextView
                android:id="@+id/titleLab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="50dp"
                android:layout_toRightOf="@+id/leftImgView"
                android:text="这是一个标题"
                android:textSize="16sp" />
    
            <ImageView
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="20dp"
                android:src="@mipmap/mine_ic_arrow_right" />
    
        </RelativeLayout>
    
    </RelativeLayout>
    
    

    创建Person实体类

    
    public class Person {
    
        public String realName;                 // 姓名
        public int sex = 1;                     // 性别(1:女,2:男),默认为女
    
        public String getRealName() {
            return realName;
        }
        public void setRealName(String realName) {
            this.realName = realName;
        }
        public int getSex() {
            return sex;
        }
        public void setSex(int sex) {
            this.sex = sex;
        }
    }
    
    

    创建MyAdapter适配器类

    
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
        private Context mContext;
        private List<Person> mList;
    
        // 构造方法
        public MyAdapter(Context mContext, List<Person> mList) {
            this.mContext = mContext;
            this.mList = mList;
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_recyclerview_item, parent, false);
            MyViewHolder holder = new MyViewHolder(itemView);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Person person = mList.get(position);
            holder.titleLab.setText(person.getRealName());
        }
    
        @Override
        public int getItemCount() {
            return mList.size();
        }
    
        // ViewHolder重用组件
        public class MyViewHolder extends RecyclerView.ViewHolder {
            private ImageView leftImageView;
            private TextView titleLab;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                leftImageView = itemView.findViewById(R.id.leftImgView);
                titleLab = itemView.findViewById(R.id.titleLab);
            }
        }
    }
    
    

    MainActivity中调用

    public class MainActivity extends AppCompatActivity {
    
        private RecyclerView myRecyclerView;
        private MyAdapter myAdapter;
        private List<Person> myList;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_recyclerview);
    
            initData();
            initRecyclerView();
        }
    
        private void initData(){
            myList = new ArrayList<>();
            for(int i = 0; i <= 10; i++){
                Person person = new Person();
                person.realName = "张" + (char)i;
                myList.add(person);
            }
        }
    
        private void initRecyclerView() {
    
            // 适配器
            myAdapter = new MyAdapter(this, myList);
            // 布局管理器
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            // RecyclerView
            myRecyclerView = findViewById(R.id.recyclerView);
            myRecyclerView.setLayoutManager(linearLayoutManager);
            myRecyclerView.setAdapter(myAdapter);
            // 设置默认分割线
            myRecyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL)); 
        }
    }
    

    为Item添加点击事件

    MyAdapter适配器类中做如下修改

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
        private Context mContext;
        private List<Person> mList;
    
        // Item点击事件回调
        private OnItemClickBlock mOnItemClickBlock;
    
        // 构造方法
        public MyAdapter(Context mContext, List<Person> mList) {
            this.mContext = mContext;
            this.mList = mList;
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_recyclerview_item, parent, false);
            MyViewHolder holder = new MyViewHolder(itemView);
            // Item点击事件监听
            holder.layoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickBlock. onItemClick( view,  pos);
                }
            });
            holder.leftImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickBlock.onItemClick(view, pos);
                }
            });
            holder.titleLab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickBlock.onItemClick(view, pos);
                }
            });
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Person person = mList.get(position);
            holder.titleLab.setText(person.getRealName());
        }
    
        @Override
        public int getItemCount() {
            return mList.size();
        }
    
        // ViewHolder重用组件
        public class MyViewHolder extends RecyclerView.ViewHolder {
            private RelativeLayout layoutView;
            private ImageView leftImageView;
            private TextView titleLab;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                layoutView = itemView.findViewById(R.id.layoutView);
                leftImageView = itemView.findViewById(R.id.leftImgView);
                titleLab = itemView.findViewById(R.id.titleLab);
            }
        }
    
        // Item点击事件回调
        public interface OnItemClickBlock {
            void onItemClick(View view, int position);
            void onItemLongClick(View view, int position);
        }
    
        public void setOnItemClickBlock(OnItemClickBlock onItemClickListener) {
            this.mOnItemClickBlock = onItemClickListener;
        }
    }
    
    

    MainActivity类中做如下修改

        private void initRecyclerView() {
    
            // 适配器
            myAdapter = new MyAdapter(this, myList);
            myAdapter.setOnItemClickBlock(new MyAdapter.OnItemClickBlock() {
                @Override
                public void onItemClick(View view, int position) {
                    if (view.getId() == R.id.layoutView) {
                        Toast.makeText(FileQueryMainListActivity.this, position + "" + "layoutView", Toast.LENGTH_SHORT).show();
                    } else if (view.getId() == R.id.leftImgView) {
                        Toast.makeText(FileQueryMainListActivity.this, position + "" + "leftImgView", Toast.LENGTH_SHORT).show();
                    } else if (view.getId() == R.id.titleLab) {
                        Toast.makeText(FileQueryMainListActivity.this, position + "" + "titleLab", Toast.LENGTH_SHORT).show();
                    }
                }
    
                @Override
                public void onItemLongClick(View view, int position) {
    
                }
            });
            // 布局管理器
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            // RecyclerView
            myRecyclerView = findViewById(R.id.recyclerView);
            myRecyclerView.setLayoutManager(linearLayoutManager);
            myRecyclerView.setAdapter(myAdapter);
            // 设置默认分割线
            myRecyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        }
    

    到这里我们直接运行代码查看效果吧!

    相关文章

      网友评论

          本文标题:Android-RecyclerView的简单使用

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