记录一下Recycler View的基础写法

  • 刚接触安卓
  • 记录一下Recycler View的思路
  • 比较基础

1.引入依赖

implementation 'androidx.recyclerview:recyclerview:1.2.1'

2.绘制布局xml

  • 首先在Main的布局中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
  • 其次绘制Item的布局(包含头像,名称,介绍)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp" />

    </LinearLayout>

</LinearLayout>

3.Java代码

  • 以下是Java代码的写法
  • 首先set了LayoutManager
  • 之后创建了一个Adapter
  • 再之后set了Adapter
  • 再ViewHolder的构造函数中对项进行监听
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<Person> list = new ArrayList<>();

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recycler);
        // 固定recyclerView保持固定大小,这样可以优化RecyclerView的性能
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        // 设置recyclerView的滚动方向
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        initData();
        RecyclerView.Adapter adapter = new RecyclerView.Adapter<PersonViewHolder>() {
            /**
             * 创建列表项组件的方法,使用该方法说创建的组件会自动被缓存
             */
            @NonNull
            @Override
            public PersonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
                return new PersonViewHolder(view, this);
            }

            /**
             * 为列表项组件绑定数据的方法,每次组件重新显示出来时都会重新执行该方法
             */
            @Override
            public void onBindViewHolder(@NonNull PersonViewHolder holder, int position) {
                holder.nameTv.setText(list.get(position).getName());
                holder.descTv.setText(list.get(position).getDesc());
                holder.headerTv.setImageResource(list.get(position).getImage());
            }

            //该方法返回值为多少个列表项
            @Override
            public int getItemCount() {
                return list.size();
            }
        };
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        String[] names = new String[]{"wang", "hua", "liu", "deng"};
        String[] desc = new String[]{"ziqi", "jiahua", "xuxin", "wenwei"};
        int[] imageIds = new int[]{R.drawable.ic_baseline_emoji_emotions_24,
                R.drawable.ic_baseline_emoji_emotions_24,
                R.drawable.ic_baseline_emoji_emotions_24,
                R.drawable.ic_baseline_emoji_emotions_24};
        for (int i = 0; i < names.length; i++) {
            list.add(new Person(names[i], desc[i], imageIds[i]));
        }
    }

    class PersonViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView nameTv;
        TextView descTv;
        ImageView headerTv;
        private RecyclerView.Adapter adapter;

        public PersonViewHolder(@NonNull View view, RecyclerView.Adapter adapter) {
            super(view);
            this.nameTv = view.findViewById(R.id.name);
            this.descTv = view.findViewById(R.id.desc);
            this.headerTv = view.findViewById(R.id.header);
            this.adapter = adapter;
            this.rootView = view.findViewById(R.id.root);

            //在这里对数据进行监听比较好
            rootView.setOnClickListener(view1 -> {
                TextView nametv = view1.findViewById(R.id.name);
                Toast.makeText(MainActivity.this, "您点击了:" + nametv.getText()
                        , Toast.LENGTH_SHORT).show();
            });
        }
    }
}

class Person {
    private String name;
    private String desc;
    private int image;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public Person(String name, String desc, int image) {
        this.name = name;
        this.desc = desc;
        this.image = image;
    }
}

最终效果

  • 这个图和上面代码的数据用的不一样,但是效果是一样的
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值