進階控制項【安卓5】——滾動視圖、列表視圖[ListView、SimpleAdapter類]

來源:互聯網
上載者:User

標籤:adapter   add   分代   gif   上下文   color   ima   ted   arraylist   

滾動視圖 

              <ScrollView

                          android: layout_width="fill_parent"

                          android: layout_height="fill_parent">

               </ScrollView>

     滾動試圖指的是提供一個專門的容器,可以裝下多於螢幕寬度的組件,而後採用拖拽的方式

顯示所有在ScrollView中的組件

     注意:滾動視圖中只能有一個組件

列表視圖【ListView、SimpleAdapter類】

   1.ListView控制項的特點:每個清單項目獨佔一行,每行的布局都相同。

     2.使用SimpleAdapter類設定列表視圖

        SimpleAdapter類的主要功能是將List集合的資料轉換為ListView可以支援的資料。

      new SimpleAdapter ( 內容物件this, 列表的資料, 清單項目的布局資源檔,

                            鍵名數組【new String[ ]{"標籤名"}】,清單項目所有控制項的索引值【new int[ ]{R.id.控制項id】)

方法

描述

public SimpleAdapter ( Context context,

List<? extends Map<String,?>>,

int resource, String[] from, int[] to)

建立SimpleAdapter對象,需要傳入Context對象,封裝的List集合,要使用的布局檔案ID,需要顯示的key(對應Map)、組件的ID

public int getCount()

得到儲存集合的個數

public Object getItem(int position)

取得指定位置的對象

public long getItemId(int position)

取得指定位置對象的ID

public void notifyDataSetChanged()

當清單項目發生改變時,通知更新顯示ListView

 

 

 

 

 

 

 

 

 

 

 

清單項目顯示模板——res/layout/data_list.xml

 

ListView事件

方法

描述

public void setOnItemSelectedListener(

AdapterView.OnItemSelectedListener listener)

選項選中時觸發

public void setOnItemClickListener(

AdapterView.OnItemClickListener listener)

選項單擊時觸發

public void setOnItemLongListener(

AdapterView.OnItemLongListener listener)

選項長按時觸發

 

 

 

 

 

 

 

 

 

 

 1         ListView lv=(ListView)findViewById(R.id.listView); 2     List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); 3     //從數組資源中擷取資料 4     String[] data=getResources().getStringArray(R.array.haha); 5     for(int i=0;i<data.length;i++){ 6         Map<String,Object> map=new HashMap<String,Object>(); 7         map.put("img", R.drawable.b); 8         map.put("txt", data[i]); 9         list.add(map);10     }11 //參數列表:內容物件、列表的資料、清單項目的布局資源檔、鍵名數組、清單項目所有控制項的索引值12     SimpleAdapter adapter=new SimpleAdapter(this, list,R.layout.data_list, new String[]{"img","txt"}, new int[]{R.id.Dataimg,R.id.Datatv});13     lv.setAdapter(adapter);14 15 //設定ListView的單擊事件16     lv.setOnItemClickListener(new OnItemClickListener() {                  17             /**  ListView選項單擊時觸發18              *   adp 取得AdapterView對象19              *   view 取得AdapterView的父組件20              *   position 取得Adapter的操作位置21              *   id 取得ListView所在行的編號22              */23         public void onItemClick(AdapterView<?> adp, View view, int position, long id) {24                 Object obj=adp.getItemAtPosition(position);25                 Map<String, Object> objs=(Map<String, Object>)obj;26                 String str= (String)objs.get("txt");27                 Toast.makeText (Listview.this, str, 1000).show();28         }29     });30     
SimpleAdapter與List事件的使用——部分代碼

 

進階控制項【安卓5】——滾動視圖、列表視圖[ListView、SimpleAdapter類]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.