編輯:關於Android編程
Android Listview中顯示不同的視圖布局
1. 使用場景
在重寫ListView的BaseAdapter時,我們常常在getView()方法中復用convertView,以提高性能。convertView在Item為單一的同種類型布局時,能夠回收並重用,但是多個Item布局類型不同時,convertView的回收和重用會出現問題。比如有些行為純文本,有些行則是圖文混排,這裡純文本行為一類布局,圖文混排的行為第二類布局。單一類型的ListView很簡單,下面著重介紹一下ListView包含多種類型視圖布局的情形。
2.ListView包含不同Item的布局
我們需要做這些工作:
1)重寫 getViewTypeCount() – 該方法返回多少個不同的布局
2)重寫 getItemViewType(int) – 根據position返回相應的Item
3)根據view item的類型,在getView中創建正確的convertView
3.案例
import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.os.Bundle; 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.ListView; import android.widget.TextView; public class listViewTest extends Activity { /** Called when the activity is first created. */ ListView listView; MyAdapter listAdapter; ArrayList<String> listString; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)this.findViewById(R.id.listview); listString = new ArrayList<String>(); for(int i = 0 ; i < 100 ; i++) { listString.add(Integer.toString(i)); } listAdapter = new MyAdapter(this); listView.setAdapter(listAdapter); } class MyAdapter extends BaseAdapter{ Context mContext; LinearLayout linearLayout = null; LayoutInflater inflater; TextView tex; final int VIEW_TYPE = 3; final int TYPE_1 = 0; final int TYPE_2 = 1; final int TYPE_3 = 2; public MyAdapter(Context context) { // TODO Auto-generated constructor stub mContext = context; inflater = LayoutInflater.from(mContext); } @Override public int getCount() { // TODO Auto-generated method stub return listString.size(); } //每個convert view都會調用此方法,獲得當前所需要的view樣式 @Override public int getItemViewType(int position) { // TODO Auto-generated method stub int p = position%6; if(p == 0) return TYPE_1; else if(p < 3) return TYPE_2; else if(p < 6) return TYPE_3; else return TYPE_1; } @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 3; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return listString.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub viewHolder1 holder1 = null; viewHolder2 holder2 = null; viewHolder3 holder3 = null; int type = getItemViewType(position); //無convertView,需要new出各個控件 if(convertView == null) { Log.e("convertView = ", " NULL"); //按當前所需的樣式,確定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 = ", "NULL TYPE_1"); 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 = ", "NULL TYPE_2"); 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.imageView = (ImageView)convertView.findViewById(R.id.imageview); Log.e("convertView = ", "NULL TYPE_3"); convertView.setTag(holder3); break; } }else{ //有convertView,按樣式,取得不用的布局 switch(type) { case TYPE_1: holder1 = (viewHolder1) convertView.getTag(); Log.e("convertView !!!!!!= ", "NULL TYPE_1"); break; case TYPE_2: holder2 = (viewHolder2) convertView.getTag(); Log.e("convertView !!!!!!= ", "NULL TYPE_2"); break; case TYPE_3: holder3 = (viewHolder3) convertView.getTag(); Log.e("convertView !!!!!!= ", "NULL TYPE_3"); 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.imageView.setBackgroundResource(R.drawable.icon); break; } return convertView; } } //各個布局的控件資源 class viewHolder1{ CheckBox checkBox; TextView textView; } class viewHolder2{ TextView textView; } class viewHolder3{ ImageView imageView; TextView textView; } }
感謝閱讀,希望能幫助到大家, 謝謝大家對本站的支持!
1、前言jni是java調用原生語言來進行開發的一座橋梁,原生語言一般是指c,c++語言,即jni機制可以讓java語言調用c,c++語言,也可以讓c,c++語言調用ja
1 創建Android工程 首先建立一個名為AndroidJniTest的Android工程,包名默認為com.example.androidjnitest,src目錄
原生應用不僅可以跳轉到RN頁面,也可以吧RN的組件放到原生應用中,作為原生應用的一部分。首先介紹如何把react native嵌入到android中,然後再介紹如何把RN
在android中提供了一種異步回調機制Handler,使用它,我們可以在完成一個很長時間的任務後做出相應的通知 handler基本使用: 在主線