Android入門之ListView應(yīng)用解析(二)
上一篇關(guān)于Android中ListView的介紹講的是如何制作一個(gè)具有兩行文本的自定義控件,作為L(zhǎng)istView的Item的使用方法。本文接下來(lái)也是圍繞ListView和Item,更加深入地介紹它們的用法。
首先,先來(lái)看看本文代碼運(yùn)行的結(jié)果,本文的Item比上一篇中的Item多出左邊的圖標(biāo):

main.xml的源代碼,跟上一篇的一樣,這里就不作解釋了,直接貼出item.xml的代碼,就是它實(shí)現(xiàn)ImageItem的UI:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip">
<ImageView
android:layout_width="wrap_content"
android:id="@+id/itemImage" android:layout_height="fill_parent">
</ImageView>
<TextView
android:text="TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/itemTitle" android:layout_toRightOf="@+id/itemImage" android:textSize="20dip">
</TextView>
<TextView
android:text="TextView02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/itemText" android:layout_toRightOf="@+id/itemImage" android:layout_below="@+id/itemTitle">
</TextView>
</RelativeLayout>
解釋一下 item.xml的代碼:這里使用了RelativeLayout布局,控件的關(guān)鍵的屬性是:
itemTitle的屬性 android:layout_toRightOf="@+id/itemImage" ,itemTitle在itemImage的右邊;
itemText的屬性 android:layout_toRightOf="@+id/itemImage",ItemText在itemImage的右邊, android:layout_below="@+id/itemTitle", itemText 在 itemTitle的下面。
最后,貼出JAVA的源代碼,代碼中的重點(diǎn)是LayoutInflate的用法。LayoutInflate的使用方法如下:
LayoutInflater的作用是,把一個(gè)View的對(duì)象與XML布局文件關(guān)聯(lián)并實(shí)例化。
View的對(duì)象實(shí)例化之后,可以通過findViewById()查找布局文件中的指定Id的組件。
Java代碼如下:
package com.testListView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class testListView extends Activity {
ListView listView;
String[] titles={"標(biāo)題1","標(biāo)題2","標(biāo)題3","標(biāo)題4"};
String[] texts={"文本內(nèi)容A","文本內(nèi)容B","文本內(nèi)容C","文本內(nèi)容D"};
int[] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("BaseAdapter for ListView");
listView=(ListView)this.findViewById(R.id.listView1);
listView.setAdapter(new ListViewAdapter(titles,texts,resIds));
}
public class ListViewAdapter extends BaseAdapter {
View[] itemViews;
public ListViewAdapter(String[] itemTitles, String[] itemTexts,
int[] itemImageRes) {
itemViews = new View[itemTitles.length];
for (int i = 0; i < itemViews.length; i++) {
itemViews[i] = makeItemView(itemTitles[i], itemTexts[i],
itemImageRes[i]);
}
}
public int getCount() {
return itemViews.length;
}
public View getItem(int position) {
return itemViews[position];
}
public long getItemId(int position) {
return position;
}
private View makeItemView(String strTitle, String strText, int resId) {
LayoutInflater inflater = (LayoutInflater) testListView.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 使用View的對(duì)象itemView與R.layout.item關(guān)聯(lián)
View itemView = inflater.inflate(R.layout.item, null);
// 通過findViewById()方法實(shí)例R.layout.item內(nèi)各組件
TextView title = (TextView) itemView.findViewById(R.id.itemTitle);
title.setText(strTitle);
TextView text = (TextView) itemView.findViewById(R.id.itemText);
text.setText(strText);
ImageView image = (ImageView) itemView.findViewById(R.id.itemImage);
image.setImageResource(resId);
return itemView;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
return itemViews[position];
return convertView;
}
}
}
- android實(shí)現(xiàn)listview分頁(yè)的方法
- android中ListView數(shù)據(jù)刷新時(shí)的同步方法
- Android編程之控件ListView使用方法
- Android中ExpandableListView的用法實(shí)例
- Android中使用ListView實(shí)現(xiàn)漂亮的表格效果
- Android提高之ListView實(shí)現(xiàn)自適應(yīng)表格的方法
- Android入門之ListView應(yīng)用解析(一)
- android開發(fā)教程之listview顯示sqlite數(shù)據(jù)
- android開發(fā)教程之實(shí)現(xiàn)listview下拉刷新和上拉刷新效果
- Android控件之ListView用法實(shí)例詳解
相關(guān)文章
Android?完整購(gòu)物商城界面的實(shí)現(xiàn)案例
這篇文章為大家?guī)?lái)一個(gè)Android?完整購(gòu)物商城的界面具體的實(shí)現(xiàn),案例中含有商品列表的顯示,為商城最重要的功能之一,感興趣的朋友來(lái)看看吧2022-03-03
Android中隱藏標(biāo)題欄和狀態(tài)欄的方法
Android中隱藏標(biāo)題欄和狀態(tài)欄的方法,需要的朋友可以參考一下2013-05-05
Android使用HorizontalScrollView實(shí)現(xiàn)水平滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android使用HorizontalScrollView實(shí)現(xiàn)水平滾動(dòng),并點(diǎn)擊有相應(yīng)的反應(yīng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
VS Code開發(fā)React-Native及Flutter 開啟無(wú)線局域網(wǎng)安卓真機(jī)調(diào)試問題
這篇文章主要介紹了VS Code開發(fā)React-Native,F(xiàn)lutter 開啟無(wú)線局域網(wǎng)安卓真機(jī)調(diào)試,需要的朋友可以參考下2020-04-04
Android頂部標(biāo)題欄的布局設(shè)計(jì)
大家好,本篇文章主要講的是Android頂部標(biāo)題欄的布局設(shè)計(jì),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android自定義view系列之99.99%實(shí)現(xiàn)QQ側(cè)滑刪除效果實(shí)例代碼詳解
這篇文章給大家介紹android自定義view系列之99.99%實(shí)現(xiàn)QQ側(cè)滑刪除效果,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-09-09
Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
android開發(fā)設(shè)計(jì)模式之——單例模式詳解
本篇文章主要介紹了android開發(fā)設(shè)計(jì)模式之——單例模式詳解,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
android TextView不用ScrollViewe也可以滾動(dòng)的方法
這篇文章主要介紹了android TextView不用ScrollViewe也可以滾動(dòng)的方法,很簡(jiǎn)單實(shí)用的代碼,大家參考使用吧2013-11-11

