我创建了一个输入EditText的关键字。输入一些文本并输入空格后,该文本将更改为芯片。我通过头添加方法创建了带有芯片和EditText视图的回收站视图。 This is the view I created:。 当我输入Delete键(键盘上的后退按钮)时,芯片将被删除。
问题是,当我再次添加芯片时,会显示第一个删除的芯片。
这是我的适配器类。
public class KeyWordchipAdapter extends RecyclerView.Adapter {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 2;
private List<String> stringArrayList;
private Activity activity;
Chip chip;
public KeyWordchipAdapter(List<String> stringArrayList, Activity activity) {
this.stringArrayList = stringArrayList;
this.activity = activity;
this.addItemListner = (CreateBoatActivity)activity;
}
ChipsRecyclerClickListner addItemListner;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_chip_group_layout, parent, false);
return new ItemViewHolder(itemView);
} else if (viewType == TYPE_HEADER) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_chip_edittext_layout, parent, false);
return new HeaderViewHolder(itemView);
}
else return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder) {
final HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.headerTitle.setHint("Enter Keywords");
headerHolder.headerTitle.addTextChangedListener(new TextWatcher() {
private int lastLength;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
lastLength = s.length();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
if (lastLength > s.length()) return;
if (s.charAt(s.length() - 1) == 32) {
addItemListner.keywordListener(headerHolder.headerTitle.getText().toString().replaceAll("\\s+",""),position);
headerHolder.headerTitle.setText("");
headerHolder.headerTitle.setFocusable(true);
headerHolder.headerTitle.requestFocus();
}
} catch (IndexOutOfBoundsException ex) {
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
headerHolder.headerTitle.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
if(stringArrayList.size()>0 && headerHolder.headerTitle.getText().toString().length() == 0){
if (addItemListner != null) {
stringArrayList.remove(position-1);
notifyItemRemoved(position-1);
notifyItemRangeChanged(position-1, stringArrayList.size());
}
}
}
return false;
}
});
}
else if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
if(stringArrayList.size()== 0){
}
else {
chip=new Chip(activity);
chip.setText(stringArrayList.get(position));
chip.setCloseIconVisible(true);
chip.setCheckable(false);
chip.setClickable(false);
itemViewHolder.itemText.addView(chip);
itemViewHolder.itemText.setVisibility(View.VISIBLE);
}
}
}
@Override
public int getItemViewType(int position) {
if (position == stringArrayList.size()) {
return TYPE_HEADER;
} else {
return TYPE_ITEM;
}
}
@Override
public int getItemCount() {
return stringArrayList.size()+1;
}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
EditText headerTitle;
public HeaderViewHolder(View view) {
super(view);
headerTitle = (EditText) view.findViewById(R.id.et_data);
}
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
ChipGroup itemText;
public ItemViewHolder(View itemView) {
super(itemView);
itemText = (ChipGroup) itemView.findViewById(R.id.chip_group);
}
}
}
这是我活动中的回调。
@Override
public void keywordListener(String value, int position) {
keywordListItem.add(value);
}