zoukankan      html  css  js  c++  java
  • RecyclerView实现上下拖动,滑动删除

    先上效果图: 

    引入第三方的RecyclerView适配器

    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

    在MianActivity的布局文件xml中加入RecyclerView控件

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/main_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    新建一个item的布局文件item_recycler.xml这里直接贴出代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:paddingRight="10dp"
        android:paddingLeft="@dimen/dp_10"
        android:layout_height="45dp">
        <ImageView
            android:src="@drawable/login_logo"
            android:layout_width="35dp"
            android:layout_height="35dp"/>
        <RelativeLayout
            android:paddingLeft="10dp"
            android:layout_width="match_parent"
            android:layout_height="45dp">
            <TextView
                android:id="@+id/recyc_title"
                android:text="标题"
                android:textStyle="bold"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/sp_14"
                android:layout_centerVertical="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <View
                android:layout_alignParentBottom="true"
                android:background="@color/colorBlack"
                android:layout_width="match_parent"
                android:layout_height="1px"/>
        </RelativeLayout>
    </LinearLayout>

    新建一个适配器类RecyclerAdapter直接贴出适配器的代码

    public class RecyclerAdapter extends BaseQuickAdapter<TestEntity, BaseViewHolder>{
    
        public RecyclerAdapter(int layoutResId, @Nullable List<TestEntity> data) {
            super(layoutResId, data);
        }
    
        @Override
        protected void convert(BaseViewHolder helper, TestEntity item) {
            helper.setText(R.id.recyc_title,item.getName());
        }
    }

    新建一个实体类TestEntity

    public class TestEntity {
        private String name;
    
        public TestEntity(String name){
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    MianActivity中添加模拟数据,绑定RecyclerView的适配器

    //模拟列表数据
    private void initData() {
            list = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                if(i % 2 == 0){
                    list.add(new TestEntity("张三"));
                }else{
                    list.add(new TestEntity("李四"));
                }
            }
        }
    
    //绑定适配器
    private void initView() {
            mAdapter = new RecyclerAdapter(R.layout.item_recycler,list);
            mainRecycler.setLayoutManager(new LinearLayoutManager(this));
            mainRecycler.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(MainActivity.this, list.get(position).getName(), Toast.LENGTH_SHORT).show();
                }
            });
    }

    实现到这里可以显示出RecyclerView的列表,发现点击事件没有效果,在drawable新建一个item_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorBackGround">
        <item android:drawable="@color/colorWhite"/>
    </ripple>

    然后在item_recycler.xml中使用,最外层LinearLayout的background

     

     接来下实现RecyclerView的上下拖动,以及侧滑删除,新建OpenationData一个接口

    public interface OpenationData {
        void onItemMove(int fromPosition,int toPosition);
        void onItemDissmiss(int position);
    }

    在适配器中implements,贴出适配器的全部代码

    public class RecyclerAdapter extends BaseQuickAdapter<TestEntity, BaseViewHolder> implements OpenationData {
    
        private List<TestEntity> entityList;
    
        public RecyclerAdapter(int layoutResId, @Nullable List<TestEntity> data) {
            super(layoutResId, data);
            entityList = data;
        }
    
        @Override
        protected void convert(BaseViewHolder helper, TestEntity item) {
            helper.setText(R.id.recyc_title,item.getName());
        }
    
        @Override
        public void onItemMove(int fromPosition, int toPosition) {
            //交换位置
            Collections.swap(entityList,fromPosition,toPosition);
            notifyItemMoved(fromPosition,toPosition);
        }
    
        @Override
        public void onItemDissmiss(int position) {
            //移除数据
            entityList.remove(position);
            notifyItemRemoved(position);
        }
    }

    最后就是MainActivity中,贴出代码

    public class MainActivity extends AppCompatActivity {
    
        @BindView(R.id.main_recycler)
        RecyclerView mainRecycler;
    
        private List<TestEntity> list;
        private RecyclerAdapter mAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            initData();
            initView();
        }
    
        private void initData() {
            list = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                if(i % 2 == 0){
                    list.add(new TestEntity("张三"));
                }else{
                    list.add(new TestEntity("李四"));
                }
            }
        }
    
        private void initView() {
            mAdapter = new RecyclerAdapter(R.layout.item_recycler,list);
            mainRecycler.setLayoutManager(new LinearLayoutManager(this));
            mainRecycler.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(MainActivity.this, list.get(position).getName(), Toast.LENGTH_SHORT).show();
                }
            });
    
    
            ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
                @Override
                public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                    //允许上下拖动
                    int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
                    //只允许从右向左滑动
                    int swipeFlags = ItemTouchHelper.LEFT;
                    return makeMovementFlags(dragFlags,swipeFlags);
                }
    
                @Override
                public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                    mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
                    return false;
                }
    
                @Override
                public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                    mAdapter.onItemDissmiss(viewHolder.getAdapterPosition());
                }
            });
    
            itemTouchHelper.attachToRecyclerView(mainRecycler);
        }
    }
  • 相关阅读:
    vue中插槽的理解
    父子组件的通信
    vue3.0怎么禁用eslint校验代码和修改端口号
    三大排序
    让机器人实现自主行走 没你想的那么难
    国内外知名激光雷达公司盘点
    激光雷达寿命短,思岚通过什么技术来解决?
    浅谈SLAM的回环检测技术
    除了ROS, 机器人定位导航还有其他方案吗?
    思岚科技即将登陆“2018日本机器人周”精彩抢先看
  • 原文地址:https://www.cnblogs.com/Mr-Deng/p/11842676.html
Copyright © 2011-2022 走看看