您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Android中ListView存在多個item樣式怎么處理”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Android中ListView存在多個item樣式怎么處理”這篇文章吧。
在項目開發的時候,相信大家可能會遇到一個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 List<String> list = new ArrayList<String>(); 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<String> 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樣式怎么處理”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。