Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 在同一個 Activity 中放多個 ListView 的做法

編輯:關於 Android 程式設計

「一個 Activity 裡能不能放兩個以上的 ListView?」答案是能,但前提是你不能用 ListActivity。原因很單純:ListView 的 id 一般寫成 android:id="@id/android:list",這是 ListActivity 約定俗成的專用 id,它會自動去畫面上找這個 id 並綁定,因此只能有一個。想放多個,就得改用普通的 Activity,並自己替每個 ListView 命名,例如 android:id="@+id/mylist",之後像操作一般控制元件那樣各自初始化即可。

Adapter 方面,做法可以很直接:繼承 BaseAdapter,為每個 ListView 各建一個 Adapter 實例,分別 setAdapter() 就完成了。

參考程式碼

public class myActivity extends Activity implements OnClickListener {

    private ListView listview_psychologicalstate, listview_weatherstate;
    private ListAdapter listadapter_psychologicalstate, listadapter_weatherstate;
    private ArrayList<String> psychologicalstatearray = new ArrayList<String>(),
            weatherstatearray = new ArrayList<String>();
    private String[] strpsychologicalstate =
            new String[]{"郁悶", "亢奮", "壓力", "失眠", "嗜睡", "易驚恐"};
    private String[] strweatherstate = new String[]{"濕熱", "燥熱", "寒冷"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personalizedorderview);
        findView();
    }

    public void findView() {
        listview_psychologicalstate = (ListView) findViewById(R.id.psychologicalstatelistid);
        listview_weatherstate = (ListView) findViewById(R.id.weatherstatelistid);

        for (int i = 0; i < strpsychologicalstate.length; i++) {
            psychologicalstatearray.add(strpsychologicalstate[i]);
        }
        for (int i = 0; i < strweatherstate.length; i++) {
            weatherstatearray.add(strweatherstate[i]);
        }

        listadapter_psychologicalstate =
                new GetinflamedAdapter(this, psychologicalstatearray);
        listview_psychologicalstate.setAdapter(listadapter_psychologicalstate);

        listadapter_weatherstate = new GetinflamedAdapter(this, weatherstatearray);
        listview_weatherstate.setAdapter(listadapter_weatherstate);
    }

    public class GetinflamedAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater mInflater;
        private ArrayList<String> arraylist;

        public GetinflamedAdapter(Context context, ArrayList<String> arraylist) {
            this.context = context;
            this.arraylist = arraylist;
            mInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return arraylist.size();
        }

        @Override
        public Object getItem(int position) {
            return arraylist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(R.layout.inflamedchild, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.label = (TextView) convertView.findViewById(R.id.bodystateid1);
            viewHolder.label.setText(arraylist.get(position).toString());
            viewHolder.label.setTextColor(Color.GREEN);
            viewHolder.label.setTextSize(22);
            convertView.setTag(viewHolder);
            return convertView;
        }

        private class ViewHolder {
            TextView label;
        }
    }
}

版面檔的注意事項

在 XML 中把兩個 ListView 直向排列時,記得給它們 android:layout_weight,或者在外層包一個 LinearLayout 並指定高度,否則兩個 fill_parent 會互相搶空間,導致其中一個被擠不見。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/psychologicalstatelistid"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <ListView
        android:id="@+id/weatherstatelistid"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

兩個容易踩到的問題

捲動會互相搶手勢。 兩個 ListView 上下並排時,手指在畫面上滑動,系統難以判斷該給誰處理,會出現「想滾上面卻滾了下面」的怪異手感。若兩段內容都不長,改用 ScrollView 搭配非捲動的版面(例如 LinearLayout 動態產生子 View)會順得多;若內容確實很長,考慮用 ViewPager 加分頁,或合併成單一 ListView 並以不同樣式的列來呈現兩個分區。

getView 沒有複用 convertView。 上面範例每次都重新 inflate,雖然示範用的資料量很小看不出差別,實務上一定要加上 null 判斷來複用,否則資料一多就會明顯掉幀。

總結:多個 ListView 在技術上完全可行,關鍵是別用 ListActivity、給每個 ListView 獨立 id 與獨立 Adapter;但要不要這麼做,則該從使用者操作順手的角度重新想一遍。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved