Android ListView自定义Adapter实现仿QQ界面

下面是详细讲解“Android ListView自定义Adapter实现仿QQ界面”的完整攻略。

简介

在Android开发中,ListView是常见的视图控件之一,用来展示一系列的元素。而自定义Adapter可以让我们更加灵活地设置ListView中的每一个Item的布局和内容。本文将介绍如何使用自定义Adapter,实现具有聊天界面中消息气泡特效的QQ界面。

步骤

步骤一:准备布局文件

我们先来创建聊天气泡的布局文件,包括左右两种对话框样式,并用drawable文件分别设置对话框的背景。其中,左对话框样式设置在left_chat_layout.xml中,右对话框样式设置在right_chat_layout.xml中,示例如下:

<!--left_chat_layout.xml-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/left_chat_bg"
    android:orientation="horizontal"
    android:padding="10dp" >
    <TextView
        android:id="@+id/left_msg_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white" />
</LinearLayout>

<!--right_chat_layout.xml-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/right_chat_bg"
    android:layout_gravity="end"
    android:orientation="horizontal"
    android:padding="10dp" >
    <TextView
        android:id="@+id/right_msg_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white" />
</LinearLayout>

步骤二:创建自定义Adapter

接下来我们需要将上面这两个布局作为ListView的Item使用。我们可以通过继承BaseAdapter或ArrayAdapter来实现自定义Adapter,这里我们选择继承BaseAdapter。

public class ChatListAdapter extends BaseAdapter {
    private List<ChatMessage> chatMsgList;
    private LayoutInflater inflater;

    public ChatListAdapter(Context context, List<ChatMessage> msgList) {
        this.chatMsgList = msgList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return chatMsgList.size();
    }

    @Override
    public Object getItem(int position) {
        return chatMsgList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatMessage chatMsg = chatMsgList.get(position);
        ViewHolder viewHolder = null;
        if(convertView == null) { // 没有缓存的view
            viewHolder = new ViewHolder();
            if(chatMsg.isComMeg()) { // 根据是否是对方消息来显示不同的item布局
                convertView = inflater.inflate(R.layout.left_chat_layout, null);
                viewHolder.msgTv = convertView.findViewById(R.id.left_msg_tv);
            } else {
                convertView = inflater.inflate(R.layout.right_chat_layout, null);
                viewHolder.msgTv = convertView.findViewById(R.id.right_msg_tv);
            }
            convertView.setTag(viewHolder); // 缓存viewHolder对象
        } else { // 有缓存的view,直接取出缓存的viewHolder对象
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.msgTv.setText(chatMsg.getMsg()); // 设置消息内容

        return convertView;
    }

    class ViewHolder {
        public TextView msgTv;
    }
}

步骤三:设置ListView的Adapter

最后一步是将Adapter与ListView关联起来:

ListView chatListView = findViewById(R.id.lv_chat);
List<ChatMessage> msgList = initChatMsgList(); // 从服务器读取聊天消息列表
ChatListAdapter adapter = new ChatListAdapter(this, msgList);
chatListView.setAdapter(adapter);

这样,我们就完成了一个仿QQ聊天界面的ListView自定义Adapter实现。

示例说明

示例一:在Activity中使用ListView

public class MainActivity extends AppCompatActivity {

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

        ListView chatListView = findViewById(R.id.lv_chat);
        List<ChatMessage> msgList = initChatMsgList(); // 从服务器读取聊天消息列表
        ChatListAdapter adapter = new ChatListAdapter(this, msgList);
        chatListView.setAdapter(adapter);
    }

    // 初始化消息列表
    private List<ChatMessage> initChatMsgList() {
        List<ChatMessage> msgList = new ArrayList<>();
        msgList.add(new ChatMessage(true, "你好,很高兴见到你!"));
        msgList.add(new ChatMessage(false, "同样很高兴见到你!"));
        return msgList;
    }
}

示例二:在Fragment中使用ListView

public class ChatFragment extends Fragment {
    private ListView chatListView;
    private ChatListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, container, false);
        chatListView = view.findViewById(R.id.lv_chat);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<ChatMessage> msgList = initChatMsgList(); // 从服务器读取聊天消息列表
        adapter = new ChatListAdapter(getActivity(), msgList);
        chatListView.setAdapter(adapter);
    }

    // 初始化消息列表
    private List<ChatMessage> initChatMsgList() { ... }
}

