HorizontalScrollView仿QQ侧滑删除

本文详细介绍了如何使用HorizontalScrollView在Android中实现QQ侧滑删除的效果,包括需求分析、布局设置、滑动控制以及多点触控的处理。关键步骤包括设置Item宽度、监听滑动事件、处理滑动距离和点击事件,最后提供了源码下载链接。
摘要由CSDN通过智能技术生成

效果:

需求:

不论什么领域,在模仿一个东西的时候,我们首先要对它进行需求提取,这样才能保证做到”惟妙惟肖”。通过对QQ侧滑功能的分析,提取出了以下需求:

  1. 每个Item都可以侧滑,根据Item类型的不同,侧滑后显示的菜单项也不同(联系人/群组的菜单有:置顶,标为已读,删除, 通知类消息展示的菜单只有置顶和删除);
  2. 侧滑的过程中,如果滑动距离超过第一个菜单的宽度,抬起手指时会显示全部的菜单,即Item会滑动到最左端;
  3. 在向右滑动关闭菜单的过程中,如果滑动距离超过最后一个菜单的宽度,抬起手指时会关闭全部菜单, 即Item会恢复至正常展示状态;
  4. 如果Item的菜单呈展开状态,则点击此Item或按下其他Item,当前的Item的菜单将会关闭;
  5. 如果没有Item的菜单呈展开状态,点击Item时将进入聊天页面;
  6. 观察Item滑动的过程,发现其是匀速滑动, 而不是快速移动;
  7. 不能同时滑动多个Item;

通过对需求的分析,首先会想到HorizontalScrollView, 当然,重写Item的RooView的onTouchListener()也可以实现,但是普通的View只有scrollTo()和scrollBy()方法, 只能快速移动而不能匀速移动,导致滑动的过程很生硬。所以我们使用HorizontalScrollView来实现我们的效果。

* 布局:*

根布局其实没什么内容,就是一个ListView,这样就不贴代码了, 下面我们主要展示一下Item的布局内容:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/horizontal_scrollview"
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:fillViewport="true"
    android:scrollbars="none">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center">
        <LinearLayout
            android:id="@+id/content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:padding="12dp">
            <ImageView
                android:id="@+id/icon"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@mipmap/ic_launcher"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="12dp"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/name_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Freeman"
                    android:textSize="15sp"
                    android:textColor="#333333"/>
                <TextView
                    android:id="@+id/content_text"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值