Android RecyclerView用实体类进行新增和删除

这次的适配器用的是BaseQuickAdapter
框架引入
在build.gradle(Project:XXXX):

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

在build.gradle(Module:app):

  //BaseQuickAdapter依赖
 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.31'
  //recyclerview依赖
 implementation 'com.android.support:recyclerview-v7:26.1.0'

然后是在Activity中

public class MainActivity extends AppCompatActivity {

    private TextView tvAdd;
    private RecyclerView recyclerView;
    private Button btnCommit;
    private CohabitantAdapter cohabitantAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        initView();
        initData();
        setListener();
    }


    private void initView() {
        tvAdd = (TextView) findViewById(R.id.tv_add);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        btnCommit = (Button) findViewById(R.id.btn_commit);
    }

    private void initData() {
        List beans = intiData();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        cohabitantAdapter = new CohabitantAdapter(this);
        recyclerView.setAdapter(cohabitantAdapter);
        cohabitantAdapter.setNewData(beans);
    }

    private void setListener() {
        //新增
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cohabitantAdapter.addData(0, new Bean());
            }
        });

        //删除
        cohabitantAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_delete: //  点击删除
                        cohabitantAdapter.remove(position);
                        break;
                }
            }
        });

        //获取数据
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List data = cohabitantAdapter.getData();
                for (Bean datum : data) {
                    String name = datum.getName();
                    String gender = datum.getGender();
                }
            }
        });
    }

    public List intiData() {
        List mData = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            mData.add(new Bean());
        }
        return mData;
    }
}

activity中的xml




    
        

        

        

适配器

public class CohabitantAdapter extends BaseQuickAdapter {

    public CohabitantAdapter(Activity mActivity) {
        super(R.layout.item_fragment_cohabitant);
    }

    @Override
    protected void convert(BaseViewHolder helper, CheckInPerson.UnderagesBean.TzrBean item) {
        if (item != null) {
            helper.setText(R.id.et_checkIn_name, item.getName());
            helper.setText(R.id.et_checkIn_relation, item.getRelationship());
        }

        helper.addOnClickListener(R.id.delete);
        EditText view = helper.getView(R.id.et_checkIn_name);
        EditText view1 = helper.getView(R.id.et_checkIn_relation);
        view.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = s.toString();
                item.setName(s1);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        view1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = s.toString();
                item.setRelationship(s1);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }
}

适配器的xml




    

        

        


        
    


    

        

        


        
    

    


最后附上效果图


效果.gif

各位看官大人,点个赞再走呗!!!!

你可能感兴趣的:(Android RecyclerView用实体类进行新增和删除)