Android ListView實現任意layout

jopen 10年前發布 | 17K 次閱讀 ListView Android開發 移動開發

SimpleAdapter adapter = new SimpleAdapter(this, getData(),
                R.layout.timenotes_item_left, new String[] { "text1", "text2",
                        "text3" }, new int[] { R.id.textView2, R.id.textView3,
                        R.id.textView4 }) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Map itemData = (Map) getItem(position);
                int type = Integer.parseInt(itemData.get("type").toString());
                View itemView = null;
                switch (type) {
                case 0:
                    Log.d("adapter", "type 0");
                    itemView = View.inflate(getApplicationContext(),
                            R.layout.timenotes_item_left, null);
                    break;
                case 1:
                    Log.d("adapter", "type 1");
                    itemView = View.inflate(getApplicationContext(),
                            R.layout.timenotes_item_right, null);
                    break;
                default:
                    Log.d("adapter", "type default");
                    break;
                }
                return itemView;
            }
        };
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!