andriod项目实战之二十二---列表右滑删除功能实现

效果图

andriod项目实战之二十二---列表右滑删除功能实现_第1张图片

XML代码

功能代码(核心)

private void initlay(){
        mRecyclerView = activity.findViewById(R.id.recyclerView);
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        // 设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL_LIST));
        // 设置 item 增加和删除时的动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //mList = getList();
        getUserList(keyword);
        mHomeAdapter = new HomeAdapter(mUserList);
        mRecyclerView.setAdapter(mHomeAdapter);
        homemobile.setText(UserManager.takeMobile(activity));
        mHomeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener(){

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                UserBean info = mUserList.get(position);
                switch (view.getId()) {
                    case R.id.delete_button:
                        //Log.d("点击我了",info.getUserName());
                        //mUserList.remove(position);
                        //mHomeAdapter.notifyDataSetChanged();
                        //mUserList.remove(position);

                        doDelete(position);
                        //mHomeAdapter.notifyDataSetChanged();
                        break;
                    case R.id.ll_item:
                       // Log.d("无聊","ss");
                        Intent intent = new Intent(activity, QingActivity.class);
                        Bundle bundle=new Bundle();
                        //bundle.putString("name",mList.get(position));
                        bundle.putInt("uId",mUserList.get(position).getUserId());
                        bundle.putString("name",mUserList.get(position).getUserName());
                        bundle.putString("remarks",mUserList.get(position).getRemarks());
                        intent.putExtras(bundle);
                        activity.startActivity(intent);
                        break;
                }
            }
        });
doDelete函数
 private void doDelete( Integer position){
        UserBean info = mUserList.get(position);
        HttpRequest.memberdel((LifecycleOwner) activity,String.valueOf(info.getUserId()), new CallBackLis() {
            @Override
            public void onSuccess(String method, Integer content) {
                //Log.d("userDelete", String.valueOf(content));
                if(1==content){
                    //Log.d("userDelete成功", String.valueOf(content));
                    getUserList(keyword);

                }else {
                    //Log.d("userDelete失败", String.valueOf(content));
                    Toast.makeText(activity, "删除失败", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(String method, String error) {
                Toast.makeText(activity, error, Toast.LENGTH_SHORT).show();
            }
        });
    }

欢迎各位同学共同提高。

你可能感兴趣的:(android)