1. 程式人生 > >Android ListView多佈局 , 三個不同佈局的listView展示在一個頁面 ,非常獨特的一個列表展示

Android ListView多佈局 , 三個不同佈局的listView展示在一個頁面 ,非常獨特的一個列表展示

使用listview多佈局會出現一點問題:

由於多個item佈局給單一的item佈局是不一樣的,使用起來,contentview的複用會出現問題。

避免出現問題的有這幾個方法:

1.重寫 getViewTypeCount() – 該方法返回多少個不同的佈局

2.重寫 getItemViewType(int) – 根據position返回相應的Item

3.根據view item的型別,在getView中建立正確的convertView

MainActivity

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
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; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; MyAdapter listAdapter; ArrayList<String> listString; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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); } public 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; } //建立正確的contentview複用 @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.mipmap.ic_launcher); break; } return convertView; } } //各個佈局的控制元件資源 class viewHolder1 { CheckBox checkBox; TextView textView; } class viewHolder2 { TextView textView; } class viewHolder3 { ImageView imageView; TextView textView; } }
下面是四個xml佈局:

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.example.listview_more.MainActivity">
    <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>

listitem1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
    <TextViewandroid:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hahaahha"
/>
    <CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

listitem2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <TextViewandroid:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1111111"
/>
</LinearLayout>
listitme3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
    <TextViewandroid:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="888888888"
/>
    <ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>