Android 在适配器中添加固定两行,使其随适配器一起移动
Android 在适配器中添加固定两行,使其随适配器一起移动
正常情况下,适配器肯定只是显示同种数据,直接传入数据列表即可。但在某些需求时候,要求适配器必须有固定的一行或者两行,这两行数据格式还和正常的适配器list不同,超出屏幕时还要和适配器一起移动。这就要求我们需要去改造这个适配器,修改其显示的布局与逻辑来实现需求。
首先,我们来看看最后的实现效果。
图中的系统消息和小秘书是在适配器中的,最下面的是正常的适配器行,显示的是最新历史消息的。当没有聊天记录的时候,系统消息和小秘书也会这样显示,当聊天记录过多时,上下滑动屏幕的时候随着适配器一起移动。
接下来,我们先看适配器的布局实现。
由于是从正式的项目直接拷贝出来的,布局代码比较多。其实在我们合并起来后,你会发现就只有三部分。
上面图片就是下面整个布局合并起来的。适配器布局中包含了系统消息,小秘书,最近历史消息item三个Relativelayout。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--System Message--> <RelativeLayout android:id="@+id/system_linearlayout" android:background="@color/white" android:layout_width="match_parent" android:layout_height="55dp" android:paddingLeft="10dp"> <ImageView android:id="@+id/image1" android:layout_width="45dp" android:layout_height="45dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/system_info" /> <LinearLayout android:id="@+id/linearLayout31" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/image1" android:orientation="vertical"> <TextView android:layout_width="270dp" android:layout_height="wrap_content" android:layout_marginLeft="20px" android:ellipsize="end" android:singleLine="true" android:text="System Message" android:textSize="13sp" /> <TextView android:id="@+id/system_name" android:layout_width="270dp" android:layout_height="wrap_content" android:layout_marginLeft="20px" android:ellipsize="end" android:singleLine="true" android:textColor="@color/gray" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:layout_marginEnd="5dp" android:layout_marginRight="10dp" android:orientation="vertical"> <TextView android:id="@+id/system_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/gray" android:textSize="10sp" /> <TextView android:id="@+id/chatCount1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="5dp" android:background="@drawable/circular" android:gravity="center" android:textColor="@color/white" android:textSize="10sp" /> </LinearLayout> </RelativeLayout> <!--小秘书--> <RelativeLayout android:layout_below="@+id/system_linearlayout" android:background="@color/white" android:id="@+id/help_linearlayout" android:layout_width="match_parent" android:layout_height="55dp" android:paddingLeft="10dp"> <ImageView android:id="@+id/help_image" android:layout_width="45dp" android:layout_height="45dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/help_image" /> <LinearLayout android:id="@+id/help_linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/help_image" android:orientation="vertical"> <TextView android:layout_width="270dp" android:layout_height="wrap_content" android:layout_marginLeft="20px" android:ellipsize="end" android:singleLine="true" android:text="@string/help_desk" android:textSize="13sp" /> <TextView android:id="@+id/help_name" android:layout_width="270dp" android:layout_height="wrap_content" android:layout_marginLeft="20px" android:ellipsize="end" android:singleLine="true" android:textColor="@color/gray" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:layout_marginEnd="5dp" android:layout_marginRight="10dp" android:orientation="vertical"> <TextView android:id="@+id/help_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/gray" android:textSize="10sp" /> <TextView android:id="@+id/help_chatCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="5dp" android:background="@drawable/circular" android:gravity="center" android:textColor="@color/white" android:textSize="10sp" android:visibility="gone" /> </LinearLayout> </RelativeLayout> <!--最近历史消息item--> <RelativeLayout android:layout_below="@+id/help_linearlayout" android:background="@color/white" android:id="@+id/historyListView" android:layout_width="match_parent" android:layout_height="55dp" android:paddingLeft="10dp"> <RelativeLayout android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true"> <ImageView android:id="@+id/image_icon" android:layout_width="45dp" android:layout_height="45dp" android:layout_alignParentLeft="true" android:background="@drawable/un" /> <TextView android:id="@+id/friend_img1" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/bg_circle_blue" android:gravity="center_vertical|center" android:textColor="@color/white" android:textSize="13sp" /> </RelativeLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/image" android:orientation="vertical"> <TextView android:id="@+id/NaturalName" android:layout_width="270dp" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:ellipsize="end" android:singleLine="true" android:textSize="13sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/aitData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20px" android:layout_marginTop="5dp" android:ellipsize="end" android:singleLine="true" android:text="@string/ait_msg" android:textColor="@color/red" android:textSize="10sp" android:visibility="gone" /> <TextView android:id="@+id/MessageData" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_marginTop="5dp" android:ellipsize="end" android:singleLine="true" android:textColor="@color/gray" android:textSize="10sp" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/official" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/linearLayout3" android:layout_toEndOf="@+id/linearLayout3" android:background="@drawable/official" android:gravity="center" android:textColor="@color/gray" android:textSize="15px" android:visibility="gone" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:layout_marginEnd="5dp" android:layout_marginRight="10dp" android:orientation="vertical"> <TextView android:id="@+id/Data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/gray" android:textSize="10sp" /> <TextView android:id="@+id/chatCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="5dp" android:background="@drawable/circular" android:gravity="center" android:textColor="@color/white" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:id="@+id/delete" android:layout_width="60dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:background="@color/red" android:gravity="center" android:visibility="gone"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/chat_delete" android:textColor="@color/white" android:textSize="13sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
布局好了,我们再看怎么来初始化适配器数据
由于系统消息和小秘书需要放置在适配器中,我们需要为这两行创建特定的数据放入正常的最近历史消息list中,便于后面在适配器中根据这两个特定数据去控制显示的逻辑。
private void initData() { String user_Id = AppPrefs.get(getActivity()).getChatLogn().UserId; chatMsgAllList = dbManager.getChatMegAllByUserId(user_Id); //此处为获取正常的最近历史消息 Collections.sort(chatMsgAllList); //list排序 ChatMsg chatMsg = new ChatMsg(); chatMsg.setName("sys_msg"); //创建系统消息值 chatMsgAllList.add(0, chatMsg); //放入chatMsgAllList第一个 ChatMsg chatMsg1 = new ChatMsg(); chatMsg1.setName("help_desk"); //创建小秘书值 chatMsgAllList.add(1, chatMsg1); //放入chatMsgAllList第二个 customerServiceResultAdapter = new ChatHRecentHistorysAdapter(mContext, chatMsgAllList); resultListView.setAdapter(customerServiceResultAdapter); }
初始化数据好了,就是适配器的显示逻辑控制
从代码中我们能看到:当item的getName的值是sys_msg时,显示系统消息,隐藏小秘书item和最近消息记录item;当item的getName的值是help_desk时,显示小秘书item,隐藏系统消息item和最近消息记录item;当item的getName的值不是sys_msg和help_desk时,说明是正常的最近消息,那么显示最近消息记录item,隐藏小秘书item和系统消息item。
这里必须添加此逻辑,不然就会导致显示很多重复的item。
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.cell_customer, null); holder = new ChatHRecentHistorysAdapter.ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final ChatMsg item = mList.get(position); if (item != null) { if ("sys_msg".equals(item.getName())) { //系统消息行显示 holder.system_linearlayout.setVisibility(View.VISIBLE); initSysteInfo(holder); holder.historyListView.setVisibility(View.GONE); holder.help_linearlayout.setVisibility(View.GONE); } else if ("help_desk".equals(item.getName())) { //小秘书行显示 holder.help_linearlayout.setVisibility(View.VISIBLE); holder.historyListView.setVisibility(View.GONE); holder.system_linearlayout.setVisibility(View.GONE); } else { holder.system_linearlayout.setVisibility(View.GONE); holder.help_linearlayout.setVisibility(View.GONE); holder.historyListView.setVisibility(View.VISIBLE); ChatMessage chatMessage = item.getChatMessage(); //...........................这下面就是正常的最近历史消息的显示..................................................... } } return convertView; }
至此,我们的在适配器中添加固定两行,使其随适配器一起移动的功能就基本完成了。而我们要添加item的点击事件,直接resultListView.setOnItemClickListener即可,这个方法能监控到我们到底是点了那个item,自己分别实现各自的事件即可。
- Google TV box:连接固定电视和Android移动设备的一座新桥梁
- Android ListView GridView添加 padding后padding部分不跟随滑动移动问题解决方案
- Android任意窗口添加固定/浮动窗体:音乐播放器底部/顶部常驻播放窗体
- Android基于box2d开发弹弓类游戏[六]-------------移动的镜头&添加目标
- android 怎么给listview添加一个固定表头以及显示像table的效果
- Android-->View.setX()和.setY()的使用(移动布局,设置布局位置,动态添加View)
- Android基于box2d开发弹弓类游戏[六]-------------移动的镜头&添加目标
- android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果
- Android 使用SeekBar时动态显示进度且随SeekBar一起移动
- Android之画小球图实例[后续添加小球移动功能]
- Android使用SeekBar时动态显示进度且随SeekBar一起移动
- 一起学android之给图片添加涂鸦(文字)(37)
- android 百度地图 marker固定在屏幕中心,动态获取可移动地图的中心坐标
- Android使用SeekBar时动态显示进度且随SeekBar一起移动
- Android中关于GrideView,ListView的getView中position=0,错乱的问题。(在GridView的最后面添加两个固定item)
- Android中TextView中添加的文本上下两行又重叠
- Android实现动态添加tab并点击移动tab背景
- android: EditText 输入固定位数后光标自动移动到下一EditText
- Android任意窗口添加固定/浮动窗体:音乐播放器底部/顶部常驻播放窗体
- Android使用SeekBar时动态显示进度且随SeekBar一起移动