RecyclerView使用解析(一)——替代ListView
RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。其可以实现数据纵向滚动,也可以实现横向滚动(ListView做不到横向滚动)。接下来讲解RecyclerView的用法。
因为RecyclerView
属于新增的控件,Android将RecyclerView定义在support库里。若要使用RecyclerView,第一步是要在app的build.gradle里添加:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
修改 activity_main.xml
<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>
Item布局:
<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/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
</LinearLayout>
定义数据类:
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准备一个适配器,新建FruitAdapter 类,让这个适配器继承RecyclerView.Adapter ,并将泛型指定为FruitAdapter.ViewHolder 。其中,ViewHolder 是我们在FruitAdapter 中定义的一个内部类,代码如下所示:
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> fruitList) {
mFruitList = fruitList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fruititem, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount() {
return mFruitList.size();
}
@Override
public long getItemId(int position)
{ return position;}
}
onCreateViewHolder() 方法是用于创建ViewHolder 实例的,在这个方法中将fruit_item 布局加载进来,然后创建一个ViewHolder 实例,并把加载出来的布局传入到构造函数当中,最后将ViewHolder 的实例返回。onBindViewHolder() 方法是用于对RecyclerView子项的数据进行赋值的,会在每个子项被滚动到屏幕内的时候执行,通过position 参数得到当前项的Fruit 实例。getItemCount() 方法用于告诉RecyclerView一共有多少子项。
使用RecyclerView了,修改MainActivity中的代码:
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);
// layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
adapter.setHasStableIds(true);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit("Apple", R.mipmap.ic_launcher);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.mipmap.ic_launcher);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.mipmap.ic_launcher);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.mipmap.ic_launcher);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.mipmap.ic_launcher);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.mipmap.ic_launcher);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.mipmap.ic_launcher);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.mipmap.ic_launcher);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.mipmap.ic_launcher);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.mipmap.ic_launcher);
fruitList.add(mango);
}
}
运行一下程序了,效果如下所示:
完整代码下载demo:https://download.csdn.net/download/nmgsyps2017/20224770?spm=1001.2014.3001.5503