基本实现思路
通过自定义View的方式实现步骤:
1、自定义ViewGroup
2、在onLayout 中,获取childView并对他们进行布局,这一步比较重要,content 占满屏幕,菜单View 在屏幕之外,当滑动的时候,content滑屏幕,menu 进入屏幕,就达到了我需要的效果,布局草图如下:
image
3、重写dispatchTouchEvent和onInterceptTouchEvent方法拦截事件和处理滚动。滑动效果的实现既可以用Scroller,也可以用属性动画ValueAnimator。
添加依赖
//侧滑SwipeRecyclerView
implementation 'com.yanzhenjie:recyclerview-swipe:1.1.4'
1、SwipeRevealLayout
SwipeRevealLayout 使用简单、代码入侵低,不但支持左右侧滑菜单,还支持上下滑出菜单。可以配合各种布局使用,包括RecyclerView 、ListView、ScrollView 等,效果很赞
使用方式:
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mode="same_level"
app:dragEdge="left">
android:layout_width="wrap_content"
android:layout_height="match_parent" />
android:layout_width="match_parent"
android:layout_height="match_parent" />
在adapterclass 中:
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, dataObjec