Android仿制iOS的抖动删除

前言

已经很久没有写了,正好最近有遇到一个安卓仿iOS的抖动删除功能。小编研究了一下,正好分享出来。先放个图给各位看官们,看看是不是自己需要的。


实现

其实这个功能并不复杂,下面开始说明一下怎么做。有需求的看官们可以直接拉到最后面查看Demo。

首先是添加依赖库Recyclerview

在app文件中的build.gradle里的dependencies下方,添加依赖库Recyclerview

implementation 'com.android.support:recyclerview-v7:28.0.0'
布局文件

首先是总体的布局,小编就随便弄了一下,差不多就行了,如下:



    
        

        
    



然后是item的布局,item可以按照自己的喜好弄,这里小编也只是弄了一个简易的,如下:



    
        
        
    
    
    
    

    

设置模型

然后就是模型的设置,小编在这里只设了两个,i代表的是所显示的数字,isDetele代表的是是否显示删除样式,如下:

public class Model {
    int i;
    boolean isDetele;
}
设置Adapter

这里是比较关键的一个步骤,按钮的点击、长按、删除都需要在这里用代理分享出去,然后还需要根据进来的数据显示相应的样式,如下:

public class ItemAdapter extends RecyclerView.Adapter {

    public interface OnItemClickListener {
        void onClick(int position);
        void onDeleteClickListener(int position);
        void onLongClick(View view, int position);
    }

    private Context mContext;
    private OnItemClickListener mListener;
    private List mDatas = new ArrayList<>();

    public ItemAdapter(Context context, List datas) {
        mContext = context;
        mDatas = datas;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }


    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onClick(position);
                }
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onDeleteClickListener(position);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View view) {
                Animation rotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
                view.startAnimation(rotate);
                holder.delete.setVisibility(View.VISIBLE);
                holder.mask.setVisibility(View.VISIBLE);

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        view.clearAnimation();
                    }
                }, 1 * 1000);

                if (mListener != null) {
                    mListener.onLongClick(view, position);
                }
                return true;
            }
        });

        Boolean isDelete = mDatas.get(position).isDetele;
        holder.textView.setText(String.valueOf(mDatas.get(position).i));
        if (isDelete) {
            holder.delete.setVisibility(View.VISIBLE);
            holder.mask.setVisibility(View.VISIBLE);
        }else {
            holder.delete.setVisibility(View.INVISIBLE);
            holder.mask.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout backgound;
        RelativeLayout mask;
        TextView textView;
        ImageButton delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            backgound = itemView.findViewById(R.id.icone_item);
            textView = itemView.findViewById(R.id.item_text);
            mask = itemView.findViewById(R.id.icone_item_mask);
            delete = itemView.findViewById(R.id.icone_item_delete);
        }
    }
}
最后

最后,就是在显示的Activity页面中调用。设置相应的参数来设定显示的方式,各种手势的处理等等,如下:

public class ItemAdapter extends RecyclerView.Adapter {

    public interface OnItemClickListener {
        void onClick(int position);
        void onDeleteClickListener(int position);
        void onLongClick(View view, int position);
    }

    private Context mContext;
    private OnItemClickListener mListener;
    private List mDatas = new ArrayList<>();

    public ItemAdapter(Context context, List datas) {
        mContext = context;
        mDatas = datas;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }


    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onClick(position);
                }
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onDeleteClickListener(position);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View view) {
                Animation rotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
                view.startAnimation(rotate);
                holder.delete.setVisibility(View.VISIBLE);
                holder.mask.setVisibility(View.VISIBLE);

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        view.clearAnimation();
                    }
                }, 1 * 1000);

                if (mListener != null) {
                    mListener.onLongClick(view, position);
                }
                return true;
            }
        });

        Boolean isDelete = mDatas.get(position).isDetele;
        holder.textView.setText(String.valueOf(mDatas.get(position).i));
        if (isDelete) {
            holder.delete.setVisibility(View.VISIBLE);
            holder.mask.setVisibility(View.VISIBLE);
        }else {
            holder.delete.setVisibility(View.INVISIBLE);
            holder.mask.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout backgound;
        RelativeLayout mask;
        TextView textView;
        ImageButton delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            backgound = itemView.findViewById(R.id.icone_item);
            textView = itemView.findViewById(R.id.item_text);
            mask = itemView.findViewById(R.id.icone_item_mask);
            delete = itemView.findViewById(R.id.icone_item_delete);
        }
    }
}

这篇文章到这里就结束了,写的不好的地方欢迎大家指出,Demo下载地址:Demo。最后,希望这篇文章对各位看官们有所帮助。如果看官们可以给小编一个小小的支持那就更好了。

你可能感兴趣的:(Android仿制iOS的抖动删除)