如题先给效果图:
主界面(MainActivity.java):
package com.tody.listviewsimpledemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/**
* 主界面
*
* @author tody.yang
*
*/
public class MainActivity extends Activity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化组件
*/
private void initView() {
mListView = (ListView) findViewById(R.id.listView);
String[] titleArr = { "我是第一项", "我是第二项", "我是第三项", "我是第四项", "我是第五项",
"我是第六项", "我是第七项", "我是第八项", "我是第九项", "我是第十项", };
int[] imageArr = { R.drawable.d0, R.drawable.d1, R.drawable.d2,
R.drawable.d3, R.drawable.d4, R.drawable.d0, R.drawable.d1,
R.drawable.d2, R.drawable.d3, R.drawable.d4 };
List<HashMap<String, Object>> mList = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < titleArr.length; i++) {
HashMap<String, Object> mHashMap = new HashMap<String, Object>();
mHashMap.put("title", titleArr[i]);
mHashMap.put("imgUrl", imageArr[i]);
mHashMap.put("content", "我就是内容");
mList.add(mHashMap);
}
SimpleAdapter mSimpleAdapter = new SimpleAdapter(MainActivity.this,
mList, R.layout.listview_item,
// 对应HashMap的key属性
new String[] { "title", "imgUrl", "content" },
// 对应动态加载布局(listview_item.xml)的需要现实组件的ID
new int[] { R.id.textView_title, R.id.imageView,
R.id.textView_content });// 谷歌提供的适配器的一种
mListView.setAdapter(mSimpleAdapter);// 列表设置适配器然后显示
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//这里虽然arg2和arg3都是能得到当前子项下标,但是推荐arg3,因为貌似有个方法arg2会得到空值
Toast.makeText(MainActivity.this, "点击了第"+(arg3+1)+"项", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
主界面的布局文件(activity_main.xml):
<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:orientation="vertical"
tools:context=".MainActivity"
>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
动态加载的布局文件(listview_item.xml):
<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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是标题" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/d0" />
</LinearLayout>
<TextView
android:id="@+id/textView_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是内容!!!" />
</LinearLayout>