代码加注释

首先布局就是一个RecyclerView

   

适配器什么的不多说,正常写就行

mRecyclerView = findViewById(R.id.recyclerview);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));myApr = new MyApr();mRecyclerView.setAdapter(myApr);mDatas = new ArrayList<>();//这里是数据 自己放我这里就不放了
//添加长按删除监听myApr.setOnremoveListnner(new MyApr.OnremoveListnner() {    @Override    public void ondelect(final int i) {        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);        builder.setMessage("确定删除?");        builder.setTitle("提示");        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                mDatas.remove(i);                //更新列表                myApr.notifyDataSetChanged();                Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();            }        });        //添加AlertDialog.Builder对象的setNegativeButton()方法        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {            }        });        builder.create().show();    }});

 

public static class MyApr extends RecyclerView.Adapter {    //创建删除监听接口    interface OnremoveListnner {        void ondelect(int i);    }    private OnremoveListnner onremoveListnner;    public void setOnremoveListnner(OnremoveListnner onremoveListnner) {        this.onremoveListnner = onremoveListnner;    }    @Override    public VH onCreateViewHolder(ViewGroup parent, int viewType) {        return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_item, parent, false));    }    @Override    public void onBindViewHolder(final VH holder, final int position) {    //show=展示的view  click=删除view 监听也和recyclerView的不一样        holder.show.setText(mDatas.get(position));        //恢复状态        holder.recyclerViewItem.apply();        holder.click.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mDatas.remove(position);                //更新列表                notifyDataSetChanged();                Toast.makeText(holder.itemView.getContext(), "删除成功", Toast.LENGTH_LONG).show();            }        });        //长按监听        holder.show.setOnLongClickListener(new View.OnLongClickListener() {            @Override            public boolean onLongClick(View v) {                if (onremoveListnner != null) {                    //删除                    onremoveListnner.ondelect(position);                }                return true;            }        });    }    @Override    public int getItemCount() {        return null == mDatas ? 0 : mDatas.size();    }}

左划删除我这里跟RecyclerView是没有关系的,布局最关键,跟item有关,父容器需要自定义HorizontalScrollView,自己创建一个类就行,属性可以自己修改

public class MyRecyclerViewItem extends HorizontalScrollView {    public MyRecyclerViewItem(Context context) {        super(context);        init(context,null);    }    public MyRecyclerViewItem(Context context, AttributeSet attrs) {        super(context, attrs);        init(context, attrs);    }    public MyRecyclerViewItem(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context, attrs);    }    public static final String TAG=MyRecyclerViewItem.class.getSimpleName();    private boolean isLeft = true;//默认左边    private int rightLayoutWidth;    private int leftLayoutWidth;    private int range;    public void setRightLayoutWidth(int rightLayoutWidth) {        this.rightLayoutWidth = rightLayoutWidth;    }    public void setLeftLayoutWidth(int leftLayoutWidth) {        this.leftLayoutWidth = leftLayoutWidth;    }    public void setRange(int range) {        this.range = range;    }    private void init(Context context, AttributeSet attrs) {        leftLayoutWidth = getScreenSize(getContext()).widthPixels;// recyclerview 宽度        rightLayoutWidth = dp2px(getContext(),200);// 右边布局的宽度        range = dp2px(getContext(), 30);// 移动多少开始切换阈值        if (attrs!=null){            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRecyclerViewItem);            int indexCount = typedArray.getIndexCount();            for (int i = 0; i < indexCount; i++) {                int index = typedArray.getIndex(i);                if (index==R.styleable.MyRecyclerViewItem_left_width){                    leftLayoutWidth = typedArray.getInteger(index, 0)==0? leftLayoutWidth : dp2px(context, typedArray.getInteger(index, 0));                }                if (index==R.styleable.MyRecyclerViewItem_right_width){                    rightLayoutWidth = typedArray.getInteger(index, 0)==0? rightLayoutWidth : dp2px(context, typedArray.getInteger(index, 0));                }                if (index==R.styleable.MyRecyclerViewItem_move_range){                    range = typedArray.getInteger(index, 0)==0? range : dp2px(context, typedArray.getInteger(index, 0));                }            }            typedArray.recycle();        }    }    //适配器 bind 方法中调用    public void apply() {        isLeft = true;        changeLayout();        scrollTo(0, 0);    }    private void changeLayout() {        try {            ViewGroup mainLayout= (ViewGroup) getChildAt(0);            ViewGroup left= (ViewGroup) mainLayout.getChildAt(0);            ViewGroup right= (ViewGroup) mainLayout.getChildAt(1);            if (left.getMeasuredWidth()== leftLayoutWidth && right.getMeasuredWidth()==rightLayoutWidth){                Log.i(TAG, "changeLayout: no change");                return;            }            ViewGroup.LayoutParams layoutParams = left.getLayoutParams();            layoutParams.width = leftLayoutWidth;            left.setLayoutParams(layoutParams);            ViewGroup.LayoutParams layoutParams2 = right.getLayoutParams();            layoutParams2.width = rightLayoutWidth;            left.setLayoutParams(layoutParams);        } catch (Exception e) {            e.printStackTrace();        }    }    public static DisplayMetrics getScreenSize(Context context){        DisplayMetrics dm = new DisplayMetrics();        WindowManager windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);        windowManager.getDefaultDisplay().getMetrics(dm);        return dm;    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {            Log.i(getClass().getSimpleName(), "down");            return true;        }        if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {            Log.i(getClass().getSimpleName(), "up");            if (isLeft) {                if (getScrollX() > range) {                    isLeft = false;                    smoothScrollTo(rightLayoutWidth, 0);                } else {                    smoothScrollTo(0, 0);                }            } else {                if (getScrollX() < (rightLayoutWidth - range)) {                    isLeft = true;                    smoothScrollTo(0, 0);                } else {                    smoothScrollTo(rightLayoutWidth, 0);                }            }            return true;        }        Log.i(getClass().getSimpleName(), "end");        return super.onTouchEvent(ev);    }    public static int dp2px(Context context,float dpValue) {        DisplayMetrics scale = context.getResources().getDisplayMetrics();        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, scale);    }}

到这里就可以了包已经放上来不需要手动导,下面是item的布局(记得改下包名路径)里面的宽高、颜色啥的可以根据自己的需要改,这里不多解释

<?xml version="1.0" encoding="utf-8"?>                                                                                                                

到这里就可以用了,告辞

更多相关文章

  1. 极光推送获取不到RegisterId(注册ID)Android
  2. 【原创】删除Android预装软件包,…
  3. Android(安卓)SDK自带教程之BluetoothChat
  4. android studio 项目的版本问题
  5. 解决Warning:android-apt plugin is incompatible with future v
  6. Android中动态添加╱删除的Spinner菜单 — ArrayList与Widget的
  7. 代码实现android手机信号监听
  8. android电话服务
  9. 使用saripaar对android输入控件进行快速验证

随机推荐

  1. Android自定义相机实现自动对焦和手动对
  2. android 上传文件到服务器代码实例
  3. 一份关于 Java、Kotlin 与 Android(安卓)
  4. android 搭建开发环境
  5. 移动3g为什么这么坑爹
  6. 二、Android(安卓)NDK编程预备之Java jni
  7. ListView 使用详解
  8. android Service与BroadcastReceiver
  9. android 电话状态的监听(来电和去电)
  10. Android保存图片到系统相册并更新