如题,本文主要介绍RecyclerView的基本使用方法,像ListView一样展示多种类型的Item数据。
首先介绍一下实体类:ItemPO,用来表示每个Item代表的数据类型:
package com.example.cxc.fullscreendemo.decoration.po;
import java.io.Serializable;
public class ItemPO implements Serializable {
private static final long serialVersionUID = 7879752396036182745L;
private String name;
private int age;
public ItemPO(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
其次,介绍一下测试的Activity页面,其布局activity_recycler_view_test.xml及RecyclerViewTestActivity如下:
package com.example.cxc.fullscreendemo.decoration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.cxc.fullscreendemo.R;
import com.example.cxc.fullscreendemo.decoration.adapter.RecyclerViewTestAdapter;
import com.example.cxc.fullscreendemo.decoration.po.ItemPO;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewTestActivity extends AppCompatActivity {
private static final String TAG = "RecyclerViewTestActivity";
private RecyclerView mRecyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局
setContentView(R.layout.activity_recycler_view_test);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//设置LayoutManager
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
//设置Adapter
RecyclerViewTestAdapter adapter = new RecyclerViewTestAdapter();
mRecyclerView.setAdapter(adapter);
//设置数据
List items = fakeItems();
adapter.notifyDataSetChanged(items);
}
private static final int ITEM_COUNT = 100;
private static List fakeItems() {
List items = new ArrayList<>(ITEM_COUNT);
for (int i = 0; i < ITEM_COUNT; i++) {
ItemPO tItem = new ItemPO("Name_" + i, i);
items.add(tItem);
}
return items;
}
}
布局及Activity中的逻辑都很简单明了,构造了100个用户信息Item用来展示。为了UI界面的丰富性,年龄为偶数的Item,名字和年龄分两行展示,年龄为奇数的Item名字和年龄在一行展示。如何构造ItemView并展示数据,需要使用Adapter来完成,即RecyclerViewTestAdapter。
package com.example.cxc.fullscreendemo.decoration.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cxc.fullscreendemo.R;
import com.example.cxc.fullscreendemo.decoration.po.ItemPO;
import java.util.List;
public class RecyclerViewTestAdapter extends RecyclerView.Adapter {
private static final int TYPE_1 = 1;//横向排列名字和年龄
private static final int TYPE_2 = 2;//竖向排列名字和年龄
private List items;
@Override
public int getItemViewType(int position) {
ItemPO item = getItem(position);
//age是奇数时使用TYPE_1,偶数时使用TYPE_2;
return item != null && item.getAge() % 2 == 0 ? TYPE_2 : TYPE_1;
}
@Override
public TestBaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TestBaseViewHolder holder = null;
switch (viewType) {
case TYPE_1:
View itemView1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_1_layout, parent, false);
holder = new TestViewHolder(itemView1);
break;
case TYPE_2:
View itemView2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_2_layout, parent, false);
holder = new TestViewHolder(itemView2);
break;
}
return holder;
}
@Override
public void onBindViewHolder(TestBaseViewHolder holder, int position) {
ItemPO item = getItem(position);
holder.fillData(item);
}
@Override
public int getItemCount() {
return items == null ? 0 : items.size();
}
public ItemPO getItem(int position) {
return position >= 0 && position < getItemCount() ? items.get(position) : null;
}
public void notifyDataSetChanged(List items) {
this.items = items;
notifyDataSetChanged();
}
}
其中:
public int getItemViewType(int position) :返回position位置的Item的ViewType,以便ItemView回收利用;
public int getItemCount():DataSet中数据Item的个数;
public TestBaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType):根据viewType返回相应的ViewHolder,也就是创建不同UI的ItemView;
public void onBindViewHolder(TestBaseViewHolder holder, int position):将DataSet中position位置的数据填充至该ViewHolder中展示;
运行结果如下图如示,其中红色/蓝色各代表一个用户信息,红色:名字和年龄各占一行展示;蓝色:名字和年龄共占一行展示;
可以根据getItemViewType()返回值在onCreateViewHolder()中创建各种各样的Ui样式,然后在onBindViewHolder()中将数据填充展示。
完整示例请参考:
GitHub:https://github.com/cxcbupt/FullscreenDemo