RecyclerView:替换:ListView,GridView,ViewPager,gallery(画廊效果)
* 1.ViewGroup子类,容器
* 2.用有限的window显示大量的数据集合快速滑动,
* 3.多种不同管理器
备注:RecyclerView负责布局的复用,布局类型由布局管理器来指定
基本使用步骤:
1.布局书写RecyclerView控件;
2.java代码查找控件;
3.设置布局管理器setLayoutManager():LinearLayoutManager(listview),GridLayoutManager(gridview),StaggeredGridLayoutManager(瀑布流)
4.设置适配器setAdapter:
5.构建适配器:
1》写一个类继承RecyclerView.Adapter<VH>:VH必须自己定义
2》在该类中写内部类ViewHolder继承RecyclerView.ViewHolder,并重写构造方法,查找findViewById()
3》给步骤1补充ViewHolder内部类的对象
设置分割线
查找分割线类的路径:sdk目录/extra/android/support/sample/src/widget/decorator/DividerItemDecoration.java
DividerItemDecoration.VERTICAL_LIST:横线,DividerItemDecoration.HORIZONTAL_LIST竖线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST));
设置监听事件
Adapter中item的具体控件设置监听事件,利用接口回调,最后在activity中的adapter具体对象实现回调方法
RecyclerView多布局:
加载不同布局,只需额外添加一个方法:getItemViewType(int position)根据指定位置得到view类型 ,
1.执行顺序:getItemCount():先得到集合的数量
2.getItemViewType(int position)根据指定位置得到view类型,再去执行 onCreateViewHolder(ViewGroup parent, int viewType):执行一屏幕item的创建,超过一屏幕复用;
备注:getItemViewType()执行次数:集合的总个数
3.找控件,ViewHolder的创建需要更改默认的构造方法,添加type,根据type决定去查找那个控件,
4.填充数据;onBindViewHolder():调用getItemViewType(pos)得到类型去匹配设置数据
案例:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
RelativeLayout relativeLayout;
public static final int TYPE_HEADER=-1;
public static final int TYPE_LEFT = 0;
public static final int TYPE_RIGHT = 1;
List<Info> infoList;//Info应额外设置type属性
Context context;
LayoutInflater inflater;
View headView=null;
public MyAdapter(List<Info> infoList, Context context) {
this.infoList = infoList;
this.context = context;
inflater = LayoutInflater.from(context);
}
public void addHeaderView(View headViews){
//在activity中adapter调用此方法,传入headViews(头布局)
this.headView =headViews;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("======", "==viewType====" + viewType);
View view = null;
switch (viewType) {
case TYPE_HEADER:
//广告轮播:
view = headView;
break;
case TYPE_LEFT:
view = inflater.inflate(R.layout.item_left, parent,false);
break;
case TYPE_RIGHT:
view = inflater.inflate(R.layout.item_right, parent,false);
break;
}
return new MyViewHolder(view, viewType);
}
//0-9
//0, 广告, 1-10
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
int type = getItemViewType(position);
Log.e("======", "==onBindViewHolder===="+type);
switch (type) {
case TYPE_HEADER:
break;
case TYPE_LEFT:
String str = infoList.get(position-1).getMessage();
holder.tv_left.setText(str);
break;
case TYPE_RIGHT:
String str1 = infoList.get(position-1).getMessage();
holder.tv_right.setText(str1);
break;
}
}
@Override
public int getItemCount() {
return infoList.size()+1;
}
//根据指定位置得到view类型
@Override
public int getItemViewType(int position) {
Log.e("======", "==getItemViewType====");
int type=0;
if (position==0){
type=TYPE_HEADER;
}else{
//position-1:原因是集合数据从0开始取,但是现在0是header
Info info = infoList.get(position-1);
type = info.getType();
}
return type;
}
class MyViewHolder extends RecyclerView.ViewHolder {
int type;
TextView tv_left;
TextView tv_right;
public MyViewHolder(View itemView, int type) {
super(itemView);
this.type = type;
switch (type) {
case TYPE_HEADER:
break;
case TYPE_LEFT:
tv_left = (TextView) itemView.findViewById(R.id.tv_left);
break;
case TYPE_RIGHT:
tv_right = (TextView) itemView.findViewById(R.id.tv_right);
break;
}
}
}
}
ListView多布局:
比原来的多了getItemViewType和getViewTypeCount这两个方法,
1)重写 getViewTypeCount() – 该方法返回多少个不同的布局
2)重写 getItemViewType(int) – 根据position返回相应的Item
* 1.ViewGroup子类,容器
* 2.用有限的window显示大量的数据集合快速滑动,
* 3.多种不同管理器
备注:RecyclerView负责布局的复用,布局类型由布局管理器来指定
基本使用步骤:
1.布局书写RecyclerView控件;
2.java代码查找控件;
3.设置布局管理器setLayoutManager():LinearLayoutManager(listview),GridLayoutManager(gridview),StaggeredGridLayoutManager(瀑布流)
4.设置适配器setAdapter:
5.构建适配器:
1》写一个类继承RecyclerView.Adapter<VH>:VH必须自己定义
2》在该类中写内部类ViewHolder继承RecyclerView.ViewHolder,并重写构造方法,查找findViewById()
3》给步骤1补充ViewHolder内部类的对象
4》重写RecyclerView.Adapter<VH>中的方法:onCreateViewHolder():创建item视图以及ViewHolder
onBindViewHolder():绑定数据到ViewHolder中的视图控件上 getItemCount():
设置分割线
查找分割线类的路径:sdk目录/extra/android/support/sample/src/widget/decorator/DividerItemDecoration.java
DividerItemDecoration.VERTICAL_LIST:横线,DividerItemDecoration.HORIZONTAL_LIST竖线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST));
设置监听事件
Adapter中item的具体控件设置监听事件,利用接口回调,最后在activity中的adapter具体对象实现回调方法
RecyclerView多布局:
加载不同布局,只需额外添加一个方法:getItemViewType(int position)根据指定位置得到view类型 ,
1.执行顺序:getItemCount():先得到集合的数量
2.getItemViewType(int position)根据指定位置得到view类型,再去执行 onCreateViewHolder(ViewGroup parent, int viewType):执行一屏幕item的创建,超过一屏幕复用;
备注:getItemViewType()执行次数:集合的总个数
3.找控件,ViewHolder的创建需要更改默认的构造方法,添加type,根据type决定去查找那个控件,
4.填充数据;onBindViewHolder():调用getItemViewType(pos)得到类型去匹配设置数据
案例:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
RelativeLayout relativeLayout;
public static final int TYPE_HEADER=-1;
public static final int TYPE_LEFT = 0;
public static final int TYPE_RIGHT = 1;
List<Info> infoList;//Info应额外设置type属性
Context context;
LayoutInflater inflater;
View headView=null;
public MyAdapter(List<Info> infoList, Context context) {
this.infoList = infoList;
this.context = context;
inflater = LayoutInflater.from(context);
}
public void addHeaderView(View headViews){
//在activity中adapter调用此方法,传入headViews(头布局)
this.headView =headViews;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("======", "==viewType====" + viewType);
View view = null;
switch (viewType) {
case TYPE_HEADER:
//广告轮播:
view = headView;
break;
case TYPE_LEFT:
view = inflater.inflate(R.layout.item_left, parent,false);
break;
case TYPE_RIGHT:
view = inflater.inflate(R.layout.item_right, parent,false);
break;
}
return new MyViewHolder(view, viewType);
}
//0-9
//0, 广告, 1-10
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
int type = getItemViewType(position);
Log.e("======", "==onBindViewHolder===="+type);
switch (type) {
case TYPE_HEADER:
break;
case TYPE_LEFT:
String str = infoList.get(position-1).getMessage();
holder.tv_left.setText(str);
break;
case TYPE_RIGHT:
String str1 = infoList.get(position-1).getMessage();
holder.tv_right.setText(str1);
break;
}
}
@Override
public int getItemCount() {
return infoList.size()+1;
}
//根据指定位置得到view类型
@Override
public int getItemViewType(int position) {
Log.e("======", "==getItemViewType====");
int type=0;
if (position==0){
type=TYPE_HEADER;
}else{
//position-1:原因是集合数据从0开始取,但是现在0是header
Info info = infoList.get(position-1);
type = info.getType();
}
return type;
}
class MyViewHolder extends RecyclerView.ViewHolder {
int type;
TextView tv_left;
TextView tv_right;
public MyViewHolder(View itemView, int type) {
super(itemView);
this.type = type;
switch (type) {
case TYPE_HEADER:
break;
case TYPE_LEFT:
tv_left = (TextView) itemView.findViewById(R.id.tv_left);
break;
case TYPE_RIGHT:
tv_right = (TextView) itemView.findViewById(R.id.tv_right);
break;
}
}
}
}
ListView多布局:
比原来的多了getItemViewType和getViewTypeCount这两个方法,
1)重写 getViewTypeCount() – 该方法返回多少个不同的布局
2)重写 getItemViewType(int) – 根据position返回相应的Item