大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要为大家展示了“Android中ListView存在多个item样式怎么处理”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android中ListView存在多个item样式怎么处理”这篇文章吧。
创新互联公司自2013年起,是专业互联网技术服务公司,拥有项目成都网站制作、成都做网站、外贸营销网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元石城做网站,已为上家服务,为石城各地企业和个人服务,联系电话:18982081108
在项目开发的时候,相信大家可能会遇到一个ListView中出现多个不同的布局,遇到这个问题我的大致思路就是创建多个viewholder,在getViewType的时候设置不同位置的item用不同的viewholder,代码:
package com.sunny.youdao; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private Context mContext; private LinearLayout linearLayout = null; private LayoutInflater inflater; private Listlist = new ArrayList (); private TextView tex; private final int VIEW_TYPE = 3; private final int TYPE_1 = 0; private final int TYPE_2 = 1; private final int TYPE_3 = 2; public MyAdapter(Context context, List list) { // TODO Auto-generated constructor stub this.mContext = context; this.list = list; inflater = LayoutInflater.from(mContext); } @Override public int getCount() { // TODO 自动生成的方法存根 return list.size(); } @Override public Object getItem(int position) { // TODO 自动生成的方法存根 return list.get(position); } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return position; } //每个convert view都会调用此方法,获得当前所需要的view样式 @Override public int getItemViewType(int position) { // TODO Auto-generated method stub int viewtype = position%6; if(viewtype == 0) return TYPE_1; else if(viewtype < 3) return TYPE_2; else if(viewtype < 6) return TYPE_3; else return TYPE_1; } //返回样式的数量 @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 3; } @Override public View getView(int position, View convertView, ViewGroup parent) { viewHolder1 holder1 = null; viewHolder2 holder2 = null; viewHolder3 holder3 = null; int type = getItemViewType(position); // 无convertView,需要new出各个控件 if (convertView == null) { Log.e("convertView = ", "###convertView为空###"); // 按当前所需的样式,确定new的布局 switch (type) { case TYPE_1: convertView = inflater.inflate(R.layout.listitem1, parent,false); holder1 = new viewHolder1(); holder1.textView = (TextView) convertView.findViewById(R.id.textview1); holder1.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox); Log.e("convertView = ", "布局样式一"); convertView.setTag(holder1); break; case TYPE_2: convertView = inflater.inflate(R.layout.listitem2, parent,false); holder2 = new viewHolder2(); holder2.textView = (TextView) convertView.findViewById(R.id.textview2); Log.e("convertView = ", "布局样式二"); convertView.setTag(holder2); break; case TYPE_3: convertView = inflater.inflate(R.layout.listitem3, parent,false); holder3 = new viewHolder3(); holder3.textView = (TextView) convertView.findViewById(R.id.textview3); holder3.p_w_picpathView = (ImageView) convertView.findViewById(R.id.p_w_picpathview); Log.e("convertView = ", "布局样式三"); convertView.setTag(holder3); break; } } else { // 有convertView,按样式,取得不用的布局 switch (type) { case TYPE_1: holder1 = (viewHolder1) convertView.getTag(); Log.e("convertView= ", "布局样式一"); break; case TYPE_2: holder2 = (viewHolder2) convertView.getTag(); Log.e("convertView= ", "布局样式二"); break; case TYPE_3: holder3 = (viewHolder3) convertView.getTag(); Log.e("convertView= ", "布局样式三"); break; } } // 设置资源 switch (type) { case TYPE_1: holder1.textView.setText(Integer.toString(position)); holder1.checkBox.setChecked(true); break; case TYPE_2: holder2.textView.setText(Integer.toString(position)); break; case TYPE_3: holder3.textView.setText(Integer.toString(position)); holder3.p_w_picpathView.setBackgroundResource(R.drawable.icon); break; } return convertView; } // 各个布局的控件资源 class viewHolder1 { CheckBox checkBox; TextView textView; } class viewHolder2 { TextView textView; } class viewHolder3 { ImageView p_w_picpathView; TextView textView; } }
以上是“Android中ListView存在多个item样式怎么处理”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!