jianjunhuang's blog

无聊写写博客

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

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

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

public int getItemViewType(int position){

return position;

}

//一共有多少个布局

public int getViewTypeCount(int position){

return position;

}

演示图片:


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

下面是代码演示:

item布局文件:

<?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="wrap_content"
    android:orientation="vertical" >

    <!-- 平时的item -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:background="#FFFFFF">

        <!-- 时间显示 -->

        <RelativeLayout
            android:id="@+id/timeInormation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.8"
            android:gravity="center_vertical"
            android:orientation="vertical" >

          
            <!-- 月 -->
<!--  <TextView
    			android:visibility="invisible"
                android:id="@+id/tv_notification_Today"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="9dp"
                android:layout_marginTop="25dp"
                android:text="Today"
                android:textColor="#60c226"
                android:textSize="15sp" />  -->
            <TextView
                android:id="@+id/tv_notification_other_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="25dp"
                android:text="March"
                android:textColor="#60c226"
                android:textSize="10sp" />
            <!-- 号 -->

            <LinearLayout
                android:id="@+id/notification_other_day_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_notification_other_month"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="3dp"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_notification_other_day"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="19"
                    android:textColor="#60c226"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_notification_other_suffic"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="th"
                    android:textColor="#60c226"
                    android:textSize="15sp" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_notification_other_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/notification_other_day_layout"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:text="7:00pm"
                android:textColor="#484848"
                android:textSize="9sp" />
        </RelativeLayout>
        <!-- 间隔线 -->

        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#f1f1f1" />
        <!-- 通知显示 -->

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:layout_weight="8"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12.5dp"
                    android:src="@drawable/icon_news" />

                <TextView
                    android:id="@+id/tv_notification_other_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="迎新系统讨论会"
                    android:textColor="#666666"
                    android:textSize="20sp" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_notification_other_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12.5dp"
                android:layout_marginRight="12.5dp"
                android:layout_marginTop="15dp"
                android:text="开会总结:总结一下。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。"
                android:textColor="#808080"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
另一个item布局文件
<?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="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layout_today"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
         android:background="#FFFFFF">

        <!-- 时间显示 -->

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.8"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_notification_Today"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="25dp"
                android:text="Today"
                android:textColor="#60c226"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_notification_time_today"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="7:00pm"
                android:textColor="#484848"
                android:textSize="9sp" />
        </LinearLayout>
        <!-- 间隔线 -->

        <View
            
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#F1F1F1" />
        <!-- 通知显示 -->

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:layout_weight="8"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_marginTop="2dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12.5dp"
                    android:src="@drawable/icon_news" />

                <TextView
                    android:id="@+id/tv_notification_title_today"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="迎新系统讨论会"
                    android:textColor="#666666"
                    android:textSize="20sp" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_notification_content_today"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12.5dp"
                android:layout_marginRight="12.5dp"
                android:layout_marginTop="15dp"
                android:text="开会总结:总结一下。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。"
                android:textColor="#808080"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
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<Map<String, Object>> 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<Map<String, Object>>();

		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<Map<String, Object>> getNotificationData() {
		for (int i = 0; i < 5; i++) {
			Map<String, Object> notification_map = new HashMap<String, Object>();
			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<Map<String, Object>> 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的方法按位置返回一个标志,在通过该标志选择加载哪一个布局和相应的数据就可以了,这样就可以在不同的位置显示不同的布局。


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

阅读更多

扫码向博主提问

去开通我的Chat快问

qq_31876841

非学,无以致疑;非问,无以广识
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31876841/article/details/50321241
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