RecyclerView概述
RecyclerView是官方在5.0之后新添加的控件,推出用来替代传统的ListView和GridView列表控件。
RecyclerView使用-基础篇
第一步:添加RecyclerView
**方法1:**点开任意一个布局文件,找到左边的RecyclerView控件,点击旁边的按钮即可添加RecyclerView。
**方法2:**在build.gradle文件中添加依赖
implementation 'androidx.recyclerview:recyclerview:1.1.0'
第二步:添加布局文件
在布局文件activity_main.xml中先添加RecyclerView控件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
再添加一个item_list.xml的布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="29dp" android:layout_marginTop="33dp" android:text="标题" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_marginTop="18dp" android:text="内容" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> </androidx.constraintlayout.widget.ConstraintLayout>
第三步:添加逻辑代码
首先定一个实体类如下:
public class News { public String title; // 标题 public String content; //内容 }
然后在MainActivity中定义内部类 ViewHolder类、 MyAdapter类以及设置RecyclerView相关逻辑
package com.lucashu.recyclerview; 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.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView mRecyclerView; MyAdapter mMyAdapter ; List<News> mNewsList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.recyclerview); // 构造一些数据 for (int i = 0; i < 50; i++) { News news = new News(); news.title = "标题" + i; news.content = "内容" + i; mNewsList.add(news); } mMyAdapter = new MyAdapter(); mRecyclerView.setAdapter(mMyAdapter); LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this); mRecyclerView.setLayoutManager(layoutManager); } class MyAdapter extends RecyclerView.Adapter<MyViewHoder> { @NonNull @Override public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(MainActivity.this, R.layout.item_list, null); MyViewHoder myViewHoder = new MyViewHoder(view); return myViewHoder; } @Override public void onBindViewHolder(@NonNull MyViewHoder holder, int position) { News news = mNewsList.get(position); holder.mTitleTv.setText(news.title); holder.mTitleContent.setText(news.content); } @Override public int getItemCount() { return mNewsList.size(); } } class MyViewHoder extends RecyclerView.ViewHolder { TextView mTitleTv; TextView mTitleContent; public MyViewHoder(@NonNull View itemView) { super(itemView); mTitleTv = itemView.findViewById(R.id.textView); mTitleContent = itemView.findViewById(R.id.textView2); } } }
运行效果