概述
在界面实现一个列表,用户可以随意给列表新增或者删除项目,在开发中比较常用,但是真正做起来又有点花时间,今天花时间做一个,以便在以后的开发中用到。
详细
运行效果:
二、实现思路:
1、实现一个主界面,主界面放一个list用来显示这个动态的列表,还要包含一个按钮用来点击时新增列表的栏目。
2、实现列表的适配器Adapter,这里实现显示栏目布局以及删除当前栏目的接口回调。
3、在主界面列表变更中(新增or删除),能准确获取当前适配器的内容,以便不会误删。
三、实现过程:
1、实现一个主界面,主界面放一个list用来显示这个动态的列表,还要包含一个按钮用来点击时新增列表的栏目。
public class MainActivity extends AppCompatActivity implements ObserveEtListener, View.OnClickListener{private ActivityMainBinding mViews;private DynamicNewAdapter dynamicNewAdapter;private MainViewModel mainViewModel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mViews = DataBindingUtil.setContentView(this,R.layout.activity_main);mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);dynamicNewAdapter = new DynamicNewAdapter(this);mViews.recycleView.setAdapter(dynamicNewAdapter);dynamicNewAdapter.syncSetNewData(mainViewModel.drugsBeanList);mViews.setOnClickListener(this);}// 删除栏目@Overridepublic void delateObserve(int pos) {dynamicNewAdapter.syncSetNewData(new ArrayList<>(getAdapterDetails(pos)));}@Overridepublic void onClick(View v) {int vId = v.getId();switch (vId){case R.id.addItem:// 新增栏目addItem();break;}}/*** 新增栏目*/private void addItem(){mainViewModel.drugsBeanList = getAdapterDetails(-1);mainViewModel.drugsBeanList.add(new DrugsBean("", "", "ml/亩"));dynamicNewAdapter.syncSetNewData(mainViewModel.drugsBeanList);}
2、实现列表的适配器Adapter,这里实现显示栏目布局以及删除当前栏目的接口回调。
public class DynamicNewAdapter extends BaseDataBindingAdapter<DrugsBean, ItemDyncmicAdapterBinding> {private ObserveEtListener listener;private String[] numbers = Utils.getApp().getResources().getStringArray(R.array.usage_array);public DynamicNewAdapter(ObserveEtListener listener) {super(R.layout.item_dyncmic_adapter);this.listener = listener;}@Overrideprotected void convert(@NonNull DataBindViewHolder<ItemDyncmicAdapterBinding> helper, DrugsBean drugsBean) {helper.binding.tvNameValue.setText(drugsBean.drugName);helper.binding.tvUsageValue.setText(drugsBean.weigth);int pos = getData().indexOf(drugsBean);helper.binding.tvUsageUnit.setText(drugsBean.unit);helper.binding.spinnerUnit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {helper.binding.tvUsageUnit.setText(numbers[position]);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});if (drugsBean.unit != null && drugsBean.unit.indexOf("ml") > -1) {helper.binding.spinnerUnit.setSelection(0, true);} else {helper.binding.spinnerUnit.setSelection(1, true);}helper.setTag(R.id.img_del, pos);helper.binding.imgDel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (listener != null) {int pos = (int) v.getTag();notifyItemRemoved(pos);getData().remove(pos);listener.delateObserve(pos);}}});}}
3、在主界面列表变更中(新增or删除),能准确获取当前适配器的内容,以便不会误删。
/*** 获取当前列表数据* @param pos* @return*/private List<DrugsBean> getAdapterDetails(int pos) {List<DrugsBean> details = new ArrayList<>();int length = mViews.recycleView.getChildCount();for (int i = 0; i < length; i++) {if (pos == i)continue;ConstraintLayout ll_item = (ConstraintLayout) mViews.recycleView.getChildAt(i);TextView tv_name_value = ll_item.findViewById(R.id.tv_name_value);TextView tv_usage_value = ll_item.findViewById(R.id.tv_usage_value);TextView tv_usage_unit = ll_item.findViewById(R.id.tv_usage_unit);details.add(new DrugsBean(tv_name_value.getText().toString().trim(),tv_usage_value.getText().toString().trim(), tv_usage_unit.getText().toString().trim()));}return details;}
四、项目文件结构截图