以上就是“Android ListView自定义Adapter实现仿QQ界面”的完整攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android ListView自定义Adapter实现仿QQ界面 - Python技术站

(0)
上一篇 2023年5月23日
下一篇 2023年5月23日

相关文章

  • JDBC SQL语法

    JDBC SQL语法可以分为四个部分:数据定义语言(DDL)、数据查询语言(DQL)、数据操纵语言(DML)和数据控制语言(DCL)。 数据定义语言 数据定义语言(DDL)用于定义和管理数据库对象,例如表、视图和索引等。常用的DDL语句有: CREATE CREATE用于创建数据库中的新对象,可以用来创建以下内容: 创建新表 创建新的视图 创建存储过程 创建…

    Java 2023年5月20日
    00
  • javascript forEach函数实现代码

    JavaScript中的forEach()函数,是一种迭代数组中每个元素的方式,是一种可以使代码更清爽、高效的编程技巧。下面是详细讲解Javascript forEach函数实现代码的完整攻略,包含了基本语法、示例说明以及实际应用场景。 基本语法 forEach()函数是JavaScript中的一个方法,用于迭代一个数组,遍历每个元素并且对其执行一个指定的操…

    Java 2023年6月15日
    00
  • 什么是垃圾回收?

    以下是关于垃圾回收的完整使用攻略: 什么是垃圾回收? 垃圾回收是指在程序运行过程中,自动回收不再使用的内存空间,从而避免内存泄漏和内存溢出。垃圾回收是一种自动化的内存管理方式,可以减少程序员的工作量,提高程序的可靠性和安全性。 垃圾回收的原理 垃圾回收的原理主要有以下几点: 1. 标记清除算法 标记清除算法是垃圾回收的一种常见算法,它的原理是在程序运行过程中…

    Java 2023年5月12日
    00
  • java8 Stream API之reduce使用说明

    Java8 Stream API之reduce使用说明 简介 reduce() 是 Stream API 的一个终端操作,它能够将 stream 中所有元素反复结合起来,得到一个最终值。 语法 Optional<T> reduce(BinaryOperator<T> accumulator); T reduce(T identity,…

    Java 2023年5月26日
    00
  • TOMCAT+IIS配置方法

    下面是 “TOMCAT+IIS配置方法” 的完整攻略: 前置条件 安装好 TOMCAT 及 IIS,并且都能正常启动。 配置步骤 步骤一:修改 IIS 默认端口 为了确保 IIS 和 TOMCAT 能够同时运行,我们需要将 IIS 默认端口从 80 改为其他端口(如:8080)。 打开 IIS 管理器。 点击左边菜单栏的“默认网站”,然后在右边窗口中找到“基…

    Java 2023年5月19日
    00
  • springboot使用shiro-整合redis作为缓存的操作

    Spring Boot使用Shiro整合Redis作为缓存的操作 在Spring Boot应用程序中,我们可以使用Apache Shiro框架来实现安全认证和授权功能。同时,我们也可以使用Redis作为Shiro的缓存存储。在本文中,我们将详细介绍如何使用Shiro整合Redis作为缓存的操作,并提供两个示例说明。 步骤分析 在Spring Boot应用程序…

    Java 2023年5月18日
    00
  • Java微信公众号开发之通过微信公众号获取用户信息

    Java微信公众号开发之通过微信公众号获取用户信息 简介 本文将详细讲解如何通过Java实现微信公众号获取用户信息细节,包括获取用户基本信息和获取关注者列表,最后提供两条常用的示例说明。 准备工作 在开始获取用户信息之前,你需要进行以下步骤的准备工作: 注册微信公众号,并获取关注者OpenID和Access Token. 创建Java Web服务器,并引入相…

    Java 2023年5月26日
    00
  • Mac OS上安装Tomcat服务器的简单步骤

    下面我将为您详细介绍在Mac OS上安装Tomcat服务器的简单步骤。 1. 下载Tomcat 首先,在Apache Tomcat官网(http://tomcat.apache.org)下载Tomcat的二进制发行版。选择最新版本,下载Core的tar.gz版,解压到一个合适位置。 2. 配置环境变量 打开终端,输入以下命令添加环境变量: $ vim ~/.…

    Java 2023年5月19日
    00
合作推广
合作推广
分享本页
返回顶部