手机中的UI界面最常见的就是列表显示内容了,最基本的电话簿就是其布局,像这种地上两层的布局内容(底层listview上层是内容如imageview、textview等等)要用到一个特殊的类BaseAdapter来实现的。
先看一下具体效果
布局文件如下:
1、activity_main.xml很简单:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
2、listviewcontent.xml:
<RelativeLayout 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:paddingBottom="4dip"
android:paddingLeft="12dip" >
<ImageView
android:id="@+id/imagerview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imagerview"
android:textSize="30dip" />
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:layout_toRightOf="@id/imagerview" />
</RelativeLayout>
代码实现部分:
package com.icedcap.listviewtest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
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 MainActivity extends Activity {
private ListView lv;
private ImageView iv;
private TextView tv1, tv2;
String[] titles = { "标题1", "标题2", "标题3", "标题4" };
String[] texts = { "文本内容A", "文本内容B", "文本内容C", "文本内容D" };
int[] resIds={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
ListViewAdapter adapter = new ListViewAdapter(titles, texts, resIds);
lv.setAdapter(adapter);
}
private void initView() {
lv = (ListView) findViewById(R.id.listview);
iv = (ImageView) findViewById(R.id.imagerview);
tv1 = (TextView) findViewById(R.id.textview1);
tv2 = (TextView) findViewById(R.id.textview2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
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) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 使用View的对象itemView与R.layout.item关联
View itemView = inflater.inflate(R.layout.listviewcontent, null);
// 通过findViewById()方法实例R.layout.item内各组件
TextView title = (TextView) itemView.findViewById(R.id.textview1);
title.setText(strTitle);
TextView text = (TextView) itemView.findViewById(R.id.textview2);
text.setText(strText);
ImageView image = (ImageView) itemView.findViewById(R.id.imagerview);
image.setImageResource(resId);
return itemView;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
return itemViews[position];
return convertView;
}
}
}
源码下载地址:http://download.csdn.net/detail/icedcap/6982525