Android ListView自定义和选中效果
1.自定义列表框的每项layout文件夹中创建xml文件,在自定义的adapter的getview方法中加载。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:weightSum="1">
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="40px"
- android:id="@+id/layout"
- android:padding="6px">
- <ImageView android:id="@+id/icon"
- android:layout_width="18dip"
- android:layout_height="18dip"
- android:layout_marginLeft="2dip"
- android:layout_marginRight="2dip" />
- <TextView android:id="@+id/text"
- android:layout_gravity="center_horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </LinearLayout>
自定义adapter,继承自BaseAdapter,在getview方法中为当前项赋值并添加选中效果。
- import android.content.Context;
- import android.content.res.Resources;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class ListviewAdapter extends BaseAdapter{
- private LayoutInflater inflater = null;
- private ArrayList<String> items = null;
- private Bitmap icon;
- private int selectedPosition = -1;
- public void setSelectedPosition(int position) {
- selectedPosition = position;
- }
- public ListviewAdapter(Context context, ArrayList<String> arraylist) {
- // TODO Auto-generated constructor stub
- // LayoutInflater用来加载界面
- inflater = LayoutInflater.from(context);
- // 保存适配器中的每项的文字信息
- this.items = arraylist;
- // 获得资源中的图片作为要显示的图标
- Resources res = context.getResources();
- this.icon = BitmapFactory.decodeResource(res, R.drawable.icon);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return items.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return items.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- // 保存每项中的控件的引用
- class ViewHolder {
- TextView text;
- ImageView icon;
- LinearLayout layout;
- }
- @Override
- public View getView(int position, View convert, ViewGroup parent) {
- // TODO Auto-generated method stub
- ViewHolder holder;
- if(convert == null)
- {
- // 调用LayoutInflater的inflate方法加载layout文件夹中的界面
- convert = inflater.inflate(R.layout.list_row, null);
- holder = new ViewHolder();
- holder.text = (TextView)convert.findViewById(R.id.text);
- holder.icon = (ImageView)convert.findViewById(R.id.icon);
- holder.layout = (LinearLayout)convert.findViewById(R.id.layout);
- // 保存包含当前项控件的对象
- convert.setTag(holder);
- } else {
- // 获取包含当前项控件的对象
- holder = (ViewHolder)convert.getTag();
- }
- // 设置当前项的内容
- holder.text.setText(items.get(position));
- holder.icon.setImageBitmap(icon);
- // 设置选中效果
- if(selectedPosition == position)
- {
- holder.text.setTextColor(Color.BLUE);
- holder.layout.setBackgroundColor(Color.YELLOW);
- } else {
- holder.text.setTextColor(Color.WHITE);
- holder.layout.setBackgroundColor(Color.TRANSPARENT);
- }
- return convert;
- }
- }
添加列表框项目的点击事件,更新列表框实现选中效果的目的。
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.AdapterView.OnItemClickListener;
- public class TestActivity extends Activity {
- private ListviewAdapter listAdapter;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 初始化列表框里显示的内容
- ArrayList<String> list = new ArrayList<String>();
- for(int i=1; i<20; i++)
- list.add("Line " + i);
- // 为列表框添加适配器
- ListView listview = (ListView)this.findViewById(R.id.listview);
- listAdapter = new ListviewAdapter(this, list);
- listview.setAdapter(listAdapter);
- // 添加列表框项目点击事件
- listview.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // 设置适配器的选中项
- listAdapter.setSelectedPosition(arg2);
- // 更新列表框
- listAdapter.notifyDataSetInvalidated();
- }
- });
- }
- }
为防止列表框滚动时背景会变黑,添加 android:cacheColorHint="#00000000" 属性。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:weightSum="1">
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Listview:"
- android:textSize="8pt"
- android:gravity="left" />
- <ListView android:id="@+id/listview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:cacheColorHint="#00000000" />
- </LinearLayout>