android嵌套listview
㈠ Android 遍歷手機根目錄下的所有文檔類文件在listView上展示,文件稍微多一點響應速度就很慢,怎麼優化
在Android應用中,遍歷手機根目錄下的文檔類文件並在ListView上展示時,如果文件數量較多,可能會導致響應速度變慢。解決這一問題,可以通過優化文件遍歷邏輯和列表渲染性能來實現。
首先,可以將符合條件的文件路徑存儲在一個String數組中。遍歷文件時,可以使用File類的listFiles()方法獲取文件列表,然後通過文件名匹配指定格式,將符合條件的文件路徑添加到數組中。這樣,可以避免在每次用戶點擊時重新遍歷文件,從而提高響應速度。
其次,優化ListView的渲染性能。可以使用ViewHolder模式,避免在每次渲染時重復創建和銷毀視圖。此外,還可以使用懶載入技術,只在可見范圍內載入文件數據,減少內存消耗和渲染時間。
進一步地,可以考慮使用非同步線程進行文件遍歷和數據載入,避免阻塞主線程。通過AsyncTask或HandlerThread等機制,可以在後台線程中完成文件遍歷任務,然後將結果更新到UI線程中。這樣可以提高應用的流暢性和響應速度。
最後,針對大量文件的情況,可以採用分頁載入的方式。將文件數據分組顯示,用戶點擊載入下一頁時,再載入相應范圍的文件數據。這樣可以減少每次載入的數據量,提高應用的載入速度和用戶體驗。
綜上所述,通過優化文件遍歷邏輯、改進ListView渲染方法、利用非同步載入技術和分頁載入策略,可以有效提升在Android應用中遍歷手機根目錄下大量文檔類文件並在ListView上展示時的響應速度。
㈡ android listview怎麼用
Android listview與adapter用法
listview與adapter用法
一個ListView通常有兩個職責。
(1)將數據填充到布局。
(2)處理用戶的選擇點擊等操作。
第一點很好理解,ListView就是實現這個功能的。第二點也不難做到,在後面的學習中讀者會發現,這非常簡單。
一個ListView的創建需要3個元素。
(1)ListView中的每一列的View。
(2)填入View的數據或者圖片等。
(3)連接數據與ListView的適配器。
也就是說,要使用ListView,首先要了解什麼是適配器。適配器是一個連接數據和AdapterView(ListView就是一個典型的AdapterView,後面還會學習其他的)的橋梁,通過它能有效地實現數據與AdapterView的分離設置,使AdapterView與數據的綁定更加簡便,修改更加方便
Android中提供了很多的Adapter,表4-5列出了常用的幾個。
表4-5 常用適配器
Adapter
含義
ArrayAdapter<T>
用來綁定一個數組,支持泛型操作
SimpleAdapter
用來綁定在xml中定義的控制項對應的數據
SimpleCursorAdapter
用來綁定游標得到的數據
BaseAdapter
通用的基礎適配器
其實適配器還有很多,要注意的是,各種Adapter只不過是轉換的方式和能力不一樣而已。下面就通過使用不同的Adapter來為ListView綁定數據(SimpleCursorAdapter暫且不講,後面講SQLite時會介紹)。
4.12.1 ListView使用ArrayAdapter
用ArrayAdapter可以實現簡單的ListView的數據綁定。默認情況下,ArrayAdapter綁定每個對象的toString值到layout中預先定義的TextView控制項上。ArrayAdapter的使用非常簡單。
實例:
工程目錄:EX_04_12
在布局文件中加入一個ListView控制項。
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="
http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent"> <!-- 添加一個ListView控制項 --> <ListView
android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</LinearLayout>
然後在Activity中初始化。
publicclass MyListView extends Activity {
privatestaticfinal String[] strs = new String[] {
"first", "second", "third", "fourth", "fifth"
};//定義一個String數組用來顯示ListView的內容private ListView lv;/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);//得到ListView對象的引用 /*為ListView設置Adapter來綁定數據*/
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strs));
}
}
▲圖4-29 ListView使用ArrayAdapter運行效果
代碼非常的簡單,運行效果如圖4-29所示。
分析一下使用的步驟。
(1)定義一個數組來存放ListView中item的內容。
(2)通過實現ArrayAdapter的構造函數來創建一個ArrayAdapter的對象。
(3)通過ListView的setAdapter()方法綁定ArrayAdapter。
其中第二步有必要說一下的是,ArrayAdapter有多個構造函數,例子中實現的是最常用的一種。第一個參數為上下文,第二個參數為一個包含TextView,用來填充ListView的每一行的布局資源ID。第三個參數為ListView的內容。其中第二個參數可以自定義一個layout,但是這個layout必須要有TextView控制項。通常我們使用Android提供的資源,除了例子中所用的,常用的還有如下幾種,可實現帶RadioButton和CheckBox的ListView。
(1)通過指定android.R.layout.simple_list_item_checked這個資源,實現帶選擇框的ListView。需要用setChoiceMode()方法設定選擇為多選還是單選,否則將不能實現選擇效果,運行效果如圖4-30所示。
實現代碼如下:
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, strs));
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
(2)通過指定android.R.layout.simple_list_item_multiple_choice這個資源實現帶CheckBox的ListView。同樣的,需要用setChoiceMode()方法來設置單選或者多選,運行效果如圖4-31所示。
實現代碼如下:
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, strs));
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
(3)通過指定android.R.layout.simple_list_item_single_choice這個資源實現帶RadioButton的ListView。這里要注意的是,這里並不是指定了單選。是多選還是單選要通過setChoiceMode()方法來指定,運行效果如圖4-32所示。
實現代碼如下:
lv.setAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,strs));
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
在前面講到過,ListView的職責除了填充數據外,還要處理用戶的操作。通過如下的代碼就可以為ListView綁定一個點擊監聽器,點擊後在標題欄顯示點擊的行數。
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//點擊後在標題上顯示點擊了第幾行 setTitle("你點擊了第"+arg2+"行");
}
});
4.12.2 ListView使用SimpleAdapter
很多時候需要在列表中展示一些除了文字以外的東西,比如圖片等。這時候可以使用SimpleAdapter。SimpleAdapter的使用也非常簡單,同時它的功能也非常強大。可以通過它自定義ListView中的item的內容,比如圖片、多選框等。看一個例子,實現一個每一行都有一個ImageView和TextView的ListView。先看一下運行效果,如圖4-34所示。
▲圖4-34 帶圖標的ListView
首先在布局文件中增加一個ListView控制項。
還需要定義一個ListView中每一行的布局,用RelativeLayout來實現一個帶兩行字和一個圖片的布局。
item.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageViewandroid:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ItemImage"/>
<TextViewandroid:id="@+id/ItemTitle" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:textSize="20sp"/>
<TextViewandroid:id="@+id/ItemText" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle"/> </RelativeLayout>
配置完畢,就可以在Java代碼中為ListView綁定數據。
publicclass MyListViewSimple extends Activity {
private ListView lv;
/** Called when the activity is first created. */ @Override
publicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);/*定義一個動態數組*/
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();/*在數組中存放數據*/
for(int i=0;i<10;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.icon);//加入圖片 map.put("ItemTitle", "第"+i+"行");
map.put("ItemText", "這是第"+i+"行");
listItem.add(map);
}
SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,//需要綁定的數據
R.layout.item,//每一行的布局//動態數組中的數據源的鍵對應到定義布局的View中new String[] {"ItemImage"
,"ItemTitle", "ItemText"},
newint[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}
);
lv.setAdapter(mSimpleAdapter);//為ListView綁定適配器 lv.setOnItemClickListener(new
OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("你點擊了第"+arg2+"行");//設置標題欄顯示點擊的行
}
});
}
}
使用simpleAdapter的數據一般都是用HashMap構成的列表,列表的每一節對應ListView的每一行。通過SimpleAdapter的構造函數,將HashMap的每個鍵的數據映射到布局文件中對應控制項上。這個布局文件一般根據自己的需要來自己定義。梳理一下使用SimpleAdapter的步驟。
(1)根據需要定義ListView每行所實現的布局。
(2)定義一個HashMap構成的列表,將數據以鍵值對的方式存放在裡面。
(3)構造SimpleAdapter對象。
(4)將LsitView綁定到SimpleAdapter上。
4.12.3 ListView使用BaseAdapter與ListView的優化
在ListView的使用中,有時候還需要在裡面加入按鈕等控制項,實現單獨的操作。也就是說,這個ListView不再只是展示數據,也不僅僅是這一行要來處理用戶的操作,而是裡面的控制項要獲得用戶的焦點。讀者可以試試用SimpleAdapter添加一個按鈕到ListView的條目中,會發現可以添加,但是卻無法獲得焦點,點擊操作被ListView的Item所覆蓋。這時候最方便的方法就是使用靈活的適配器BaseAdapter了。
使用simpleAdapter的數據一般都是用HashMap構成的列表,列表的每一節對應ListView的每一行。通過SimpleAdapter的構造函數,將HashMap的每個鍵的數據映射到布局文件中對應控制項上。這個布局文件一般根據自己的需要來自己定義。梳理一下使用SimpleAdapter的步驟。
(1)根據需要定義ListView每行所實現的布局。
(2)定義一個HashMap構成的列表,將數據以鍵值對的方式存放在裡面。
(3)構造SimpleAdapter對象。
(4)將LsitView綁定到SimpleAdapter上。
4.12.3 ListView使用BaseAdapter與ListView的優化
在ListView的使用中,有時候還需要在裡面加入按鈕等控制項,實現單獨的操作。也就是說,這個ListView不再只是展示數據,也不僅僅是這一行要來處理用戶的操作,而是裡面的控制項要獲得用戶的焦點。讀者可以試試用SimpleAdapter添加一個按鈕到ListView的條目中,會發現可以添加,但是卻無法獲得焦點,點擊操作被ListView的Item所覆蓋。這時候最方便的方法就是使用靈活的適配器BaseAdapter了。
㈢ android中listview怎麼用
創建繼承BaseAdapter並實現其抽象方法的類MyListViewAdapter
說明
下面的講解中,只創建自定義的適配器類,如何使用請參考android中常用控制項的使用之ListView
1.創建類MyListViewAdapter
創建類MyListViewAdapter,該類繼承BaseAdapter,並實現其抽象方法:
1
2
3
4
int getCount();
Object getItem(int position);
long getItemId(int position);
View getView(int position,View convertView,ViewGroup parent);
getCount需要返回有多少個item,也就是說最會在listview中展示這么多行
getItem需要返回參數position位置的數據
getItemId返回position就行了
2.給MyListViewAdapter類添加成員變數和構造方法
兩個成員變數
1
2
List<String> list;
Context context;
list表示要顯示的數據,context變數在生成View對象時需要用到
構造方法:構造方法是為了給兩個成員變數賦值
1
2
3
4
public MyListViewAdapter(List<String> list , Context context) {
this.list = list;
this.context = context;
}
3.給getCount,getItem,getItemId方法添加代碼
getCount需要返回有多少個item,也就是說最會在listview中展示這么多行,所以返回list.size
getItem需要返回參數position位置的數據,也就是list中第position項的值list.get(position)
getItemId返回position就行了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
4.給getView方法添加代碼
getView方法是返回位置為position的View對象,第二個參數convertView考慮到資源重用問題,在上下滑動的過程中,需要顯示某項的時候才會調用getView方法,而如果有某項被隱藏不顯示,就會把不顯示那一行的View作為convertView參數傳入,如果沒有某項被隱藏,convertView值為null。可以通過下面代碼中的if(convertView!=null)中的輸出來看哪一行被隱藏了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("調用getView方法,顯示position="+position+"項");
if(convertView!=null){
TextView t = (TextView) convertView.findViewById(R.id.firstTextView);
System.out.println(t.getText());
}else{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_mylistviewadapter, null);
}
TextView t = (TextView)(convertView.findViewById(R.id.firstTextView));
t.setText(list.get(position));
if(position%2==0)
{
t.setBackgroundColor(Color.WHITE);
}
else{
t.setBackgroundColor(Color.GRAY);
}
return convertView;
}
補充:通過xml生成View對象
通過Context對象生成一個LayoutInflater對象
調用LayoutInflater對象的inflate方法生成控制項對象,inflate方法的第一個參數為xml文件,第二個參數一般為null。返回值為該xml文件最外層的標簽對象。
1
2
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout convertView =(LinearLayout)layoutInflater.inflate(R.layout.item_mylistvie
源代碼下載
pan..com/s/1ntuQDdv
㈣ 請問在android的listView中怎麼動態加入radioButton和Button按鈕
在Android的ListView中動態加入RadioButton和Button按鈕,首先需要一個bean來存儲數據。這個bean里可以包含一個標志位,用來標記是要顯示RadioButton還是Button。在自定義的Adapter中的getView方法里,根據這個標志位來決定顯示哪種類型的按鈕。為了實現這一點,布局文件中需要包含兩個按鈕,一個RadioButton和一個Button,只是在初始狀態下,一個按鈕會被隱藏。具體來說,可以通過設置這些控制項的visibility屬性來實現。
當軟體的下載狀態或安裝狀態發生變化時,bean中的標志位會隨之更新。此時,只需調用Adapter的notifyDataSetChanged()方法,ListView就會重新繪制,顯示最新的按鈕。
舉個例子,假設你的bean類如下所示:
public class MyBean {
private int type; // 0表示RadioButton,1表示Button
private String text;
...
}
在Adapter的getView方法里,你可以這樣處理:
public View getView(int position, View convertView, ViewGroup parent) {
MyBean bean = getItem(position);
View view = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
RadioButton radioButton = view.findViewById(R.id.radio_button);
Button button = view.findViewById(R.id.button);
if (bean.getType() == 0) {
button.setVisibility(View.GONE);
radioButton.setVisibility(View.VISIBLE);
} else {
radioButton.setVisibility(View.GONE);
button.setVisibility(View.VISIBLE);
}
return view;
}
這樣,每次數據發生變化時,ListView會自動更新顯示的內容。