RecyclerView是ListView的升级版,它具备了更好的性能,且更容易使用。和ListView一样,RecyclerView是用来显示大量数据的容器,并通过复用有限数量的View,来提高滚动时的性能。当你的视图上的元素经常动态的且有规律的改变时候,可以使用RecyclerView控件。
与ListView不同的是RecyclerView现在不再负责布局,只专注于复用机制,布局交由LayoutManager来管理。
RecyclerView仍然通过Adapter来获取需要显示的对象。
1 布局
SwipeRefreshLayout 作下拉刷新用
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swip_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
2 代码
1 创建DataBean
public class DataBean {
public int iconRes;
public String des;
public DataBean(int iconRes, String des) {
this.iconRes = iconRes;
this.des = des;
}
}
2 创建Adapter与Viewhodler
/**
* Created by cynic on 2016/5/27.
*/
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> {
private Context context;
private List<DataBean> datas;
public ListAdapter(Context context, List<DataBean> datas) {
this.context = context;
this.datas = datas;
}
@Override
public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.item_list, null);
return new ListHolder(view);
}
@Override
public void onBindViewHolder(ListHolder holder, int position) {
holder.refreshDataUI(datas.get(position));//数据显示
}
@Override
public int getItemCount() {
if (datas != null) {
return datas.size();
}
return 0;
}
public class ListHolder extends RecyclerView.ViewHolder {
private ImageView iv;
private TextView tv;
public ListHolder(View itemView) {
super(itemView);
iv = (ImageView) itemView.findViewById(R.id.item_list_iv);
tv = (TextView) itemView.findViewById(R.id.item_list_tv);
}
public void refreshDataUI(DataBean dataBean) {
iv.setImageResource(dataBean.iconRes);
tv.setText(dataBean.des);
}
}
}
3 给recycleview设置adapter和layoutmanager adapter
//listview 垂直显示
private void initRecycleListV() {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getBaseContext(),
LinearLayoutManager.VERTICAL, false);
mRecycleView.setLayoutManager(layoutManager);
ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
mRecycleView.setAdapter(adapter);
}
//listview 水平显示
private void initRecycleListH() {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getBaseContext(),
LinearLayoutManager.HORIZONTAL, false);
mRecycleView.setLayoutManager(layoutManager);
ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
mRecycleView.setAdapter(adapter);
}
//gridview 垂直显示
private void initRecycleGridV() {
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getBaseContext(), 2);
mRecycleView.setLayoutManager(layoutManager);
ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
mRecycleView.setAdapter(adapter);
}
//gridview 水平显示
private void initRecycleGridH() {
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getBaseContext(), 2,
LinearLayoutManager.HORIZONTAL, false);
mRecycleView.setLayoutManager(layoutManager);
ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
mRecycleView.setAdapter(adapter);
}
5 瀑布流
1 item布局
CardView 包裹的内容以卡片显示,字布局为LinearLayout包裹时,设置layout_width="match_parent"
不起作用,不知为何。其Adapter、ViewHolder与上一个相似,这里不在记录。
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="@+id/item_straggered_iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/item_straggered_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_straggered_iv"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="gagaga"
android:textColor="#212121"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
代码设置
//瀑布流 垂直显示
private void initRecycleStraggeredV() {
RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL);
mRecycleView.setLayoutManager(layoutManager);
StraggeredAdapter adapter = new StraggeredAdapter(getBaseContext(), mStraggeredDatas);
mRecycleView.setAdapter(adapter);
}
//瀑布流 水平显示
private void initRecycleStraggeredH() {
RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2,LinearLayoutManager.HORIZONTAL);
mRecycleView.setLayoutManager(layoutManager);
StraggeredAdapter adapter = new StraggeredAdapter(getBaseContext(), mStraggeredDatas);
mRecycleView.setAdapter(adapter);
}
//设置下拉刷新
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Thread(){
@Override
public void run() {
SystemClock.sleep(2000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
mRecycleView.getAdapter().notifyDataSetChanged();
}
});
}
}.start();
}
});