閱讀197 返回首頁    go 阿裏雲 go 技術社區[雲棲]


各種ListView列表方法

ListView的一些用法
https://blog.csdn.net/pathuang68/article/details/6455925
https://blog.csdn.net/comkingfly/article/details/6578907




Adapter用於實現要顯示的列表,複雜的可以重寫BaseAdapter提供的getView方法,簡單的就用SimpleAdapeter(this,要顯示的list,布局文件,提取list中Map的Key對應的value,布局中的那些控件作為列表項)
方法一:
讓該activity entends ListActivity,並且用SimpleAdapter{

// 生成一個List對象,並按照SimpleAdapter的標準,將bean當中的數據添加到List當中去
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
Mp3Info mp3Info = (Mp3Info) iterator.next();

                        //利用哈希map存儲對應的鍵值
HashMap<String, String> map = new HashMap<String, String>();
map.put("mp3_name", mp3Info.getMp3Name());
map.put("mp3_size", mp3Info.getMp3Size());

                        //並將map存入list
list.add(map);
}
// 創建一個SimpleAdapter對象
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
R.layout.mp3info_item, new String[] { "mp3_name", "mp3_size" },
new int[] { R.id.mp3_name, R.id.mp3_size });
// 將這個SimpleAdapter對象設置到ListActivity當中,則會顯示在控件當中
setListAdapter(simpleAdapter);







可以新建一個xml,用於注冊要顯示內容的控件,R.id.mp3_name,R.id.mp3_size
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"
android:paddingBottom="1dip"
>
<TextView android:
android:layout_height="30dip"
android:layout_width="180dip"
android:textSize="10pt"/>
<TextView android:
android:layout_height="30dip"
android:layout_width="180dip"
android:textSize="10pt"/>
</LinearLayout>





方法二:
製作漂亮的ListView

在xml中製作按鈕,或者ListItem按下的效果
新建一個listview-selected.xml,在裏麵填寫pressed的時候引用的圖片。
在ListView的item項目布局中調用listView-selected
具體提代碼:
按鈕的狀態:
我們一般搞UI設計,按鈕通常有三個狀態:normal(正常狀態);focus(焦點狀態),pressed(按下狀態)。如下圖所示:



我們會在res/drawable目錄下定義一個資源文件,比如我們本例中要用到的handle.xml,在裏麵定義三種狀態,每種狀態對應一張圖片:
代碼如下:
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:andro> 
  3. <item android:state_window_focused="false" android:drawable="@drawable/handle_normal" /> 
  4. <item android:state_focused="true" android:drawable="@drawable/handle_focused" /> 
  5. <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" /> 
  6. </selector> 

複製代碼
而我們使用這個資源文件的用法隻需要引用drawable裏的資源文件(android:background="@drawable/handle")代碼如下:

  1. [/backcolor][/color][/align]
  2. [align=left][color=rgb(51,51,51)][backcolor=rgb(255,255,255)]<Button    
  3. android: 
  4. android:layout_width="wrap_content" 
  5. android:layout_height="fill_parent" 
  6. android:background="@drawable/handle" 
  7. /> [/backcolor][/color][/align]
  8. [align=left][color=rgb(51,51,51)][backcolor=rgb(255,255,255)]
複製代碼



Android中的層:
看過《盜夢空間》的人都知道,夢境有多少層,而Android中也有層次之分,在Android中第一層"夢境",我們可以認為是壁紙。第二層就是應用的Activity,第三層就是放在Activity上的容器(ViewGroup以及它的子類FrameLayout,LinearLayout等布局對象),當然容器中還可以放容器,你也可以放到N層(最多放多少我還沒驗證過),總之最後一層就是那些繼承於View的控件了(諸如,Button,TextView等.)

而ListView以及GridView中UI是怎麼設計的呢,下麵我們看一下效果圖:



上圖是一個ListView的效果圖,正常狀態下是白色背景黑色字體,當我們點擊一列時會出現黃色背景。這一效果是如何做到的呢?
ListView單元格顯示的內容其實是我們事先定義在Layout目錄下的一個布局文件,從這個效果來看,我們可以看出它一共有三個“層”
第一層容器(LinearLayout) 背景色為白色:
第二層也是容器(LinearLayout)當按下時,背景色為黃色,把第一層擋住(具體做法可以參照按鈕):
第三層是控件(TextView)。
實例 :
上麵說了一些,有些人肯定會雲裏霧裏,所以我們直接來個實例,實例做完後,再看一下,效果會更好,大家按照步驟跟我來:
第一步:首先準備素材,準備三個按鈕,以及ListView的背景圖(上麵三個按鈕已經有了,下麵我隻貼一個ListView背景圖片):


