仿QQ左滑删除【基于RecyclerView】

这是我github开源项目,目前测试阶段,欢迎大家使用提意见!

 

https://github.com/SineyCoder/LeftSlideView

 

使用步骤 

 添加仓库

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

 添加依赖包

dependencies {
	        implementation 'com.github.SineyCoder:LeftSlideView:v1.2'
	}

 

因为此list是根据RecyclerView来进行整改的,项目中自带design-28.0.0

 

首先设置Activity的布局



    

 

然后设置item

需要注意的是,item的基布局,即id为content_item这个布局,一定要设置id

然后自己要显示的文本覆盖整个宽度,即id为text这个view

然后设置隐藏项,记住一定要设置具体宽度和id,我这里设置宽度为65dp,id为a/b/c



    

    

    

    

最后使用

Activity

public class MainActivity extends AppCompatActivity {

    private LeftSlideView mLeftSlideView;

    List list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLeftSlideView = findViewById(R.id.test_view);
        for(int i = 0;i < 20;i++){
            list.add(UUID.randomUUID().toString());
        }
        MyAdapter adapter = new MyAdapter(list);//自己创建adapter
        LeftSlideLinearManager manager = new LeftSlideLinearManager(this);//项目自带的LeftSlidemanager,修改了部分功能
        mLeftSlideView.setLayout(R.id.content_item);//一定要设置基布局id
        mLeftSlideView.setItems(R.id.a, R.id.b, R.id.c);//一定要设置隐藏项
        mLeftSlideView.setLayoutManager(manager);
        mLeftSlideView.setAdapter(adapter);
    }
}

adapter

public class MyAdapter extends RecyclerView.Adapter {

    private List list;

    public MyAdapter(List list){
        this.list = list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.content.setText(list.get(i));
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView content;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.text);
        }
    }

}

 测试结果:

仿QQ左滑删除【基于RecyclerView】_第1张图片

 

 

 

你可能感兴趣的:(android)