【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库

这篇具有很好参考价值的文章主要介绍了【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

不断学习,做更好的自己!💪

前言

侧滑菜单确实是一个比较常见的功能,其中场景较多的就是侧滑删除,它是iOS列表删除通用交互方式,比如微信、QQ、苹果自带的短信、通讯录列表等,都有侧滑删除功能。由于国内Android、iOS通常都是一套设计,因次,Android端怎么能少得了这个功能呢?Android 端实现起来确实稍显麻烦,它需要你掌握自定义View、属性动画、事件分发等一些比较深入的知识点。如果这些知识点你掌握得不错,那么实现一个侧滑菜单其实也不难。本文就讲讲实现思路和整理的一些不错的关于侧滑菜单的开源库。

实现思路

通过自定义 View 的方式实现步骤:
1、自定义 ViewGroup
2、在 onLayout 中,获取 childView 并对他们进行布局,这一步比较重要,content 占满屏幕,菜单View 在屏幕之外,当滑动的时候,content 滑屏幕,menu 进入屏幕,就达到了我需要的效果,布局草图如下:
【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库
3、重写 dispatchTouchEvent 和 onInterceptTouchEvent 方法拦截事件和处理滚动。滑动效果的实现既可以用 Scroller,也可以用属性动画 ValueAnimator。

侧滑菜单库

1、SwipeRevealLayout
SwipeRevealLayout 使用简单、代码入侵低,不但支持左右侧滑菜单,还支持上下滑出菜单。可以配合各种布局使用,包括 RecyclerView 、ListView、ScrollView 等,效果很赞
GitHub 地址: https://github.com/chthai64/SwipeRevealLayout

  • 效果图:
    【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库

  • 使用方式:

<com.chauthai.swipereveallayout.SwipeRevealLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:mode="same_level"
        app:dragEdge="left">

        <!-- Your secondary layout here -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <!-- Your main layout here -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
            
</com.chauthai.swipereveallayout.SwipeRevealLayout>
  • adapter class 中:
public class Adapter extends RecyclerView.Adapter {
  // This object helps you save/restore the open/close state of each view
  private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();
  
  public Adapter() {
    // uncomment the line below if you want to open only one row at a time
    // viewBinderHelper.setOpenOnlyOne(true);
  }
  
  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
    // get your data object first.
    YourDataObject dataObject = mDataSet.get(position); 
    
    // Save/restore the open/close state.
    // You need to provide a String id which uniquely defines the data object.
    viewBinderHelper.bind(holder.swipeRevealLayout, dataObject.getId()); 

    // do your regular binding stuff here
  }
}
private class ViewHolder extends RecyclerView.ViewHolder {
        private SwipeRevealLayout swipeLayout;
        private View frontLayout;
        private View deleteLayout;
        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            swipeLayout = (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout);
            frontLayout = itemView.findViewById(R.id.front_layout);
            deleteLayout = itemView.findViewById(R.id.delete_layout);
            textView = (TextView) itemView.findViewById(R.id.text);
        }

        public void bind(final String data) {
            deleteLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDataSet.remove(getAdapterPosition());
                    notifyItemRemoved(getAdapterPosition());
                }
            });

            textView.setText(data);

            frontLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String displayText = "" + data + " clicked";
                    Toast.makeText(mContext, displayText, Toast.LENGTH_SHORT).show();
                    Log.d("RecyclerAdapter", displayText);
                }
            });
        }
    }

2、SwipeDelMenuLayout
GitHub 地址:https://github.com/mcxtzhang/SwipeDelMenuLayout

和 SwipeRevealLayout 差不多。

  • 效果图:
    【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库

  • 使用方式:
    在 RecyclerView、ListView 可直接使用,在 Adapter 中,在 item 布局最外层包上
    SwipeMenuLayout 就好。

