android - How to dynamically delete a view added with addView()
问题描述
I am making a dialog to write a comment.
Dynamically add comment fields using addView()
.
Create a comment model class
and create a model class every time you press EnterKey
.
So far, it's good.
But the problem lies in deletion.
In my code, when I delete an item, only the last item is deleted.
In other words, only one last item is deleted.
How do I dynamically delete the currently focused item?
CommentModel.java
public class CommentModel {
public interface EditInputListener {
void OnEnterPressed();
void OnDeleteKeyPressed();
}
@Nullable
private CommentModel.EditInputListener listener;
private EditText commentEdit;
private final View rootView;
public CommentModel(@NonNull LayoutInflater layoutInflater, @Nullable ViewGroup parent) {
rootView = layoutInflater.inflate(R.layout.writing_comment_item, parent, false);
initView();
}
private void initView() {
commentEdit = this.findViewById(R.id.comment_edit);
commentEdit.setSingleLine(true);
// addView Event
commentEdit.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
if (listener != null) {
listener.OnEnterPressed();
return true;
}
}
return false;
});
// removeView Event
commentEdit.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(commentEdit.getText().toString().length() == 0) {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
listener.OnDeleteKeyPressed();
return true;
}
}
return false;
}
});
}
public void addListener(CommentModel.EditInputListener listener) {
this.listener = listener;
}
public void setFocus() {
commentEdit.requestFocus();
}
public View getRootView() {
return rootView;
}
private Context getContext() {
return rootView.getContext();
}
private <T extends View> T findViewById(@IdRes int id) {
return rootView.findViewById(id);
}
}
WritingCommentDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
LinearLayout mContainer; // input layout
CommentModel commentItem;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
bindViews(view);
addCommentItem(); // First Item
return view;
}
private void bindViews(View v) {
mContainer = v.findViewById(R.id.container);
}
private void addCommentItem() {
commentItem = new CommentModel(getLayoutInflater(), null);
commentItem.addListener(this);
commentItem.setFocus();
mContainer.addView(commentItem.getRootView());
}
@Override
public void OnEnterPressed() {
if (mContainer.getChildCount() >= 5) {
return;
}
addCommentItem();
}
@Override
public void OnDeleteKeyPressed() {
if (mContainer.getChildCount() <= 1) {
return;
}
mContainer.removeView(commentItem.getRootView());
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Override
public void onResume() {
super.onResume();
setDialogSize();
}
private void setDialogSize() {
getDialog().getWindow().setLayout(1000, 1000);
}
}
解决方案
Change
mContainer.removeView(commentItem.getRootView());
To
mContainer.removeView(mContainer.getFocusedChild());
Try this
推荐阅读
- codeigniter - 如何在codeigniter中的数据库中显示dropzone中的图像
- docker - 传感器不会因工人故障而重新安排
- django - 在 DRF 中删除与 PrimaryKeyRelatedField 的多对多关系
- pandas - 与熊猫系列按键分组并导出 to_dict()
- elasticsearch - Elasticsearch 在列表字段中查找唯一项目
- google-apps-script - 谷歌应用脚本超出内存限制
- swift - 点击单元格后在第二个 UI 集合视图中加载特定项目?
- gradle - Kotlin 添加集成测试模块
- pyqt - Pyqt5 两个 QThread 通信使用信号和槽问题
- java - 只有有限的 mqtt 客户端连接到 IBM MQ 代理