java 用于andorid的自定义适配器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 用于andorid的自定义适配器相关的知识,希望对你有一定的参考价值。

import java.util.List;


import android.content.Context;
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.TextView;


public class DrawerAdapter extends BaseAdapter {

	private Context context;
	private List<String> drawerEntries;
	private LayoutInflater mInflater;

	public DrawerAdapter(Context context, List<String> drawerEntries) {
		super();
		this.context = context;
		this.drawerEntries = drawerEntries;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return drawerEntries.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return drawerEntries.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_drawer, null);
			holder = new ViewHolder();
			holder.textViewItem = (TextView) convertView
					.findViewById(R.id.text_drawer_item);
			holder.iconItem = (ImageView)convertView.findViewById(R.id.image_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			holder.textViewItem.setText(drawerEntries.get(position));
			switch (position) {
			case 0:
				holder.iconItem.setImageResource(R.drawable.image);
				break;
			case 1:
				holder.iconItem.setImageResource(R.drawable.audio);
				break;
			case 2:
				holder.iconItem.setImageResource(R.drawable.video);
				break;
			case 3:
				holder.iconItem.setImageResource(R.drawable.voice_note);
				break;
			case 4:
				holder.iconItem.setImageResource(R.drawable.profile);
				break;
			case 5:
				holder.iconItem.setImageResource(R.drawable.plus_one_undone);
				convertView.setBackgroundColor(Color.parseColor("#D24127"));
				holder.textViewItem.setTextColor(Color.WHITE);
				break;
			case 6:
				holder.iconItem.setImageResource(R.drawable.ic_launcher);
				break;
			}
			
			if(HostActivity.menuPosition == position){
				setSelection(position, convertView);
			} else if (position != 5) {
				convertView.setBackgroundResource(android.R.color.white);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return convertView;
	}

	public class ViewHolder {
		TextView textViewItem;
		ImageView iconItem;
	}
	
	private void setSelection(int position, View convertView){
		switch (position) {
		case 0:
			convertView.setBackgroundResource(R.color.drawer_image);
			break;
		case 1:
			convertView.setBackgroundResource(R.color.drawer_audio);
			break;
		case 2:
			convertView.setBackgroundResource(R.color.drawer_video);
			break;
		case 3:
			convertView.setBackgroundResource(R.color.drawer_voice);
			break;
		case 4:
			convertView.setBackgroundResource(R.color.drawer_profile);
			break;
		}
	}
	
}

以上是关于java 用于andorid的自定义适配器的主要内容,如果未能解决你的问题,请参考以下文章

如何将最新数据附加到android中的自定义基本适配器列表视图?

片段中 ListView 的自定义适配器不起作用

Java 数据类型的自定义 JSON 序列化

使用自定义适配器的自定义对象

ListView 的自定义适配器忽略设置值

Android 中的自定义适配器