第二步:新建一個Android工程,命名為UIDemo.目錄結構如下圖所示:

第三步:在res目錄下新建一個drawable文件夾,定義兩個資源文件一個是handle.xml另一個為listview_selected.xml,其中handle.xml代碼已經在上麵貼出,listview_selected.xml代碼如下:


  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:andro>
  3. <item android:state_pressed="true" android:drawable="@drawable/list_selector_background_pressed" /> 
  4. </selector> 
複製代碼


第四步:修改main.xml布局文件,這裏我用到了SliddingDrawer控件,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:andro 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 

  6. <SlidingDrawer 
  7. android: 
  8. android:layout_width="fill_parent" 
  9. android:layout_height="fill_parent" 
  10. android:orientation="horizontal" 
  11. android:handle="@+id/handle" 
  12. android:content="@+id/content"> 
  13. <Button 
  14. android: 
  15. android:layout_width="wrap_content" 
  16. android:layout_height="fill_parent" 
  17. android:background="@drawable/handle" 
  18. /> 
  19. <ListView 
  20. android: 
  21. android:layout_width="fill_parent" 
  22. android:layout_height="wrap_content" 
  23. /> 
  24. </SlidingDrawer> 
  25. </LinearLayout> 
複製代碼


我們這裏用到了ListView控件,而我們ListView控件顯示的內容我事先在layout目錄下定義兩個TextView,命名為listview_layout.xml,代碼如下(這裏有三層哦!):

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:andro 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. android:background="#ffffff" 

  7. <LinearLayout 
  8. android:orientation="vertical" 
  9. android:layout_width="fill_parent" 
  10. android:layout_height="fill_parent" 
  11. android:background="@drawable/listview_selected" 
  12. android:padding="6px" 

  13. <TextView 
  14. android: 
  15. android:layout_width="fill_parent" 
  16. android:layout_height="wrap_content" 
  17. android:textSize="20px" 
  18. android:textColor="#000000" 
  19. /> 
  20. <TextView 
  21. android: 
  22. android:layout_width="fill_parent" 
  23. android:layout_height="wrap_content" 
  24. android:textSize="16px" 
  25. android:textColor="#000000" 
  26. /> 
  27. </LinearLayout> 
  28. </LinearLayout> 
複製代碼


第五步:修改主核心程序UIDemo.java,代碼如下:
  1. package com.tutor.uidemo; 
  2. import android.app.Activity; 
  3. import android.os.Bundle; 
  4. import android.view.LayoutInflater; 
  5. import android.view.View; 
  6. import android.view.ViewGroup; 
  7. import android.widget.BaseAdapter; 
  8. import android.widget.ListView; 
  9. import android.widget.TextView; 
  10. public class UIDemo extends Activity { 

  11. private ListView mListView; 
  12. @Override 
  13. public void onCreate(Bundle savedInstanceState) { 
  14. super.onCreate(savedInstanceState); 
  15. setContentView(R.layout.main); 

  16. setupViews(); 


  17. private void setupViews(){ 
  18. mListView = (ListView)findViewById(R.id.content); 
  19. mListView.setAdapter(new ListViewAdapter()); 


  20. private class ListViewAdapter extends BaseAdapter{ 
  21. //這裏返回10行,ListView有多少行取決於getCount()方法 
  22. public int getCount() { 
  23. return 10; 

  24. public Object getItem(int arg0) { 
  25. return null; 

  26. public long getItemId(int arg0) { 
  27. return 0; 

  28. public View getView(int position, View v, ViewGroup parent) { 

  29. final LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); 

  30. if(v == null){ 
  31. v = inflater.inflate(R.layout.listview_layout, null); 

  32. TextView mBookName = (TextView)v.findViewById(R.id.bookname); 
  33. TextView mBookAuthor = (TextView)v.findViewById(R.id.author); 

  34. mBookName.setText("Android傻瓜教程" + position); 
  35. mBookAuthor.setText("Frankiewei" + position); 
  36. return v; 




複製代碼
第六步:運行上述工程,查看效果:
運行效果1:


點擊按鈕效果2:




ListView正常效果3:


ListView正常效果4:



最後更新:2017-04-02 17:28:36

  上一篇:go android 動態向Gallery中添加圖片及倒影&amp;&amp;3D效果
  下一篇:go Android 屏蔽 Home 按鍵