<?xml version="1.0" encoding="utf-8"?>
<com.mcxtzhang.swipemenulib.SwipeMenuLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:clickable="true"
    android:paddingBottom="1dp">

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        android:gravity="center"
        android:text="项目中我是任意复杂的原ContentItem布局"/>

    <!-- 以下都是侧滑菜单的内容依序排列 -->
    <Button
        android:id="@+id/btnTop"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="#d9dee4"
        android:text="置顶"
        android:textColor="@android:color/white"/>

    <Button
        android:id="@+id/btnUnRead"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:background="#ecd50a"
        android:clickable="true"
        android:text="标记未读"
        android:textColor="@android:color/white"/>

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="@color/red_ff4a57"
        android:text="删除"
        android:textColor="@android:color/white"/>

</com.mcxtzhang.swipemenulib.SwipeMenuLayout>

3、AndroidSwipeLayout
GitHub 地址:https://github.com/daimajia/AndroidSwipeLayout

出自代码家大神,功能强大,支持上下左右四个方向滑出菜单,可单独使用,也支持RecyclerView 和 ListView等列表,Adapter需要继承RecylerViewAdapter或者BaseSwipeAdapter。

  • 效果图:
    【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库

  • 使用方式:

<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="80dp">
    <!-- Bottom View Start-->
     <LinearLayout
        android:background="#66ddff00"
        android:id="@+id/bottom_wrapper"
        android:layout_width="160dp"
        android:weightSum="1"
        android:layout_height="match_parent">
        <!--What you want to show-->
    </LinearLayout>
    <!-- Bottom View End-->

    <!-- Surface View Start -->
     <LinearLayout
        android:padding="10dp"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--What you want to show in SurfaceView-->
    </LinearLayout>
    <!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>
  • 代码中:
SwipeLayout swipeLayout =  (SwipeLayout)findViewById(R.id.sample1);

//set show mode.
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);

//add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));

swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onClose(SwipeLayout layout) {
                //when the SurfaceView totally cover the BottomView.
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
               //you are swiping.
            }

            @Override
            public void onStartOpen(SwipeLayout layout) {

            }

            @Override
            public void onOpen(SwipeLayout layout) {
               //when the BottomView totally show.
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
               //when user's hand released.
            }
        });

4、SwipeRecyclerView

  • 效果图
    【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库

GitHub 地址:https://github.com/yanzhenjie/SwipeRecyclerView

  • 使用方式:
    本库的一大特色是它滑出的菜单可以是左右排列的,也可以是上下排列,提供多种选择,不过侵入性稍微有点高,需要使用本库提供的 SwipeRecyclerView ,但是使用方式与提供的 api 和原生的 RecyclerView 是一样的。还有它通过代码来创建划出的菜单。如下:
// 设置监听器。
swipeRecyclerView.setSwipeMenuCreator(mSwipeMenuCreator);

// 创建菜单:
SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
    @Override
    public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
        SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
            ...; // 各种文字和图标属性设置。
        leftMenu.addMenuItem(deleteItem); // 在Item左侧添加一个菜单。

        SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
            ...; // 各种文字和图标属性设置。
        leftMenu.addMenuItem(deleteItem); // 在Item右侧添加一个菜单。
        
        // 注意:哪边不想要菜单,那么不要添加即可。
    }
};

// 菜单点击监听。
swipeRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener);

OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
    @Override
    public void onItemClick(SwipeMenuBridge menuBridge, int position) {
        // 任何操作必须先关闭菜单,否则可能出现Item菜单打开状态错乱。
        menuBridge.closeMenu();
        
        // 左侧还是右侧菜单:
        int direction = menuBridge.getDirection();
        // 菜单在Item中的Position:
        int menuPosition = menuBridge.getPosition();
    }
};

5、RecyclerViewUndoSwipe
GitHub 地址:https://github.com/HoneyNeutrons/RecyclerViewUndoSwipe

一个可以拖拽和侧滑的UI效果,动画非常炫。文章来源地址https://www.toymoban.com/news/detail-420763.html

  • 效果图
    【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库

