每个人每天都会在浏览器上看一些最新的新闻,作为一个IT程序员你们会发现,她们的布局排版都是有区别的,有时候同一个样式的条目有4、5条 界面一样,下面又出现1 条不一样的条目,这样的效果是不是很美观,这在Android称为多布局,下面来简单实现一下,大概理解下
利用Adapter的getItemViewType()方法返回不同的VIEW_TYPE,利用求末取余的算法获取到当前位置的条目,然后在onCreateViewHolder中设置对应的ViewHolder 。
final int VIEW_TYPE = 3;
final int TYPE_1 = 0;
final int TYPE_2 = 1;
final int TYPE_3 = 2;
/**
* 获取每个位置的View
*
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
int p = position % VIEW_TYPE;
if (p == 0) {
return TYPE_1;
} else if (p == 1) {
return TYPE_2;
} else {
return TYPE_3;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_1) {
View view = View.inflate(mContext, R.layout.adapter, null);
return new MyViewHolder(view);
} else if (viewType == TYPE_2) {
View view2 = View.inflate(mContext, R.layout.adapter2, null);
return new MyViewHolder2(view2);
} else {
View view3 = View.inflate(mContext, R.layout.adapter3, null);
return new MyViewHolder3(view3);
}
}
这个方法是给每一个ViewHolder设置数据
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder holder1 = null;
MyViewHolder2 holder2 = null;
MyViewHolder3 holder3 = null;
if (holder instanceof MyViewHolder) {
holder1 = (MyViewHolder) holder;
} else if (holder instanceof MyViewHolder2) {
holder2 = (MyViewHolder2) holder;
} else {
holder3 = (MyViewHolder3) holder;
}
}
完整代码:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context mContext;
List mList;
final int VIEW_TYPE = 3;
final int TYPE_1 = 0;
final int TYPE_2 = 1;
final int TYPE_3 = 2;
public MyAdapter(Context context, List list) {
mContext = context;
mList = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_1) {
View view = View.inflate(mContext, R.layout.adapter, null);
return new MyViewHolder(view);
} else if (viewType == TYPE_2) {
View view2 = View.inflate(mContext, R.layout.adapter2, null);
return new MyViewHolder2(view2);
} else {
View view3 = View.inflate(mContext, R.layout.adapter3, null);
return new MyViewHolder3(view3);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder holder1 = null;
MyViewHolder2 holder2 = null;
MyViewHolder3 holder3 = null;
if (holder instanceof MyViewHolder) {
holder1 = (MyViewHolder) holder;
} else if (holder instanceof MyViewHolder2) {
holder2 = (MyViewHolder2) holder;
} else {
holder3 = (MyViewHolder3) holder;
}
}
/**
* 获取每个位置的View
*
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
int p = position % VIEW_TYPE;
if (p == 0) {
return TYPE_1;
} else if (p == 1) {
return TYPE_2;
} else {
return TYPE_3;
}
}
@Override
public int getItemCount() {
if (mList != null) {
return mList.size();
}
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
class MyViewHolder2 extends RecyclerView.ViewHolder {
public MyViewHolder2(View itemView) {
super(itemView);
}
}
class MyViewHolder3 extends RecyclerView.ViewHolder {
public MyViewHolder3(View itemView) {
super(itemView);
}
}
}
ListView比RecyclerView多了getViewTypeCount() 布局数量 的方法
// 总共有几种布局
@Override
public int getViewTypeCount() {
return VIEW_TYPE;
}
完整代码:
public class MyAdapter2 extends BaseAdapter {
private List list;
private Context ctx;
final int VIEW_TYPE = 3;
final int TYPE_1 = 0;
final int TYPE_2 = 1;
final int TYPE_3 = 2;
public MyAdapter2(List list, Context ctx) {
super();
this.list = list;
this.ctx = ctx;
}
// 获取Item的View的类型
@Override
public int getItemViewType(int position) {
int i = position % VIEW_TYPE;
if (i == 0) {
return TYPE_1;
} else if (i == 1) {
return TYPE_2;
} else {
return TYPE_3;
}
}
// 总共有几种布局
@Override
public int getViewTypeCount() {
return VIEW_TYPE;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder holder1 = null;
MyViewHolder2 holder2 = null;
MyViewHolder3 holder3 = null;
int type = getItemViewType(position);
if (convertView == null) {
//设置布局和找ID
switch (type) {
case TYPE_1:
convertView = View.inflate(ctx, R.layout.adapter, null);
holder1 = new MyViewHolder(convertView);
convertView.setTag(holder1);
break;
case TYPE_2:
convertView = View.inflate(ctx, R.layout.adapter2, null);
holder2 = new MyViewHolder2(convertView);
convertView.setTag(holder2);
break;
case TYPE_3:
convertView = View.inflate(ctx, R.layout.adapter3, null);
holder3 = new MyViewHolder3(convertView);
convertView.setTag(holder3);
break;
}
} else {
//设置复用
switch (type) {
case TYPE_1:
holder1 = (MyViewHolder) convertView.getTag();
break;
case TYPE_2:
holder2 = (MyViewHolder2) convertView.getTag();
break;
case TYPE_3:
holder3 = (MyViewHolder3) convertView.getTag();
break;
}
}
//设置数据
switch (type) {
case TYPE_1:
break;
case TYPE_2:
break;
case TYPE_3:
break;
}
return convertView;
}
private class MyViewHolder {
public MyViewHolder(View convertView) {
}
}
private class MyViewHolder2 {
public MyViewHolder2(View convertView) {
}
}
private class MyViewHolder3 {
public MyViewHolder3(View convertView) {
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView mRecy;
ListView mLv;
List mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
initView();
}
private void initList() {
for (int i = 0; i < 100; i++) {
mList.add(i);
}
}
private void initView() {
mRecy = findViewById(R.id.mRecy);
mLv = findViewById(R.id.mLv);
// mRecy.setLayoutManager(new LinearLayoutManager(this));
// mRecy.setAdapter(new MyAdapter(this, mList));
mLv.setAdapter(new MyAdapter2(mList,this));
}
}
效果图: