整理::ListView的各种隔行换色法

隔行换色 ListView:1.使用BaseAdapter

package com.xsjayz.listview0822;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity extends Activity {

	private String[] moreNames = null;
	private ListView listView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 在string中定义的字符串数组
		moreNames = getResources().getStringArray(R.array.list_array);
		// 在main.xml中定义的listview
		listView = (ListView) findViewById(R.id.listViewMoreInfo);

		// 隐藏ListView的分隔线
		listView.setDivider(null);
		listView.setAdapter(baseAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> view, View arg1,
					int position, long id) {

				switch (position) {
				case 0:
					toastUtil(ListViewActivity.this, position);
					break;
				case 1:
					toastUtil(ListViewActivity.this, position);
					break;
				case 2:
					toastUtil(ListViewActivity.this, position);
					break;
				case 3:
					toastUtil(ListViewActivity.this, position);
					break;
				case 4:
					toastUtil(ListViewActivity.this, position);
					break;
				case 5:
					toastUtil(ListViewActivity.this, position);
					break;
				case 6:
					toastUtil(ListViewActivity.this, position);
					break;
				}
			}
		});
	}

	/**
	 * 由于使用了大量的Toast,所以这里将这些Toast封装成一个方法
	 * 
	 * @param context
	 * @param position
	 */
	public void toastUtil(Context context, int position) {
		Toast.makeText(context, moreNames[position], 3000).show();
	}

	/**
	 * 自定义BaseAdapter,返回隔行换色的视图显示在ListView上。
	 */
	BaseAdapter baseAdapter = new BaseAdapter() {
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView = null;
			TextView textView = null;
			// 获得布局文件more_info_my.xml,里面定义了TextView和ImageView两个组件
			convertView = LayoutInflater.from(ListViewActivity.this).inflate(
					R.layout.more_info_my, null);

			textView = (TextView) convertView.findViewById(R.id.mytxt);
			imageView = (ImageView) convertView.findViewById(R.id.myimage);
			// 设置ListView显示的文字内容和图片
			textView.setText(moreNames[position]);
			imageView.setImageResource(R.drawable.moreinfo_extend_next);
			// 为ListView设置隔行不同的背景颜色
			if (position % 2 == 0) {
				convertView.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.list_item_2));
			} else {
				convertView.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.list_item_1));
			}
			// 返回的convertView对象将作为ListView的列表项
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public int getCount() {
			return moreNames.length;
		}
	};
}

隔行换色 ListView:2.重写SimpleAdapter



import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class SpecialAdapter extends SimpleAdapter {
    private int[] colors = new int[] { 0x30FF0000, 0x300000FF };

    public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
        super(context, items, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      int colorPos = position % colors.length;
      view.setBackgroundColor(colors[colorPos]);
      return view;
    }
}

Activity 引用:

SpecialAdapter adapter = new SpecialAdapter(this,fillMaps,R.layout.grid_item,from,to);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值