ListView:如果不适用一些技巧来提升它的效率,那么ListView的性能就会非常差。ListView的扩展性也不够好,他只能实现数据纵向滚动的效果。
RecyclerView不仅可以轻松实现ListView同样的效果,还优化了ListView中存在的各种不足。Android官方更推荐使用RecyclerView。
RecyclerView的基本用法
在dependencies闭包中添加,可以直接在Project Structure中搜索再添加
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha06'
修改布局文件的内容,其中宽度和高度都设置为match_parent,这样RecyclerView就会沾满整个布局的控件。
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
接着,需要准备水果的图片放在drawable中,Fruit类和fruit_item.xml
新建FruitAdapter类继承RecyclerView.Adapter,并将泛型指定为FruitAdapter.ViewHolder。
定义一个内部类ViewHolder,ViewHolder要继承自RecyclerView.ViewHolder。
ViewHolder构造函数中的itemView参数,通常指RecyclerView子项的最外层布局。
FruitAdapter的构造函数用于把展示的数据源传进来。
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
在该方法中将fruit_item布局加载进来,然后创建一个ViewHolder实例,并把加载出来的布局传入到构造函数中,最后将ViewHolder实例返回。
public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position)
该方法在每个子项滚动到屏幕内的时候执行,通过position获取当前项的Fruit实例,然后再将数据设置到holder的ImageView和TextView。
public int getItemCount()
告诉RecyclerView一共有多少个子项。
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View itemView) {
super(itemView);
fruitImage = (ImageView)itemView.findViewById(R.id.fruit_image);
fruitName = (TextView)itemView.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> mFruitList) {
this.mFruitList = mFruitList;
}
//用来创建ViewHolder实例,并把加载出来的布局传入构造函数中,最后将ViewHolder的实例返回
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
//用于对RecycleView子项的数据进行赋值的,会在每个子项滚动到屏幕内的时候执行
@Override
public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
//告诉RecyclerView一共有多少个子项,直接返回数据源的长度
@Override
public int getItemCount() {
return mFruitList.size();
}
}
MainActivity.java
首先获取RecyclerView的实例,然后创建LinearLayoutManager对象,并将它设置到RecyclerView当中。LayoutManager用于指定RecyclerView的布局方式,这里是线性布局。之后创建FruitAdapter实例,并将水果数据传入FruitAdapter的构造函数中,最后使用RecyclerView的setAdapter()方法来完成适配器设置。
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 layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 2; 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 orange = new Fruit("Orange", R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
fruitList.add(pear);
Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("cherry", R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
fruitList.add(mango);
}
}
}