动手打造史上最简单的 Recycleview 侧滑菜单

本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发。

我的开源库:一句代码搞定 RecycleView 侧滑菜单、添加头部底部、加载更多

在实现 Recycleview 侧滑菜单时起初使用了开源库 SwipeRecyclerView ,此库功能广泛,但无法满足个人需求,这是因为此库中存在以下局限性:

  1. 菜单文字一旦确定将无法修改
  2. 侧滑时整个 item 都会滑动
  3. 无法自定义菜单样式

只能自己实现了,查阅资料后发现,较多通过 DragHelper 实现的,它是一个手势滑动辅助工具,使 item 可以滑动,然后… , 等等!既然目的是让 item 可以滑动,那为什么不直接在 item 布局中使用 HorizontalScrollView 呢?参考鸿神的Android 自定义控件打造史上最简单的侧滑菜单,标题致敬一波~

首先是自定义 SlidingMenu :

public class SlidingMenu extends HorizontalScrollView {

    private static final float radio = 0.3f;//菜单占屏幕宽度比
    private final int mScreenWidth;
    private final int mMenuWidth;
    private boolean once = true;

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScreenWidth = ScreenUtil.getScreenWidth(context);
        mMenuWidth = (int) (mScreenWidth * radio);
        setOverScrollMode(View.OVER_SCROLL_NEVER);
        setHorizontalScrollBarEnabled(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (once) {
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            wrapper.getChildAt(0).getLayoutParams().width = mScreenWidth;
            wrapper.getChildAt(1).getLayoutParams().width = mMenuWidth;
            once = false;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (Math.abs(scrollX) > mMenuWidth / 2) {
                    this.smoothScrollTo(mMenuWidth, 0);
                } else {
                    this.smoothScrollTo(0, 0);
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }
}

在 item 布局中应用 SlidingMenu,注意 SlidingMenu 中是按照顺序区分 content 和 menu 的,所以布局文件中顺序要对应一致 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="2dp"
              android:orientation="vertical"
    >
    <com.xmwj.slidingmenu.SlidingMenu
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <LinearLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >
                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="70dp"
                    android:background="#af6fe1"
                    />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/menu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="置顶"
                    android:textColor="#fff"
                    android:gravity="center"
                    android:background="@color/colorAccent"
                    />
            </LinearLayout>
        </LinearLayout>
    </com.xmwj.slidingmenu.SlidingMenu>
</LinearLayout>

OK~ ,这就实现了具有侧滑菜单的 Recyclevie

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 27
    评论
要在RecyclerView中实现上下渐变,你可以使用一个叫做`ItemDecoration`的类来实现。在`ItemDecoration`中,你可以重写`onDrawOver()`方法,然后在该方法中绘制你想要的渐变效果。 下面是一个简单的实现示例,可以实现RecyclerView顶部和底部的渐变效果: ```java public class GradientItemDecoration extends RecyclerView.ItemDecoration { private int mGradientHeight = 100; //渐变高度,单位为px private int mGradientColor = Color.TRANSPARENT; //渐变颜色,默认为透明 public GradientItemDecoration(int gradientHeight, int gradientColor) { mGradientHeight = gradientHeight; mGradientColor = gradientColor; } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(c, parent, state); int width = parent.getWidth(); int height = parent.getHeight(); //绘制顶部渐变 LinearGradient topGradient = new LinearGradient(0, 0, 0, mGradientHeight, mGradientColor, Color.TRANSPARENT, Shader.TileMode.CLAMP); Paint topPaint = new Paint(); topPaint.setShader(topGradient); c.drawRect(0, 0, width, mGradientHeight, topPaint); //绘制底部渐变 LinearGradient bottomGradient = new LinearGradient(0, height - mGradientHeight, 0, height, mGradientColor, Color.TRANSPARENT, Shader.TileMode.CLAMP); Paint bottomPaint = new Paint(); bottomPaint.setShader(bottomGradient); c.drawRect(0, height - mGradientHeight, width, height, bottomPaint); } } ``` 你可以在适当的地方创建一个`GradientItemDecoration`对象,并将其添加到你的RecyclerView中: ```java GradientItemDecoration decoration = new GradientItemDecoration(100, Color.BLACK); recyclerView.addItemDecoration(decoration); ``` 这样就可以实现RecyclerView的上下渐变效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值