android – ListView的使用。

來源:互聯網
上載者:User

ListView 的功能比較強大,也有很多種使用方法,在這裡介紹一種簡單的使用方法。首先,看一下:

這個列表的功能是單擊某一項時,就選中某一項,並反顯它,其它功能同ListView.

要實現上面的效果,需要做一下BaseAdapter,同時需要修改ListView的屬性及右邊滑塊的屬性。

下面給出MyListAdapter的代碼:

import java.lang.reflect.Field;import android.content.Context;import android.graphics.Color;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.*;public class MyListAdapter extends BaseAdapter{private Context mContext;private boolean[] isFocused ;private TextView[] ItemContent ;private int ItemCount;private int whichClick = -1;public StoryListAdapter(Context c, String[] str, int length){this.mContext = c;this.isFocused = new boolean[length];this.ItemContent = new TextView[length];this.ItemCount = length;for (int i = 0; i < length; i++){isFocused[i] = false;}TextView v;for (int i = 0; i < length; i++){v = new TextView(mContext);v.setText("◆"+str[i]);v.setTextSize(30);ItemContent[i] = v;}}//@Overridepublic int getCount(){// TODO Auto-generated method stub//return ListData.length;return ItemCount;}//@Overridepublic Object getItem(int position){// TODO Auto-generated method stubreturn position;}//@Overridepublic long getItemId(int position){// TODO Auto-generated method stubreturn position;}//@Overridepublic View getView(int position, View convertView, ViewGroup parent){TextView v = ItemContent[position];if(isFocused[position])v.setTextColor(Color.WHITE);elsev.setTextColor(Color.BLACK);v.setBackgroundColor(isFocused[position]?Color.rgb(100,66,24):Color.TRANSPARENT);return v;}public void setCurrentSel(int index){isFocused[whichClick == -1 ? 0 : whichClick] = false;whichClick = index;isFocused[index] = true;notifyDataSetInvalidated();}public void setUnCurrentSel(){for (int i = 0; i < ItemCount; i++){isFocused[i] = false;}notifyDataSetInvalidated();}public int getCurrentSel(){int ret = 0 ;if(whichClick != -1)ret = whichClick; return ret;}}

在主程式中用下面的方式進行調用:

String[] ListTestData = {"測試資料1","測試資料2","測試資料3","測試資料4","測試資料5","測試資料6","測試資料7","測試資料8","測試資料9","測試資料10","測試資料11","測試資料12","測試資料13","測試資料14","測試資料15","測試資料16"};MyListAdapter myListAdapter;listview = (ListView)findViewById(R.id.main_list_view);myListAdapter = new MyListAdapter(this,ListTestData,ListTestData.length);        listview.setAdapter(storyListAdapter);        storyListAdapter.setCurrentSel(0);

到此,基本上就實現了上面的效果,但是還有很多問題,我們一個一個來解決:

1、單擊列表後,列表的背景變成了黑色了。可以指定android:cacheColorHint的屬性來放變它,我們需要將它指定為透明。使用下面的屬性值:

android:cacheColorHint="#000000"

2、去除清單項目中間的分割線:android:divider="#00000000",這裡的值也可以指向一個drawable圖片對象(android:divider="@drawable/list_line"),如果使用了圖片高度大於系統的像素的話,可以自己設定一個高度。android:dividerHight="10px"

3、listview在拖動時,listview的背景變成黑色。可以使用下面的代碼時行解決:android:scrollingCache="false"

4、listview的上邊和下邊有黑色的陰影。可以使用下面的代碼進行解決:android:fadingEdge="none"

5、listview右邊的滑動條覆蓋清單項目的內容。可以使用下面的代碼進行解決:android:scrollbarStyle="outsideInset"

6、修改listvew右邊的滑動條與清單項目之間的距離。可以使用下面代碼進行解決:android:paddingRight="10dip",可以根據需要進行修改。

7、修改右邊的滑動條顯示的顏色。可以使用下面的代碼進行解決:

    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"
    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"  

其中scrollbar_vertical_track和scrollbar_vertical_thumb是滑動條的XML設定檔,在DemoAPI中有,你只要根據自己的需要修改一下開始顏色和結束顏色即可。下面給出xml檔案的內容:

scrollbar_vertical_track.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">     <gradient android:startColor="#D6C9B5" android:endColor="#D6C9B5"             android:angle="0"/>    <corners android:radius="0dp" /></shape>

scrollbar_vertical_thumb.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <gradient android:startColor="#634117" android:endColor="#634117"            android:angle="0"/>    <corners android:radius="6dp" /></shape>

其中android:startColor="#D6C9B5"為#RGB的開始顏色。

8、單擊Item時,無背景顏色變化,需要在ListView中指定它的listSelector屬性,如下:

android:listSelector="@layout/list_press"

list_press.xml的內容如下:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /></selector> 

到此,基上已經完成了上面的效果。

下面給出所有的完整的ListView屬性設定項:

<ListView    android:id="@+id/main_list_view"      android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:scrollingCache="false"    android:fadingEdge="none"    android:cacheColorHint="#000000"    android:divider="#00000000"    android:scrollbarStyle="outsideInset"    android:paddingRight="10dip"    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"        android:listSelector="@layout/list_press" >

9、如何使用和自訂FastScroller,在ListView的右邊顯示一個滑塊,如下:

要實現它,很容易,只需要在ListView中設定它的屬性即可,如下:

    android:fastScrollEnabled="true"     android:focusable="true" 

但是有時候會發現設定屬性無效,滾動ListView並未出現滑塊。原因是該屬性生效有最小記錄限制。當ListView記錄能夠在4屏以內顯示(也就是說滾動4頁)就不會出現滑塊。可能是api設計者認為這麼少的記錄不需要快速滾動。

如何修改FastScroller那個滑塊的圖片呢,如下:

可以使用下面的代碼:

try {Field f = AbsListView.class.getDeclaredField("mFastScroller");f.setAccessible(true);Object o = f.get(listview);f = f.getType().getDeclaredField("mThumbDrawable");f.setAccessible(true);Drawable drawable = (Drawable) f.get(o);        //R.drawable.scrollbar為自己自訂的圖片drawable = getResources().getDrawable(R.drawable.scrollbar);f.set(o, drawable);} catch (Exception e) {throw new RuntimeException(e);}  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.