本文已参与「新人创作礼」活动,一起开启掘金创作之路。
在开发的过程中,我们经常使用ListView控件,但是ListView也有它的缺点,就是它不能够左右滑动数据,运行效率不高;
所以我们可以使用更强大的控件RecyclerView,可以说它是一个增强版的ListView,Google推荐使用,那就简单的使用一下这个控件
- 下面是效果参考图
纵向的想实现
代码实现
recycleActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".RecycleActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
- 注意:布局加载器 StaggeredGridLayoutManager.HORIZONTAL 为纵向 StaggeredGridLayoutManager.VERTICAL 为横向
RecycleActivity.class
public class RecycleActivity extends AppCompatActivity {
List<Bean> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
//添加50条测试数据
for (int i=1;i<50;i++){
list.add(new Bean(i));
}
RecyclerView recyclerView = findViewById(R.id.recycleView1);
//布局加载器 StaggeredGridLayoutManager.HORIZONTAL 为纵向 StaggeredGridLayoutManager.VERTICAL 为横向
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(6,StaggeredGridLayoutManager.HORIZONTAL));
recyclerView.setAdapter(new RecyclerAdapter(list));
}
}
定义了一个内部类ViewHolder,继承RecyclerView.ViewHolder,然后定义两个两个全局变量,即行布局中的控件,然后再构造方法中绑定控件。 然后将适配器这个类继承RecyclerView.Adapter<内部类>,需要重写onCreateViewHolder()、onBindViewHolder()、getItemCount()这三个方法。
- onCreateViewHolder():创建ViewHolder实例
- onBindViewHolder():对数据进行赋值
- getItemCount():返回有多少数据
RecyclerAdapter和ViewHolder
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
List<Bean> list;
public RecyclerAdapter(List<Bean> list1) {
list=list1;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item1, parent, false);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.MyViewHolder holder, int position) {
holder.id.setText(list.get(position).getA()+" ");
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView id;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
id = itemView.findViewById(R.id.id1);
}
}
}
item1布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:id="@+id/ll"
android:layout_height="wrap_content">
<TextView
android:layout_marginStart="50dp"
android:textColor="@color/black"
android:id="@+id/id1"
android:text="aa"
android:layout_width="50dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp" />
</LinearLayout>
Bean 实体类
public class Bean {
private int a;
public Bean(int i) {
a=i;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
- 以上就是整个过程,仅供参考,有不懂的欢迎留言