ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 绘制出漂亮的列表,说道ListView就不得不说Adapter适配器,因为只有通过Adapter才可以把列表中的数据映射到ListView中。
在android的开发中最Adapter 一共可以分为
ArrayAdapter<T>,
BaseAdapter,
CursorAdapter,
HeaderViewListAdapter,
ResourceCursorAdapter,
SimpleAdapter,
SimpleCursorAdapter,
WrapperListAdapter
软件开发中最常用的有ArrayAdapter<T>, BaseAdapter, SimpleAdapter,今天我用一段代码向大家诠释如何使用ListView控件。
1.简单的ListView
在List列表中如果不存在过于复杂的东西 我们可以直接去new ArrayAdapter() 来绘制列表,无须继承ArrayAdapter,重写它的方法。但是如果列表中过于复杂的话就需要使用自定义布局来实现List列表。
item.xml:
<span style="font-family:宋体, Arial Narrow, arial, serif;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/list1" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </LinearLayout></span><span style="font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 26.6666679382324px; background-color: rgb(255, 255, 255);"> </span>
<span style="font-size: 24px;"> </span><span style="font-size:14px;">private ListView listview; private String[] ListViewArray = {"skyfin","locojyw","facker","gad"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.list1); listview = (ListView)findViewById(R.id.list1); listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ListViewArray)); listview.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(listAdapterActivity.this,"您选择了" + ListViewArray[position], Toast.LENGTH_LONG).show(); } }); }</span>
2.带标题的ListView列表
使用 simpleAdapter 需要注意的是须要用Map<String,Object> item 来保存列表中每一项的显示的title与text , new SimpleAdapter的时候将map中的数据写入 ,程序就会帮我们绘制列表了。
private ListView listview; private String[] ListViewTitleArray = {"skyfin","locojyw","facker","gad"}; private String[] ListViewSubTitleArray = {"2013","2014","2015","2016"}; private ArrayList<Map<String, Object>> mData= new ArrayList<Map<String,Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.list1); listview = (ListView)findViewById(R.id.list1); int lengh = ListViewTitleArray.length; for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("title", ListViewTitleArray[i]); item.put("text", ListViewSubTitleArray[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), mData, android.R.layout.simple_list_item_2 , new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2}); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(SimpleAdapterActivity.this,"您选择了" + ListViewTitleArray[position], Toast.LENGTH_LONG).show(); } }); }
3.带图片的ListView列表
使用 simpleAdapter 来操作 但是构造simpleAdapter的时候须要使用我们自己写的布局来完成 ,因为系统的布局已经不能满足需求了,同样Map<String,Object> item 来保存列表中每一项须要的显示内容 如 图片 标题 内容等。
添加我们自己的列表布局 图片 标题 内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="45dp" android:orientation="horizontal" > <ImageView android:id="@+id/image" android:layout_width="45dip" android:layout_height="45dip" android:src="@drawable/drawer_menu_icon_setting_nor" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingLeft="13dip" android:textSize="30dip" android:text="aaa" /> </LinearLayout>
private ListView listview; private String[] ListViewArray = {"技术问答","开源软件","博客区","Git客户端"}; private int[] ListViewImageArray = {R.drawable.drawer_menu_icon_quest_nor,R.drawable.drawer_menu_icon_opensoft_nor ,R.drawable.drawer_menu_icon_blog_nor,R.drawable.drawer_menu_icon_gitapp_nor}; ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.list3); listview = (ListView)findViewById(R.id.list3); int lengh = ListViewArray.length; for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("image", ListViewImageArray[i]); item.put("title", ListViewArray[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.item3, new String[]{"image","title"},new int[]{R.id.image,R.id.textview}); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(PicListAdapterActivity.this,ListViewArray[position], Toast.LENGTH_LONG).show(); } }); }
4.自定义布局BaseAdapter修改列表颜色
因为通过直接 构造系统的布局来绘制列表方法肯定是有限的,所以我们需要重写绘制方法 ,写一个类去继承BaseAdapter 并实现这个类中的方法,listView在一开始绘制的时候首先会调用getCout()方法得到绘制次数 ,然后会实例化自己定义的BaseAdapter通过getView()方法一层一层绘制ListView,所以我们可以在这里面根据position(当前绘制的ID)来任意的修改绘制的内容,做出好看漂亮的ListView,下面这个例子我通过重写getView修改每个列表的颜色
并且实现用户选中后成高亮状态。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60dip" android:orientation="horizontal" > <ImageView android:id="@+id/iamgeview" android:layout_width="45dip" android:layout_height="45dip" android:layout_gravity="center" android:src="@drawable/drawer_menu_icon_night_nor" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingLeft="20dip" android:text="hello" android:textSize="25dip"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingLeft="20dip" android:text="hello" android:textSize="25dip"/> </LinearLayout>
简单的适配器
package com.skyfin.listview; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends BaseAdapter { LayoutInflater mInflater = null; List<Iteminfo> list = new ArrayList<Iteminfo>(); public MyAdapter(Context context, List<Iteminfo> list) { super(); this.mInflater = LayoutInflater.from(context); this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.i("hello", "123"); TextView titleTextView = null; TextView textView = null; ImageView img = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.item4, null); titleTextView = (TextView) convertView.findViewById(R.id.title); textView = (TextView) convertView.findViewById(R.id.text); img = (ImageView)convertView.findViewById(R.id.iamgeview); } titleTextView.setText(list.get(position).getTitle()); textView.setText(list.get(position).getText()); img.setImageResource(list.get(position).getImage()); return convertView; } }
public class BaseAdapterActivity extends Activity { private ListView listview; private MyAdapter mydapter; private String[] ListViewArray = { "skyfin", "locojyw", "facker", "gad" }; private String[] ListViewArrayNum = { "2013", "2014", "2015", "2016" }; private int[] ListViewImageArray = { R.drawable.drawer_menu_icon_quest_nor, R.drawable.drawer_menu_icon_opensoft_nor, R.drawable.drawer_menu_icon_blog_nor, R.drawable.drawer_menu_icon_gitapp_nor }; List<Iteminfo> listIteminfos = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.list4); listview = (ListView) findViewById(R.id.list4); listIteminfos = new ArrayList<Iteminfo>(); for(int i =0;i<ListViewArray.length;i++){ Iteminfo iteminfo = new Iteminfo(ListViewArray[i], ListViewArrayNum[i], ListViewImageArray[i]); listIteminfos.add(iteminfo); } mydapter = new MyAdapter(this, listIteminfos); listview.setAdapter(mydapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(getApplicationContext(), ListViewArray[position],Toast.LENGTH_LONG).show(); } }); } }
5.自定义布局ArrayAdapter
ArrayAdapter是BaseAdapter的子类,ArrayAdapter不仅具有BaseAdapter的所有方法还自定义了一些新的方法来处理列表项,所以单纯的从功能能上来讲ArrayAdapter远远强与BaseAdapter,如果是绘制一些数量比较少的列表建议使用BaseAdapter 如果绘制一些比较复杂的列表项 并且列表项很多的 建议使用ArrayAdapter。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60dip" android:orientation="horizontal" > <ImageView android:id="@+id/photo" android:layout_width="45dip" android:layout_height="45dip" android:layout_gravity="center" android:src="@drawable/drawer_menu_icon_night_nor" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingLeft="20dip" android:text="hello" android:textSize="25dip"/> </LinearLayout>
package com.skyfin.listview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MyArrayAdapter extends ArrayAdapter<Object> { private int[] ListViewImageArray = { R.drawable.drawer_menu_icon_quest_nor, R.drawable.drawer_menu_icon_opensoft_nor, R.drawable.drawer_menu_icon_blog_nor, R.drawable.drawer_menu_icon_gitapp_nor }; int mTextViewResourceID = 0; private Context mContext; public MyArrayAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); mTextViewResourceID = textViewResourceId; mContext = context; } private int[] colors = new int[] { 0xff626569, 0xff4f5257 }; public int getCount() { return 4; } @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; Button button = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( mTextViewResourceID, null); iamge = (ImageView) convertView.findViewById(R.id.photo); button = (Button)convertView.findViewById(R.id.btn); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(getContext(),"您点击的第"+position +"个按钮", Toast.LENGTH_LONG).show(); } }); } iamge.setImageResource(ListViewImageArray[position]); return convertView; } }
public class ArrayAdapterActivity extends Activity { private ListView listview; private MyArrayAdapter mydapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.list5); listview = (ListView) findViewById(R.id.list5); mydapter = new MyArrayAdapter(this, R.layout.item5); listview.setAdapter(mydapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(getApplicationContext(), position,Toast.LENGTH_LONG).show(); } }); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。