RecyclerView
超级ListView,需要support:recyclerview
1、在build.gradle中导入support包
compile 'com.android.support:recyclerview-v7:25.0.0'
2、在xml中布局
<android.support.v7.widget.RecyclerView
android:id="@+id/m_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3、java中定义适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ItemHolder> {
private List<String> list;
private LayoutInflater mInflater;
public MyAdapter(Context context, List<String> list) {
this.list = list;
mInflater = LayoutInflater.from(context);
}
//对数据显示需要创建的视图
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layout = mInflater.inflate(R.layout.item_layout, null);
return new ItemHolder(layout);
}
//数据和视图绑定(处理显示)
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
String s = list.get(position);
holder.tv.setText(s);
}
//配置显示的总个数
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
//自定义一个Holder类继承RecyclerView.ViewHolder,管理每个item显示的视图
class ItemHolder extends RecyclerView.ViewHolder {
TextView tv;
public ItemHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.item_name);
}
}
}
4、配置显示管理以及适配器
RecyclerView mRecycler = (RecyclerView) findViewById(R.id.m_recycler);
//RecyclerView有3种显示方式(列表、九宫格、瀑布流)每一种都有一个显示管理器
//显示列表
mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setAdapter(new MyAdapter(this,list));
设置九宫格显示
mRecycler.setLayoutManager(new GridLayoutManager(this,3)); //第二个参数表示列数
item之间的间距
//添加分割线(item之间的间距)
mRecycler.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));
添加自定义分割线
/**
* 定义item之间的间距
*/
public class MyItemDecoration extends RecyclerView.ItemDecoration {
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
//获取item的位置
int childPos = parent.getChildLayoutPosition(view);
if (childPos > 0 && childPos %2==0) {
outRect.top = 20;
}
}
}
添加边距
mRecycler.addItemDecoration(new MyItemDecoration());
关于Item的点击监听,需要在适配器中初始化item布局时设置,然后在适配器中自定义回调接口将监听传回Activity或者Fragment中即可
//自定义一个Holder类继承RecyclerView.ViewHolder,管理每个item显示的视图
class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tv;
public ItemHolder(View itemView) {
super(itemView);
//对Item设置点击监听
itemView.setOnClickListener(this);
tv = (TextView) itemView.findViewById(R.id.item_name);
}
@Override
public void onClick(View v) {
//获得当前显示的item的位置
int position = getLayoutPosition();
onItemClickListener.onClick(position);
}
}
//将item的点击转出去的接口
public interface OnItemClickListener{
void onClick(int position);
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
瀑布流
mRecycler.setLayoutManager(new StaggeredGridLayoutManager
(2,StaggeredGridLayoutManager.VERTICAL));
第一个参数表示列数(或者行数),第二个参数表示显示的方向,VERTICAL表示垂直
适配器刷新
移除刷新
adapter.notifyItemRemoved(position);
添加刷新
adapter.notifyItemInserted(position);
移动刷新
adapter.notifyItemMoved(from,to);
当前屏幕上所有内容的刷新
adapter.notifyDataSetChanged();