Android开发中,我们常使用到ListView视图,下面介绍ListView的几种使用方法。
首先,在主界面上添加几个按钮,用于界面跳转。
界面布局:
<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: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" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button1" android:text="button1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button2" android:text="button2"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button3" android:text="button3"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button4" android:text="SimpleList"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button5" android:text="TitleList"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button6" android:text="IconList"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button7" android:text="ColorList"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button8" android:text="ArrayList"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Button9" android:text="test"/> </LinearLayout>
package com.example.listviewdemo; import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void Button1(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, Activity01.class); startActivity(intent); } public void Button2(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, Activity02.class); startActivity(intent); } public void Button3(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, Activity03.class); startActivity(intent); } public void Button4(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, SimpleList.class); startActivity(intent); } public void Button5(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, TitleList.class); startActivity(intent); } public void Button6(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, IconList.class); startActivity(intent); } public void Button7(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, ColorList.class); startActivity(intent); } public void Button8(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, ArrayList.class); startActivity(intent); } public void Button9(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, MyTest.class); startActivity(intent); } }
package com.example.listviewdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class Activity01 extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = new ListView(this); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData())); setContentView(listView); } private List<String> getData(){ List<String> data = new ArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据4"); return data; } }
package com.example.listviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.widget.SimpleAdapter; public class Activity02 extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist, new String[]{"title","info","img"}, new int[]{R.id.title,R.id.info,R.id.img}); setListAdapter(adapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "标题1"); map.put("info", "内容1"); map.put("img", R.drawable.icon_marka); list.add(map); map = new HashMap<String, Object>(); map.put("title", "标题2"); map.put("info", "内容2"); map.put("img", R.drawable.icon_markb); list.add(map); map = new HashMap<String, Object>(); map.put("title", "标题3"); map.put("info", "内容3"); map.put("img", R.drawable.icon_markc); list.add(map); return list; } }
vlist布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="22px" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13px" /> </LinearLayout> </LinearLayout>
package com.example.listviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.AlertDialog; import android.app.ListActivity; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class Activity03 extends ListActivity { private List<Map<String, Object>> mData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mData = getData(); MyAdapter adapter = new MyAdapter(this); setListAdapter(adapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "标题1"); map.put("info", "内容1"); map.put("img", R.drawable.icon_marka); list.add(map); map = new HashMap<String, Object>(); map.put("title", "标题2"); map.put("info", "内容2"); map.put("img", R.drawable.icon_markb); list.add(map); map = new HashMap<String, Object>(); map.put("title", "标题3"); map.put("info", "内容3"); map.put("img", R.drawable.icon_markc); list.add(map); return list; } @Override protected void onListItemClick(ListView l, View v, int position, long id) { String string = "click" + (String)mData.get(position).get("title"); Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); } public void showInfo() { new AlertDialog.Builder(this) .setTitle("我的listview") .setMessage("介绍...") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } public final class ViewHolder { public ImageView img; public TextView title; public TextView info; public Button viewBtn; } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int arg0) { return null; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.vlist2, null); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.title = (TextView)convertView.findViewById(R.id.title); holder.info = (TextView)convertView.findViewById(R.id.info); holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.img.setBackgroundResource((Integer)mData.get(position).get("img")); holder.title.setText((String)mData.get(position).get("title")); holder.info.setText((String)mData.get(position).get("info")); holder.viewBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showInfo(); } }); return convertView; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="22px" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13px" /> </LinearLayout> <Button android:id="@+id/view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" android:layout_gravity="bottom|right" android:focusable="false" /> </LinearLayout>
package com.example.listviewdemo; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class SimpleList extends ListActivity { private String[] mListStr = {"标题1:内容1","标题2:内容2","标题3:内容3","标题4:内容4"}; ListView mListView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListView = getListView(); setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListStr)); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) { Toast.makeText(SimpleList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show(); } }); } }
效果
package com.example.listviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class TitleList extends ListActivity { private String[] mListTitle = { "标题1", "标题2", "标题3", "标题4", "标题5"}; private String[] mListStr = { "内容1", "内容2", "内容3", "内容4", "内容5" }; ListView mListView = null; ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListView = getListView(); int lengh = mListTitle.length; for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("title", mListTitle[i]); item.put("text", mListStr[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,android.R.layout.simple_list_item_2, new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2}); setListAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) { Toast.makeText(TitleList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show(); } }); } }
package com.example.listviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class IconList extends ListActivity { private String[] mListTitle = { "标题1", "标题2", "标题3", "标题4", "标题5"}; private String[] mListStr = { "内容1", "内容2", "内容3", "内容4", "内容5" }; ListView mListView = null; ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListView = getListView(); int lengh = mListTitle.length; for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("image", R.drawable.ic_launcher); item.put("title", mListTitle[i]); item.put("text", mListStr[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist, new String[]{"image","title","text"},new int[]{R.id.image,R.id.title,R.id.text}); setListAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) { Toast.makeText(IconList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:padding="2dip" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_above="@+id/text" android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical" android:textSize="15dip" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="20dip" /> </RelativeLayout>
package com.example.listviewdemo; import android.app.ListActivity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ColorList extends ListActivity { private String[] mListTitle = { "标题1", "标题2", "标题3", "标题4", "标题5"}; private String[] mListStr = { "内容1", "内容2", "内容3", "内容4", "内容5" }; ListView mListView = null; MyListAdapter myAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListView = getListView(); myAdapter = new MyListAdapter(this); setListAdapter(myAdapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) { View v=adapterView.getChildAt(position); v.setBackgroundColor(Color.RED); Toast.makeText(ColorList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show(); } }); } class MyListAdapter extends BaseAdapter { private int[] colors = new int[] { 0xff626569, 0xff4f5257 }; public MyListAdapter(Context context) { mContext = context; } public int getCount() { return mListStr.length; } @Override public boolean areAllItemsEnabled() { return false; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView iamge = null; TextView title = null; TextView text = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.colorlist, null); iamge = (ImageView) convertView.findViewById(R.id.color_image); title =(TextView) convertView.findViewById(R.id.color_title); text= (TextView) convertView.findViewById(R.id.color_text); } int colorPos = position % colors.length; convertView.setBackgroundColor(colors[colorPos]); title.setText(mListTitle[position]); text.setText(mListStr[position]); iamge.setImageResource(R.drawable.icon_gcoding); return convertView; } private Context mContext; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/color_image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:padding="2dip" /> <TextView android:id="@+id/color_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/color_image" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="15dip" /> <TextView android:id="@+id/color_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/color_image" android:layout_below="@+id/color_title" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="20dip" /> </RelativeLayout>
package com.example.listviewdemo; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ArrayList extends ListActivity { private String[] mListTitle = { "标题1", "标题2", "标题3", "标题4", "标题5"}; private String[] mListStr = { "内容1", "内容2", "内容3", "内容4", "内容5" }; ListView mListView = null; MyListAdapter myAdapter = null; ArrayList arrayList = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); arrayList = this; mListView = getListView(); myAdapter = new MyListAdapter(this,R.layout.arraylist); setListAdapter(myAdapter); } public class MyListAdapter extends ArrayAdapter<Object> { int mTextViewResourceID = 0; private Context mContext; public MyListAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); mTextViewResourceID = textViewResourceId; mContext = context; } private int[] colors = new int[] { 0xff626569, 0xff4f5257 }; public int getCount() { return mListStr.length; } @Override public boolean areAllItemsEnabled() { return false; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { ImageView iamge = null; TextView title = null; TextView text = null; Button button = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(mTextViewResourceID, null); iamge = (ImageView) convertView.findViewById(R.id.array_image); title = (TextView) convertView.findViewById(R.id.array_title); text = (TextView) convertView.findViewById(R.id.array_text); button = (Button)convertView.findViewById(R.id.array_button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(arrayList,"您点击的第"+position +"个按钮", Toast.LENGTH_LONG).show(); } }); } int colorPos = position % colors.length; convertView.setBackgroundColor(colors[colorPos]); title.setText(mListTitle[position]); text.setText(mListStr[position]); if(colorPos == 0) iamge.setImageResource(R.drawable.icon_st); else iamge.setImageResource(R.drawable.icon_en); return convertView; } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/array_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一个按钮" /> <ImageView android:id="@+id/array_image" android:layout_toRightOf="@+id/array_button" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:padding="2dip" /> <TextView android:id="@+id/array_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/array_image" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="15dip" /> <TextView android:id="@+id/array_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/array_image" android:layout_below="@+id/array_title" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="20dip" /> </RelativeLayout>
第一个界面
package com.example.listviewdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class MyTest extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = new ListView(this); listView.setAdapter(new DemoListAdapter()); setContentView(listView); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) { onListItemClick(position); } }); } void onListItemClick(int index) { Intent intent = null; intent = new Intent(this,demos[index].demoClass); startActivity(intent); } private static final DemoInfo[] demos = { new DemoInfo(R.string.title1, R.string.content1, MyTest2.class), new DemoInfo(R.string.title2, R.string.content2, MyTest2.class), new DemoInfo(R.string.title3, R.string.content3, MyTest2.class), }; private class DemoListAdapter extends BaseAdapter { public DemoListAdapter() { super(); } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = View.inflate(MyTest.this, R.layout.list, null); TextView title = (TextView)convertView.findViewById(R.id.title); TextView desc = (TextView)convertView.findViewById(R.id.info); title.setText(demos[position].title); desc.setText(demos[position].desc); return convertView; } @Override public int getCount() { return demos.length; } @Override public Object getItem(int position) { return demos[position]; } @Override public long getItemId(int position) { return position; } } private static class DemoInfo { private final int title; private final int desc; private final Class<? extends android.app.Activity> demoClass; public DemoInfo(int title, int desc, Class<? extends android.app.Activity> demoClass) { this.title = title; this.desc = desc; this.demoClass = demoClass; } } }
package com.example.listviewdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; public class MyTest2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = new View(this); setContentView(view); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="3dp"/> <TextView android:id="@+id/info" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="12sp" android:layout_marginBottom="3dp"/> </LinearLayout>
原文地址:http://blog.csdn.net/bingdianlanxin/article/details/46593107