values 下dimens.xmljava
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
佈局:android
activity_main.xmlapi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:scrollbars="none" > </ListView> <ListView android:id="@+id/listView2" android:layout_width="15dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:cacheColorHint="#00000000" android:divider="@null" android:scrollbars="none" > </ListView> </RelativeLayout>
letter_list.xml緩存
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/letterListTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="#3aa2cf" /> </RelativeLayout>
letter.xmlapp
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" android:background="#eeeeee" android:gravity="center_vertical" > <TextView android:id="@+id/letterTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:text="TextView" /> </LinearLayout> </RelativeLayout>
city.xmlide
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" android:background="#ffffff" android:gravity="center_vertical" > <TextView android:id="@+id/cityTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="TextView" /> </LinearLayout> </RelativeLayout>
letter_city.xml函數
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" android:gravity="center_vertical" > <TextView android:id="@+id/letterTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="TextView" /> </LinearLayout> </RelativeLayout>
主頁佈局
MainActivity.javaui
package com.example.zimusortlistview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/** * @author 彭嚮明 * 在listview中加載多個佈局 * 以城市列表爲例,保證cityLetter和cityName中沒有相同的元素,且letter中沒有相同的元素 */ public class MainActivity extends Activity implements OnItemClickListener{ String[] letter={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; String[] cityLetter={"C","B","H","G","W","C","S","S","C","W","H","Z","C","D","N","L","Q","S","S" ,"L","N","H","X","H","S","Z","G","N","J","T","S","X","A"}; String[] cityName={"長沙","北京","杭州","廣州","武漢","重慶","上海","深圳","長春","烏魯木齊","哈爾濱","鄭州","成都","大連","南昌","蘭州","齊齊哈爾","汕頭","蘇州" ,"拉薩","南京","呼和浩特","廈門","合肥","瀋陽","張家界","貴州","寧夏","濟南","天津","石家莊","西安","澳門"}; List<String> letterToCity=new ArrayList<String>(); int count; ListView lv; ListView lv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str=""; for(int i=0;i<letter.length;i++){ str=letter[i]; boolean isAddLetter=false; for(int j=0;j<cityLetter.length;j++){ if(str.equals(cityLetter[j])){ if(!isAddLetter){ letterToCity.add(str); isAddLetter=true; } letterToCity.add(cityName[j]); } } } lv=(ListView)findViewById(R.id.listView1); lv.setAdapter(new MyAdapter()); lv.setOnItemClickListener(this); lv1=(ListView)findViewById(R.id.listView2); lv1.setAdapter(new MyAdapter1()); lv1.setOnItemClickListener(this); } class MyAdapter1 extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return letter.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return letter[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter_list, null); TextView tv=(TextView)view.findViewById(R.id.letterListTextView); tv.setText(letter[position]); return view; } } class MyAdapter extends BaseAdapter{ final static int TYPE_1=1; final static int TYPE_2=2; @Override public int getCount() { // TODO Auto-generated method stub return letterToCity.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return letterToCity.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public int getItemViewType(int position) { for(int i=0;i<letter.length;i++){ if(letterToCity.get(position).equals(letter[i])){ return TYPE_1; } } return TYPE_2; } @Override public int getViewTypeCount() { return 3; } @Override public View getView(int position, View convertView, ViewGroup parent) { /** * 即便convertView緩存了一些佈局,可是在重構時,根本不知道怎麼樣去讓convertView返回你所須要的佈局,這時你需 * 要讓adapter知道我當前有哪些佈局,我重構Item時的佈局選取規則,好讓convertView能返回你須要的佈局。 * 須要重寫如下兩個函數 * @Override * public int getItemViewType(int position) {}這個函數獲取在getView中建立的視圖的類型 * @Override * public int getViewTypeCount() {}返回在getView中建立視圖類型的數量 * 至於這兩個方法的詳細用處,本身看api便可 */ ViewHolder1 vh1=null; ViewHolder2 vh2=null; int type=getItemViewType(position); if(convertView==null){ switch (type) { case TYPE_1: convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter, null); vh1=new ViewHolder1(); vh1.tv=(TextView)convertView.findViewById(R.id.letterTextView); convertView.setTag(vh1); break; case TYPE_2: convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.city, null); vh2=new ViewHolder2(); vh2.tv=(TextView)convertView.findViewById(R.id.cityTextView); convertView.setTag(vh2); break; default: break; } }else{ switch (type) { case TYPE_1: vh1=(ViewHolder1)convertView.getTag(); break; case TYPE_2: vh2=(ViewHolder2)convertView.getTag(); break; default: break; } } switch (type) { case TYPE_1: vh1.tv.setText(letterToCity.get(position)); break; case TYPE_2: vh2.tv.setText(letterToCity.get(position)); break; default: break; } return convertView; //此方法不可取,刷新時view會刷新 // boolean isLetter=false; // for(int i=0;i<letter.length;i++){ // if(letterToCity.get(position).equals(letter[i])){ // isLetter=true; // break; // } // } // ViewHolder vh=null; // if(convertView==null){ // vh=new ViewHolder(); // if(isLetter){ // convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter, null); // vh.tv=(TextView)convertView.findViewById(R.id.letterTextView); // }else{ // convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.city, null); // vh.tv=(TextView)convertView.findViewById(R.id.cityTextView); // } // convertView.setTag(vh); // }else{ // vh = (ViewHolder) convertView.getTag(); // } // vh.tv.setText(letterToCity.get(position)); // return convertView; //城市列表,修改佈局 // boolean isLetter=false; // for(int i=0;i<letter.length;i++){ // if(letterToCity.get(position).equals(letter[i])){ // isLetter=true; // break; // } // } // View view; // ViewHolder vh; // if(convertView==null){ // view=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter_city, null); // vh=new ViewHolder(); // vh.tv=(TextView)view.findViewById(R.id.letterTextView); // vh.ll=(LinearLayout)view.findViewById(R.id.ll); // convertView=view; // convertView.setTag(vh); // }else{ // view=(View)convertView; // vh=(ViewHolder)convertView.getTag(); // } // vh.tv.setText(letterToCity.get(position)); // if(isLetter){ // vh.ll.setBackgroundColor(Color.LTGRAY); // vh.tv.setPadding(5, 5, vh.tv.getPaddingRight(), 5); // }else{ // vh.ll.setBackgroundColor(Color.WHITE); // vh.tv.setPadding(10, 10, vh.tv.getPaddingRight(), 10); // } // return view; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (parent.getId()) { case R.id.listView1: boolean isLetter=false; for(int i=0;i<letter.length;i++){ if(letter[i].equals(letterToCity.get(position))){ isLetter=true; break; } } if(!isLetter){ Toast.makeText(this, letterToCity.get(position), Toast.LENGTH_SHORT).show(); } break; case R.id.listView2: for(int i=0;i<letterToCity.size();i++){ if(letter[position].equals(letterToCity.get(i))){ lv.setSelection(i); break; } } break; default: break; } } } class ViewHolder{ TextView tv; LinearLayout ll; } class ViewHolder1{ TextView tv; } class ViewHolder2{ TextView tv; }