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,自己分别实现各自的事件即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值