BaseAdapter-ListView中的item显示多种布局

想要在listview中的item添加多中布局主要是加多两个方法:

//按位置设置不同的布局

public int getItemViewType(int position){

return position;

}

//一共有多少个布局

public int getViewTypeCount(int position){

return position;

}

演示图片:


不要吐槽效果只是演示一下如何实现的,嗯!

下面是代码演示:

item布局文件:




    

    

        

        

          
            

            
            

            

                

                
            

            
        
        

        
        

        

            

                

                
            

            
        
    

另一个item布局文件



    

        

        

            

            
        
        

        
        

        

            

                

                
            

            
        
    

java文件:



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tentcoo.tentcooTools.R;
import com.tentcoo.tentcooTools.framework.BaseActivity;
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.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/*
*
*@auther Jianjun Huang
*
*@date 2015年12月4日
*/
public class Notification extends BaseActivity {

	private ListView notification_ListView;

	private TextView title;

	List> notification_List;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_main);

		title = (TextView) this.findViewById(R.id.tv_actionbar_title);
		title.setText("通知");
		// 建立数据
		notification_List = new ArrayList>();

		notification_ListView = (ListView) findViewById(R.id.lv_notification);

		Notification_Adapter notification_Adapter = new Notification_Adapter(this, getNotificationData());
		// 绑定适配器与listview
		notification_ListView.setAdapter(notification_Adapter);

	}

	// 数据
	private List> getNotificationData() {
		for (int i = 0; i < 5; i++) {
			Map notification_map = new HashMap();
			notification_map.put("key_today", "Today");
			notification_map.put("key_month", "January");
			notification_map.put("key_day", "" + i);
			notification_map.put("key_suffic", "th");
			notification_map.put("key_time", i + ":00pm");
			notification_map.put("key_title", "android交流会");
			notification_map.put("key_content", "开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!开会啊!");
			notification_List.add(notification_map);
		}
		return notification_List;
	}

	// BaesAdapter
	public class Notification_Adapter extends BaseAdapter {

		private LayoutInflater mInflater;

		private final int TODAY_STYLE = 0;

		private final int OTHER_STYLE = 1;

		public Notification_Adapter(Context context, List> list) {

			mInflater = LayoutInflater.from(context);
			// 初始化
			notification_List = list;

		}

		@Override
		public int getCount() {

			return notification_List.size();

		}

		@Override
		public Object getItem(int position) {

			return notification_List.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public int getItemViewType(int position) {
			// 按位置分配布局
			if (position == 0) {
				return TODAY_STYLE;
			} else {
				return OTHER_STYLE;
			}

		}

		// 布局类型数量
		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			int p = position;

			int type = getItemViewType(position);

			ViewHolder_Other viewHolder_Other = null;

			ViewHolder_Today viewHolder_Today = null;
			// 设置视图
			if (convertView == null) {

				viewHolder_Other = new ViewHolder_Other();

				viewHolder_Today = new ViewHolder_Today();

				// 确定布局
				switch (type) {
				case OTHER_STYLE:

					convertView = mInflater.inflate(R.layout.notification_item_other, null);

					viewHolder_Other.month = (TextView) convertView.findViewById(R.id.tv_notification_other_month);
					viewHolder_Other.day = (TextView) convertView.findViewById(R.id.tv_notification_other_day);
					viewHolder_Other.suffic = (TextView) convertView.findViewById(R.id.tv_notification_other_suffic);
					viewHolder_Other.time = (TextView) convertView.findViewById(R.id.tv_notification_other_time);
					viewHolder_Other.title = (TextView) convertView.findViewById(R.id.tv_notification_other_title);
					viewHolder_Other.content = (TextView) convertView.findViewById(R.id.tv_notification_other_content);

					convertView.setTag(viewHolder_Other);
					break;

				case TODAY_STYLE:

					convertView = mInflater.inflate(R.layout.notification_item_today, null);

					viewHolder_Today.today = (TextView) convertView.findViewById(R.id.tv_notification_Today);
					viewHolder_Today.time = (TextView) convertView.findViewById(R.id.tv_notification_time_today);
					viewHolder_Today.title = (TextView) convertView.findViewById(R.id.tv_notification_title_today);
					viewHolder_Today.content = (TextView) convertView.findViewById(R.id.tv_notification_content_today);

					convertView.setTag(viewHolder_Today);
					break;
				default:
					break;
				}
			} else {
				switch (type) {
				case OTHER_STYLE:
					viewHolder_Other = (ViewHolder_Other) convertView.getTag();
					break;
				case TODAY_STYLE:
					viewHolder_Today = (ViewHolder_Today) convertView.getTag();
				}

			}
			// 设置资源
			switch (type) {
			case OTHER_STYLE:

				viewHolder_Other.month.setText(notification_List.get(position).get("key_month").toString());
				viewHolder_Other.day.setText(notification_List.get(position).get("key_day").toString());
				viewHolder_Other.suffic.setText(notification_List.get(position).get("key_suffic").toString());
				viewHolder_Other.time.setText(notification_List.get(position).get("key_time").toString());
				viewHolder_Other.title.setText(notification_List.get(position).get("key_title").toString());
				viewHolder_Other.content.setText(notification_List.get(position).get("key_content").toString());
				break;
			case TODAY_STYLE:

				viewHolder_Today.today.setText(notification_List.get(position).get("key_today").toString());
				viewHolder_Today.time.setText(notification_List.get(position).get("key_time").toString());
				viewHolder_Today.title.setText(notification_List.get(position).get("key_title").toString());
				viewHolder_Today.content.setText(notification_List.get(position).get("key_content").toString());

			}
			// 设置透明度
			switch (p) {
			case 0:

				viewHolder_Today.today.setTextColor(Color.argb(255, 95, 194, 38));
				viewHolder_Today.time.setTextColor(Color.argb(255, 72, 72, 7));
				notifyDataSetChanged();

				break;
			case 1:
				viewHolder_Other.month.setTextColor(Color.argb(200, 95, 194, 38));
				viewHolder_Other.day.setTextColor(Color.argb(200, 95, 194, 38));
				viewHolder_Other.suffic.setTextColor(Color.argb(200, 95, 194, 38));
				viewHolder_Other.time.setTextColor(Color.argb(200, 72, 72, 72));
				notifyDataSetChanged();
				break;
			case 2:
				viewHolder_Other.month.setTextColor(Color.argb(150, 95, 194, 38));
				viewHolder_Other.day.setTextColor(Color.argb(150, 95, 194, 38));
				viewHolder_Other.suffic.setTextColor(Color.argb(150, 95, 194, 38));
				viewHolder_Other.time.setTextColor(Color.argb(150, 72, 72, 72));
				notifyDataSetChanged();
				break;

			default:
				viewHolder_Other.month.setTextColor(Color.argb(100, 95, 194, 38));
				viewHolder_Other.day.setTextColor(Color.argb(100, 95, 194, 38));
				viewHolder_Other.suffic.setTextColor(Color.argb(100, 95, 194, 38));
				viewHolder_Other.time.setTextColor(Color.argb(100, 72, 72, 72));
				notifyDataSetChanged();
			}

			return convertView;
		}

		class ViewHolder_Other {

			private TextView month;
			private TextView day;
			private TextView suffic;
			private TextView time;
			private TextView title;
			private TextView content;
		}

		class ViewHolder_Today {
			private TextView today;
			private TextView time;
			private TextView title;
			private TextView content;
		}

	}
}




其实最主要的就是通过getItemViewType的方法按位置返回一个标志,在通过该标志选择加载哪一个布局和相应的数据就可以了,这样就可以在不同的位置显示不同的布局。


源码中还有其他东西比较乱就不上传了,有什么不对的或更好的方法希望可以评论一下。

你可能感兴趣的:(Android)