ListView的使用方法
package com.example.listview_xie;
import com.example.listview_xie.Color_ListView.Color_ListView;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_menu);
Button bn_simple = (Button)findViewById(R.id.button_simple);
Button bn_title = (Button)findViewById(R.id.button_title);
Button bn_picture = (Button)findViewById(R.id.butto_picture);
Button bn_color = (Button)findViewById(R.id.butto_color);
bn_simple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent _intent = new Intent();
_intent.setClass(MainActivity.this,Simple_ListView.class );
startActivity(_intent);
}
});
bn_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent _intent = new Intent();
_intent.setClass(MainActivity.this,Title_ListView.class );
startActivity(_intent);
}
});
bn_picture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent _intent = new Intent();
_intent.setClass(MainActivity.this,Picture_Listview.class);
startActivity(_intent);
}
});
bn_color.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent _intent = new Intent();
_intent.setClass(MainActivity.this,Color_ListView.class);
startActivity(_intent);
}
});
}
}
layout_menu.xml
<?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" >
<Button
android:id="@+id/button_simple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单ListView"/>
<Button
android:id="@+id/button_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="有标题ListView"/>
<Button
android:id="@+id/butto_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带图片的ListView"/>
<Button
android:id="@+id/butto_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义颜色的ListView"/>
</LinearLayout>
package com.example.listview_xie;
//简单的ListView
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
//简单的ListView
import android.widget.ListView;
import android.widget.Toast;
public class Simple_ListView extends ListActivity {
private String[] myListStr = {"姓名:伊斯坦堡","性别:男","年龄:21","居住地:广东徐闻","邮箱:1396994724@qq.com"};
ListView mylistview =null; //定义一个ListView对象
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mylistview = getListView();
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myListStr));
mylistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(Simple_ListView.this,"您选择了" + myListStr[position], Toast.LENGTH_LONG).show();
}
});
}
}
//public void onItemClick (AdapterView<?> parent, View view, int position, long id )
//parent 相当于listview Y适配器的一个指针,可以通过它来获得Y里装着的一切东西,再通俗点就是说告诉你,你点的是Y,不是X - -
//view 是你点b item的view的句柄,就是你可以用这个view,来获得b里的控件的id后操作控件
//position 是b在Y适配器里的位置(生成listview时,适配器一个一个的做item,然后把他们按顺序排好队,在放到listview里,
//意思就是这个b是第position号做好的)
//id 是b在listview Y里的第几行的位置(很明显是第2行),大部分时候position和id的值是一样的,如果需要的话,你可以自己加个
//log把position和id都弄出来在logcat里瞅瞅,看了之后心里才踏实。
2.带标题的ListView列表
package com.example.listview_xie;
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.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
//有标题的ListView
public class Title_ListView extends ListActivity{
private String[] Titlelist = {"姓名","性别","年龄","居住地","邮箱"};
private String[] Titlestr = {"伊斯坦堡","男","21","广东徐闻","1396994724@qq.com"};
ListView mylistview = null;
ArrayList<Map<String, Object>> Data = new ArrayList<Map<String,Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mylistview = getListView();
int length = Titlelist.length;
for(int i=0;i<length;i++) {
Map<String , Object> item = new HashMap<String,Object>();
item.put("title", Titlelist[i]);
item.put("text", Titlestr[i]);
Data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,Data,android.R.layout.simple_list_item_2,
new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
mylistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(Title_ListView.this,"您选择了标题:" + Titlelist[position] + "内容:"+Titlestr[position], Toast.LENGTH_LONG).show();
}
});
}
}
3.带图片的ListView列表
<?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>
Picture_ListView.java
package com.example.listview_xie;
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 Picture_Listview extends ListActivity {
private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};
private String[] mListStr = { "伊斯坦堡", "男", "21", "广东徐闻",
"1396994724@qq.com" };
ListView mListView = null;
ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;
@Override
protected void onCreate(Bundle 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.jayzhou);
item.put("title", mListTitle[i]);
item.put("text", mListStr[i]);
mData.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.picture,
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(Picture_Listview.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show();
}
});
super.onCreate(savedInstanceState);
}
}
4.自定义布局BaseAdapter修改列表颜色
因为通过直接 构造系统的布局来绘制列表方法肯定是有限的,所以我们需要重写绘制方法 ,写一个类去继承BaseAdapter 并实现这个类中的方法,listView在一开始绘制的时候首先会调用getCout()方法得到绘制次数 ,然后会实例化自己定义的BaseAdapter通过getView()方法一层一层绘制ListView,所以我们可以在这里面根据position(当前绘制的ID)来任意的修改绘制的内容,做出好看漂亮的ListView,下面这个例子我通过重写getView修改每个列表的颜色 并且实现用户选中后成高亮状态。
<?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_alignParentBottom="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>
Color_ListView.java
package com.example.listview_xie.Color_ListView;
import com.example.listview_xie.R;
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 Color_ListView extends ListActivity {
private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};
private String[] mListStr = { "伊斯坦堡", "男", "21", "广东徐闻",
"xuanyusong@gmail.com" };
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
protected void onCreate(Bundle 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(Color_ListView.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show();
}
});
super.onCreate(savedInstanceState);
}
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.clolor, 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.jayzhou);
return convertView;
}
private Context mContext;
}
}
源码下载地址:http://download.csdn.net/detail/qq_27384607/8957013