RecyclerView的简单使用总结java/kotlin

作为Android开发的你一定对RecyclerView再熟悉不过了,

相信在你的各个大小项目中都有用到,

这篇文章主要属于作最基础的总结和笔记,包含Adapter种java和kotlin的写法,

好了废话不多说,直接进入正文.


基本使用:

1.使用前需要在gradle中添加依赖:

implementation 'androidx.recyclerview:recyclerview:1.1.0'

2.编写相关代码,首先要在xml文件中写RecyclerView的布局:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

然后在activity中获取RecyclerView,设置相关的布局管理器及adapter:

        //通过findViewById拿到RecyclerView实例
        recyclerView = findViewById(R.id.recyclerview);
        //设置RecyclerView管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        //初始化适配器
        testAdapter = new TestAdapter(this);
        //设置数据
        testAdapter.setmData(mDatas);
        //设置适配器
        recyclerView.setAdapter(testAdapter);

TestAdapter代码如下(java):

package com.example.testdemo0;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Random;

/**
 * create by joy
 * create on 2023/10/20
 * description
 */
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestViewHolder> {

    private Context mContext;
    private List<String> mData;

    public TestAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmData(List<String> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent, false);
        return new TestViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TestViewHolder holder, int position) {
        //随机背景颜色
        Random random = new Random();
        int color = Color.argb(255,random.nextInt(256),random.nextInt(256),random.nextInt(256));
        holder.textView.setBackgroundColor(color);
        holder.textView.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData == null? 0:mData.size();
    }

    class TestViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public TestViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }
}

TestAdapter代码如下(kotlin):

package com.example.testdemo0

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.testdemo0.TestAdapter.TestViewHolder
import com.example.testdemo0.databinding.ItemTestBinding
import java.util.*

/**
 * create by joy
 * create on 2023/10/20
 * description
 */
class TestAdapter(
    private val mContext: Context,
) : RecyclerView.Adapter<TestViewHolder>() {

    private var mData: List<String>? = null

    fun setmData(mData: List<String>?) {
        this.mData = mData
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
        val binding = ItemTestBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return TestViewHolder(binding)
    }

    override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
        //随机背景颜色
        val random = Random()
        val color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
        val binding = holder.binding
        val data = mData?.get(position)
        binding.text.setBackgroundColor(color)
        binding.text.text = data
    }

    override fun getItemCount(): Int {
        return mData?.size ?: 0
    }

    class TestViewHolder(val binding: ItemTestBinding) : RecyclerView.ViewHolder(binding.root)
}

运行结果如下(竖向布局):

横向布局:

竖向布局

设置横向布局:

recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

运行结果如下(横向布局):

设置网格布局:

recyclerView.setLayoutManager(new GridLayoutManager(this, 4));

运行结果如下(网格布局):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值