ListView的屬性詳解和探究

在咱們的平常開發中,ListView是一個最經常使用的組件,因此咱們很是有必要對它的屬性進行全面的瞭解。如今就以一個簡單的實例,對ListView的屬性作一個簡單的講解。           java

       首先咱們給出簡單的佈局文件,就一個簡單的ListView列表        :                 android

<LinearLayout 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" 
    android:background="#FFE1FF" 
    android:orientation="vertical" > 
    <ListView 
        android:id="@+id/listView1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> </LinearLayout>

     

       Activity文件,使用ArrayAdapter對列表進行填充        :                 ide

public class MainActivity extends Activity { 
    private ListView listView; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        listView = (ListView) findViewById(R.id.listView1); 
        final List<String> adapterData = new ArrayList<String>(); 
        for (int i = 0; i < 20; i++) { 
           adapterData.add("ListItem" + i); 
        } 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, adapterData); 
        listView.setAdapter(adapter); 
     } 
}

     

       運行效果以下        :                 佈局

                       

       接下來咱們在佈局的ListView中添加以下屬性,並演示其效果        。                 post

         

                 1.                      android:            stackFromBottom屬性:                                      在ListView和GridView中使用,使它們的內容從底部開始顯示        。                 測試

                佈局文件修改以下        :                 this

<ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:stackFromBottom="true" />

     

運行效果以下,列表在開始的時候,內容就從底部開始顯示:           spa

     

                                             

                 2.android:          transcriptMode屬性:                    設置列表的      transcriptMode模式,該模式指定列表      添加新的選項的時候,是否自動滑動到底部,顯示新的選項。           .net

                   disabled:取消transcriptMode模式,默認的                 code

                   normal:當接受到數據集合改變的通知,而且僅僅當最後一個選項已經顯示在屏幕的時候,自動滑動到底部        。                 

                   alwaysScroll:不管當前列表顯示什麼選項,列表將會自動滑動到底部顯示最新的選項        。                 

       佈局修改以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stackFromBottom="true" 
    android:transcriptMode="alwaysScroll" />

     

       Activity文件代碼以下        :                 

public class MainActivity extends Activity { 
    private ListView listView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        listView = (ListView) findViewById(R.id.listView1); 
        final List<String> adapterData = new ArrayList<String>(); 
        for (int i = 0; i < 20; i++) { 
          adapterData.add("ListItem" + i); 
        } 
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, adapterData); 
        listView.setAdapter(adapter); 
 
        //定時給列表添加新的選項,並通知列表更新 
        final Handler handler = new Handler(); 
        Timer timer = new Timer(); 
        timer.scheduleAtFixedRate(new TimerTask() { 
            @Override 
            public void run() { 
                adapterData.add("New ListItem"); 
                handler.post(new Runnable() { 
                @Override 
                public void run() { 
                    adapter.notifyDataSetChanged(); 
                }
            });  
        } 
    }, 3000, 3000); }

         運行結果以下      :               

     

6秒後的結果(每當添加新的表項的時候,列表自動滑動的底部顯示最新的表項)           

     

       其它兩個normal和disable屬性就不在演示,如上已經解釋的比較清楚,你們能夠本身試驗感覺一下        。                 

                                             

                 3.          android:cacheColorHint屬性:                            該屬性在官方文檔和個資料中找不到比較好的描述;根據實際的體驗,當你設置的ListView的背景時,應該設置該屬性爲「#00000000」(透明),        否則在滑動的時候,列表的顏色會變黑或者背景圖片小時的狀況        !                 

                                             

                 4.                      android:            divider屬性:                                      列表之間繪製的顏色或者圖片。通常開發中用於分隔表項        。                 

       在實際開發過程當中,若是你不想要列表之間的分割線,能夠設置屬性爲@null ,佈局文件以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:divider="@null" />

     

       運行結果以下        :                 

     

                                                                     

                             5.android:            fadingEdge屬性:                                      設置列表的陰影                 

       佈局文件以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/ic_launcher" 
    android:cacheColorHint="#000000ff" 
    android:fadingEdge="vertical"  
    android:fadingEdgeLength="40dp"/>

     

                 運行效果以下          :                           

     

                                             

                 6.          android:fastScrollEnabled屬性:                    啓用快速滑動條,它能快速的滑動列表。      但在實際的測試中發現,當你的數據比較小的時候,是不會顯示快速滾動條。           

       佈局文件以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fastScrollEnabled="true" />

     

       運行結果以下(當你快速滑動列表的時候,就出現以下快速滑動滾動條        ):                 

     

                                             

                 7.android          :                      listSelector                                屬性:                                      用來指明列表當前選中的選項的圖片                        

       佈局文件以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:listSelector="@drawable/ic_launcher" />

     

運行結果以下(當用手指點擊ListItem3的時候,出現以下效果,圖片顯示在ListItem3的底部,並未遮擋ListItem3):           

     

                                             

                 8.android:                      drawSelectorOnTop屬性:                                      當設置爲true時候,listSelector的圖片將會被繪製在被選中的選項之上        。                 

       佈局文件以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:drawSelectorOnTop="true" 
    android:listSelector="@drawable/ic_launcher" />

     

運行結果以下(對比上面,發現圖片遮擋了ListItem3的顯示):           

     

                                                                     

                             9.android:            choiceMode屬性:                                      定義了列表的選擇行爲,默認的狀況下,列表沒有選擇行爲        。                 

       none:正常不指定選擇的列表                 

       singleChoice:列表容許一個選擇                 

       multipleChoice:列表容許選擇多個                 

       mutipleChoiceModal        :                 

       佈局文件以下        :                 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:choiceMode="multipleChoice" />

     

                 Activity文件以下          :                           

public class MainActivity extends Activity { 
    private ListView listView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        listView = (ListView) findViewById(R.id.listView1); 
        final List<String> adapterData = new ArrayList<String>(); 
        for (int i = 0; i < 40; i++) { 
            adapterData.add("ListItem" + i); 
        } 
        //只有使用支持選擇的佈局才能選擇多項 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_checked, adapterData); 
        listView.setAdapter(adapter); 
        listView.setFastScrollEnabled(true); 
    } 
}

     

                 運行結果以下(點擊多個選項          ),          關於nono和singleChoice能夠本身嘗試一下          :                           

     

相關文章
相關標籤/搜索