(android基础) RecyclerView使用

本文介绍了Android开发中RecyclerView的使用,作为ListView的增强版,RecyclerView提供了更强大的功能。内容包括如何导入依赖,创建列表视图,设计单个列表元素的视图,以及在MainActivity.java中实现逻辑。通过创建HomeAdapter,重写Adapter的相关方法,如onCreateViewHolder、onBindViewHolder和getItemCount,以实现数据绑定和列表展示。此外,还涉及了内部类MyViewHolder的设计,用于持有Item界面上的控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RecyclerView是谷歌提供的控件,与ListView相似,但更强大

1.导入依赖:

    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'

在activity_main.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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/id_recyclerview"
        >
    </androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>

再创建单个列表元素的视图:
recycler_item.xml

<?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="wrap_content"
    android:padding="16dp"
    android:gravity="center"
    android:orientation="horizontal"
    >
    <ImageView
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:id="@+id/iv"
        android:src="@drawable/ic_launcher_foreground"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:textSize="20sp"
            android:textColor="#FF8F03"
            android:text="哈士奇"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/introduce"
            android:textSize="16sp"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/name"
            android:textColor="#FF716C6D"
            android:maxLines="2"
            android:ellipsize="end"
            android:text="西伯利亚雪橇犬,别名二哈,或者哈士奇"
            />
    </RelativeLayout>
</LinearLayout>

逻辑MainActivity.java:

package com.example.againrecyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private HomeAdapter homeAdapter;

    private String[] names = {"鱼人","猪人","猎犬","蝙蝠","克劳斯"};
    private String[] introduces = {"西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇"};
    private int[] icons = {R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = ((RecyclerView) findViewById(R.id.id_recyclerview));

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        homeAdapter = new HomeAdapter();
        recyclerView.setAdapter(homeAdapter);
    }
    class HomeAdapter extends  RecyclerView.Adapter<HomeAdapter.MyViewHolder>{

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.recycler_item,parent,false));
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.name.setText(names[position]);
            holder.introduce.setText(introduces[position]);
            holder.iv.setBackgroundResource(icons[position]);
        }

        @Override
        public int getItemCount() {
            return names.length;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            TextView name;
            ImageView iv;
            TextView introduce;

            public MyViewHolder(View view){
                super(view);
                name = ((TextView) view.findViewById(R.id.name));
                iv = ((ImageView) view.findViewById(R.id.iv));
                introduce = ((TextView) view.findViewById(R.id.introduce));
            }
        }
    }

}

要点创建两个内部类:
HomeAdapter 继承 RecyclerView.Adapter类 重写 其方法 :
onCreatViewHolder()方法主要是加载布局文件
onBindViewHolder()方法主要将数据设置到对应的控件上
getItemCount()方法获取列表条目总数

MyViewHolder 类继承 RecyclerView.ViewHolder
主要获取Item界面上的控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值