Android RecycleView 实现左滑上下分层示例代码(自定义功能)
由于项目原因,需要在很多地方需要左划删除,并在后期会在item中不定增加功能。网上很多左划删除的demo,但大多封装的不太彻底,所以就试着自己进行封装。
独立上下两层互补干扰,满足不同item的左划
底部功能项不定如(删除、收藏)
截图
独立Adapter的操作方式
思路: public RCommonSlideAdapter(Object object, List datas, int layoutId, Class clazz, SlideItemHolder handle) {
在onCreateViewHolder中通过反射创建holder,保证数据的操作通过回调函数,交给activity处理
Constructor constructor2 = mClazz.getConstructor(View.class); holder = constructor2.newInstance(v);
上下分层
思路:创建底部xml布局,再通过addView插入顶部view,并交由activity处理,底部布局处理放在adapter中处理。
Constructor constructor = ViewHolderClass.SlideViewHolder.class.getConstructor(View.class); holder = constructor.newInstance(v); View contentView = View.inflate(parent.getContext(), mLayoutId, null); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup .LayoutParams.WRAP_CONTENT); ((ViewHolderClass.SlideViewHolder) holder).mRlSlidContent.addView(contentView, params); Constructor constructor2 = mClazz.getConstructor(View.class); holder = constructor2.newInstance(v); ((ViewHolderClass.SlideViewHolder) holder).mRlSlidContent.getLayoutParams().width = getScreenWidth(parent.getContext());
独立功能项
思路:在xml布局中,不断增加item。并再回调函数中,对每个按钮选择性处理,并可动态添加
<LinearLayout android:id="@+id/linDelete" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_toRightOf="@+id/rlSlidContent" android:orientation="vertical" > <ImageView android:id="@+id/sliding_delete" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center_horizontal" android:src="@drawable/icon_item_delete" /> </LinearLayout>
public class ViewHolderClass { public static class SlideViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.sliding_delete) public ImageView btn_Delete; @BindView(R.id.rlSlidContent) public RelativeLayout mRlSlidContent; @BindView(R.id.linSlideTitle) public LinearLayout mLinSlideTitle; @BindView(R.id.tvSlideTitle) public TextView mTvSlideTitle; public SlideViewHolder(View view) { super(view); ButterKnife.bind(this, view); } } public static class ShopCarViewHolder extends SlideViewHolder { @BindView(R.id.itemTv) public TextView mItemTv; public ShopCarViewHolder(View view) { super(view); } } }
代码如下:RecycleSlideDemo_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)