build.gradle中 dependencies 添加下方依赖
implementation 'androidx.recyclerview:recyclerview:1.1.0'
布局 main、recycle_view
<?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="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
<?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=".recyclerView"
tools:ignore="MissingDefaultResource">
<TextView
android:id="@+id/tv"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity、适配器MyAdapter
public class MainActivity extends AppCompatActivity {
// 定义一个Java Bean的List集合
private List<Bean> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建数据,未来给适配器使用
for (int i = 9990; i < 10100; i++) {
Bean bean = new Bean();
bean.setName("张三" + i);
data.add(bean);
}
// 获取RecyclerView的布局对象
RecyclerView recyclerView = findViewById(R.id.rv);
// 需要自定义布局才可以使用 如:LinearLayoutManager、网格GridLayoutManager、瀑布流StaggeredGridLayoutManager
// LinearLayoutManager viewManager = new LinearLayoutManager(this);
// GridLayoutManager viewManager = new GridLayoutManager(this,3);
StaggeredGridLayoutManager viewManager = new StaggeredGridLayoutManager(3, LinearLayout.VERTICAL);
// 布局对象绑定自定义布局(不绑定就没有外观,且控制台报错)
recyclerView.setLayoutManager(viewManager);
// 创建适配器对象,以渲染数据
MyAdapter myAdapter = new MyAdapter(data, this);
// 布局对象绑定适配器,以便于渲染视图了
recyclerView.setAdapter(myAdapter);
// 适配器绑定自定义的单击回调(需要在适配器内部自己创建方法!)
myAdapter.setRecycleItemClickListener(new MyAdapter.OnRecyClerItemClickListener() {
@Override
public void onRecyClerItemClick(int positon) {
Log.e("自定义回调","点击了:"+positon);
}
});
}
}
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
// 自定义适配器需要继承RecyclerView.Adapter的,且强制我们创建内部类MyViewHolder,防止我们遗忘导致不停的findViewById
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
// 数据集合
private List<Bean> list;
private Context context;
// 适配器的构造方法,并传入相应的参数
public MyAdapter(List<Bean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 获取布局
View view = View.inflate(context, R.layout.activity_recycler_view, null);
return new MyViewHolder(view);
}
// 绑定数据
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv.setText(list.get(position).getName());
}
// 获取数据数量
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
private OnRecyClerItemClickListener mOnItemClickListener;
// 继承自己的内部类,防止我们遗忘导致不停的findViewById
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v != null) {
mOnItemClickListener.onRecyClerItemClick(getAdapterPosition());
}
}
});
}
}
// 设定监听事件
public void setRecycleItemClickListener(OnRecyClerItemClickListener listener) {
mOnItemClickListener = listener;
}
// 定义本类接口,使得本类拥有自定义功能,需要在调用适配器时,绑定时间才可用
public interface OnRecyClerItemClickListener {
void onRecyClerItemClick(int positon);
}
}
效果演示https://www.ixigua.com/iframe/7094552363721031694?autoplay=0
特殊说明: 以上文章,均是我实际操作,写出来的笔记资料,不会盗用别人文章!烦请各位,请勿直接盗用!转载记得标注来源!