RecyclerView简单例子演示
Posted 轻烟散入五侯家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView简单例子演示相关的知识,希望对你有一定的参考价值。
一、首先主活动的xml布局文件activity.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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
二、RecyclerView适配器的适配类型(用一个实体类Fruit表示),要这么写
package com.example.magicalpai.recyclerview_view_demo;
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
三、RecyclerView子项我们自己指定的布局fruit_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">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="274dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp" />
</LinearLayout>
四、RecyclerView适配器FruitAdapter,要这么写`
package com.example.magicalpai.recyclerview_view_demo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.example.magicalpai.R;
import java.util.List;
//这个FruitAdapter是为RecyclerView准备的一个适配器,它的泛型指定为FruitAdapter.ViewHolder,这个ViewHolder是我们在FruitAdapter中定义的一个内部类
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
ImageView fruitImage;
TextView fruitName;
//ViewHolder的构造函数要传入View参数,这个参数通常就是RecyclerView子项的最外层布局,
public ViewHolder(View view) {
super(view);
fruitView = view;//利用fruitView变量来保存子项最外层布局的实例
fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList) {
mFruitList = fruitList;
}
//这个方法是用来创建ViewHolder实例的
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//将fruit_item布局加载进来,然后创建一个ViewHolder实例
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
//为最外层布局注册点击事件
holder.fruitView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(v.getContext(), "you clicked view " + fruit.getName(),
Toast.LENGTH_SHORT).show();
}
});
//为ImageView注册点击事件
holder.fruitImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(v.getContext(), "you clicked image " + fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
return holder;
}
//这个方法是用于对RecyclerView子项的数据进行赋值的,会在每个子项被滚动到屏幕内的时候执行
@Override
public void onBindViewHolder(ViewHolder holder, int position) { //通过position参数得到当前项的Fruit实例
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
//这个方法用于告诉RecyclerView一共有多少个子项
@Override
public int getItemCount() {
return mFruitList.size();
}
}
五、主活动的类MainActivity,要这么写`
package com.example.magicalpai.recyclerview_view_demo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.magicalpai.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);initFruits(); // 初始化水果数据
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
//创建一个LinearLayoutManager对象,可以实现和ListView类似的效果。
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 3; i++) {
Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(banana);
Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
fruitList.add(watermelon);
}
}
}
六、这样就可以实现以下效果
点击图片的控件,还有相应的提示
以上是关于RecyclerView简单例子演示的主要内容,如果未能解决你的问题,请参考以下文章
Android:RecyclerView 不显示片段中的列表项