到了这里,关于【Android -- UI 开发】RecyclerView 侧滑菜单(侧滑删除) 开源库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 19_微信小程序之优雅实现侧滑菜单

    19_微信小程序之优雅实现侧滑菜单 一.先上效果图 要实现这样一个效果,布局其实很简单,整体布局是一个横向滚动的scroll-view,难点在于怎么控制侧滑菜单的回弹,以及寻找回弹的边界条件? 此篇文章主要是基于uni-app来实现的,以后也将继续使用uni-app,但是即使使用的是原

    2024年02月09日
    浏览(28)
  • Android开发—RecyclerView使用

    RecyclerView 在Android中用于创建列表。 官网的解释为: RecyclerView 可以让您轻松高效地显示大量数据。您提供数据并定义每个列表项的外观,而 RecyclerView 库会根据需要动态创建元素。 当RecyclerView的列表项滚出屏幕的时候,RecyclerView不会销毁该视图,相反而是将这些视图复用到

    2024年02月16日
    浏览(46)
  • android 五大应用开发框架(1),腾讯竟然又偷偷开源了一套Android原生UI框架

    2、Android Runtime Android包含一个核心库的集合,提供大部分在Java编程语言核心类库中可用的功能。每一个Android应用程序是Dalvik虚拟机中的实例,运行在他们自己的进程中。Dalvik虚拟机设计成,在一个设备可以高效地运行多个虚拟机。Dalvik虚拟机可执行文件格式是.dex,dex格式是

    2024年04月09日
    浏览(47)
  • Android TV屏 开发、RecyclerView焦点处理等

      TV屏使用遥控器控制,通过焦点操作界面,就跟电视投屏类似 一共两个核心,焦点的处理,按键的监听处理 按键原生提供了 onKeyDown 来监听,通过不同的 keyCode 区分不同的按键 一般如果没有遥控器,可以通过电脑键盘测试,使用投屏软件投屏后,对键盘按键效果跟遥控器类

    2024年02月10日
    浏览(31)
  • Android10 Settings系列(三)根据需求动态添加删除一级菜单、二级菜单的设置项

    当时遇到定制需求,需要根据实际需要隐藏Settings的菜单项,于是开始了寻找方法 在看了一下源码,经过尝试后,确认生效后,就简单说明一下Settings中布局中主要组成元素 Settings中的菜单项是由 PreferenceScreen 和Preference组成的。其中PreferenceScreen 类似于我们平常使用布局中的

    2024年02月14日
    浏览(36)
  • 安卓开发面试问题回答技巧,腾讯竟然又偷偷开源了一套Android原生UI框架

    偶然看到知乎的内推帖,投了个简历,下午hr姐姐call我,安排面试选在3天后,然而又要笔试阿里,所以没怎么复习。 8点起床,9点过比较紧张的去了创业园,感觉知乎氛围很好,在那等了一小会,有前台大叔给你倒水。 应该是个参加工作不久的研究僧师兄,出了一道算法题

    2024年03月12日
    浏览(50)
  • Android开发:RecyclerView获取item位置的几种方法比较

            当使用 RecyclerView 来展示列表数据时,获取 item 的位置是一个常见的需求。RecyclerView 提供了多种获取 item 位置的方法,包括 getAdapterPosition() 、 getBindingAdapterPosition() 、 getAbsoluteAdapterPosition() 等等。这些方法的实现原理和返回值有所不同,因此在实际使用时需要根据

    2023年04月20日
    浏览(54)
  • Android侧滑栏(一)可缩放可一起移动的侧滑栏

    在实际的各类App开发中,经常会需要做一个左侧的侧滑栏,类似于QQ这种。 今天这篇文章总结下自己在开发中遇到的这类可以跟随移动且可以缩放的侧滑栏。 一、实现原理 使用 HorizontalScrollView 实现一个水平方向的可滑动的View,左布局为侧滑栏,右布局为自己的主页内容。

    2024年02月13日
    浏览(23)
  • android 13.0 去掉recovery模式UI操作页面的菜单选项

    在13.0进行系统rom定制化开发中,在进行一些定制化开发中,会根据需要在进入recovery模式的时候,去掉recovery模式的一些菜单选项, Reboot to bootloader,Enter rescue等菜单项,经过分析得知, 就是在device.cpp去掉一些菜单选项就可以了,接下来就来分析实现相关功能 在13.0的recove

    2024年02月04日
    浏览(36)
  • Android自定义侧滑Item

    源码地址:https://github.com/LanSeLianMa/CustomizeView/tree/master/cehuaitem 先定义一个容器

    2024年02月13日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包