RecylerView的介绍与简单应用

本文介绍了RecylerView的基本概念,强调了它在大数据集展示和视图复用上的优势,并对比了与ListView的区别。内容包括RecylerView的依赖添加、初始化、Adapter设置,以及如何实现水平滚动、网格布局、瀑布流和使用多种ViewHolder。
摘要由CSDN通过智能技术生成

简单介绍

    RecylerView能够灵活地实现大数据集的展示,视图的复用管理比ListView更好,能够显示列表,网格,瀑布流等形式,且不同的ViewHolder能够实现item多元化的功能.

使用准备,添加依赖

implementation 'androidx.recyclerview:recyclerview:1.1.0'
下列例子所有依赖
dependencies {
   

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

RecyclerView 的简单使用

一:RecyclerView 的初始化

        recyclerView = findViewById(R.id.recycler1);
        recyclerView.setLayoutManager(new LinearLayoutManager(LinearRecycleActivity.this));
       //recyclerView.setAdapter(new CLinearAdapter(LinearRecycleActivity.this));简单使用
        recyclerView.setAdapter(new CLinearAdapter(LinearRecycleActivity.this, new CLinearAdapter.onItemClickListener() {
   
            @Override
            public void click(int pos) {
   
                Toast.makeText(LinearRecycleActivity.this,"InActivity cilik is "+pos,Toast.LENGTH_SHORT).show();
            }
        }));//这里嵌入了一个lambda表达式,用于实现监听功能

 //xml:       
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:padding="50dp">

    <Button
        android:id="@+id/btn_toListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="toListView"
        android:textAllCaps="false"/>

</LinearLayout>
  • 二:RecyclerView 的 Adapter

public class CLinearAdapter extends RecyclerView.Adapter <CLinearAdapter.LinearViewHolder>{
   

    private Context context;

    private onItemClickListener listener;

    CLinearAdapter(Context context,onItemClickListener listener){
   
        this.context = context;
        this.listener = listener;
    }

    @Override
    public CLinearAdapter.LinearViewHolder onCreateViewHolder(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值