介绍:RecyclerView是一个非常好用的控件,它和ListView非常的相似,它们都是用来处理很多条目的需求的,例如微信聊天页面,但是ListView他是自己写,但是RecyclerView是一个框架,写好的模板,可以直接拿来用,优化也比ListView好,也可以满足垂直,水平,瀑布以及网格布局。
接下来咱们来一起做一个demo来熟悉一下吧,先给朋友们看看项目架构
那么我们开始上代码:(大家不熟练的要自己多打哦,而不是做cv工程师哦)
这个是主页面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/id_rv">
<!--这个是RecyclerView布局的作用域,条目都会显示出来-->
</androidx.recyclerview.widget.RecyclerView>
这个是对应主页面布局的java代码块
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.feizhi.recyclerviewdemo.R;
import com.feizhi.recyclerviewdemo.adapter.RecyclerViewAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Integer> icon = new ArrayList<>();
private List<String> title = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个是图片,可以自己添加
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
icon.add(R.mipmap.ic_launcher);
//这个是图片介绍,也可以自己喜欢自定义
title.add("aaaaaaa");
title.add("aaaaaaa");
title.add("aaaaaaa");
title.add("aaaaaaa");
title.add("aaaaaaa");
title.add("aaaaaaa");
title.add("aaaaaaa");
title.add("aaaaaaa");
recyclerView = findViewById(R.id.id_rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(new RecyclerViewAdapter(this, icon, title));
}
}
这个是Adapter适配器
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.feizhi.recyclerviewdemo.R;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHodler> {
private Context context;
private List<Integer> icon = new ArrayList<>();
private List<String> title = new ArrayList<>();
private LayoutInflater inflater;
//这个是构造方法
public RecyclerViewAdapter(Context context, List<Integer> icon, List<String> title) {
inflater = LayoutInflater.from(context);
this.context = context;
this.icon = icon;
this.title = title;
}
//这个用来绑定Item布局
@Override
public MyViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHodler(inflater.inflate(R.layout.item_recycler, parent, false));
}
//初始化控件
@Override
public void onBindViewHolder(MyViewHodler holder, int position) {
holder.textView.setText(title.get(position));
holder.imageView.setImageResource(icon.get(position));
}
//一共返回的条目有多少,就是一共有多少条数据显示
@Override
public int getItemCount() {
return icon.size();
}
//用来给控件赋值
class MyViewHodler extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public MyViewHodler(View v) {
super(v);
imageView = v.findViewById(R.id.image_view);
textView = v.findViewById(R.id.text_view);
}
}
}
这个是item_recycler布局
<?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:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好"
android:layout_gravity="center_vertical"
android:textSize="20sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
现在运行是没有问题了,展示结果:
但是现在有个问题,它每一个条目点击不了,没有反应,那么我们应该有需求,要点击给到反应,那么我们来试试吧,其实是有两种方式,第一种是在适配器中设置,还有一种就是在Main Java类种设置,让我来一一介绍吧
在适配器中设置
找到适配器种的public void onBindViewHolder()的方法,把以下代码加进去里面
@Override
public void onBindViewHolder(MyViewHodler holder, int position) {
holder.textView.setText(title.get(position));
holder.imageView.setImageResource(icon.get(position));
//设置图片的点击事件
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "点击了第" + position + "个", Toast.LENGTH_SHORT).show();
}
});
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "标题是:" + title.get(position), Toast.LENGTH_SHORT).show();
}
});
}
结果如下,但是这样是有问题的,我们写在Main Java类中写变化会比较多,所以我们试试吧,但是首先我们得先去把刚才得代码注释掉
在适配器中加入这几行代码
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
在public void onBindViewHolder()方法中加以下代码
@Override
public void onBindViewHolder(MyAdapter holder, @SuppressLint("RecyclerView") int position) {
holder.textView.setText(title.get(position));
holder.imageView.setImageResource(image.get(position));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v, position);
}
});
}
回到主页面中,在添加适配器中加入点击方法
recyclerView = findViewById(R.id.id_rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, icon, title);
recyclerView.setAdapter(adapter);
//这是新增的,用来设置点击事件
adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "点击了第" + position + "个", Toast.LENGTH_SHORT).show();
}
});
这样就没有问提, 点击事件是灵活的,目前我只是做了一个Toast提示而已,如果想要跳转可以查阅以下资料或者自己问我,图片和图片描述也可以自己手动换成自己喜欢的哦,再见哦,各位拜拜