我正在处理一个笔记应用程序,当我长按时,我会在列表视图上显示复选框项目。当用户在复选框可见时尝试返回时,我需要删除复选框可见性。我该怎么做?
public class NotesActivity extends AppCompatActivity {
private ListView mListNotes;
NoteListAdapter na;
private CheckBox mCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
mListNotes = findViewById(R.id.main_listview);
listViewLongClick();
}
private void listViewLongClick() {
mListNotes.setLongClickable(true);
mListNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
for (int index = 0; index < parent.getChildCount(); ++index) {
View nextChild = (parent.getChildAt(index));
mCheckBox = nextChild.findViewById(R.id.checkbox);
mCheckBox.setVisibility(View.VISIBLE);
}
CheckBox checkBox = view.findViewById(R.id.checkbox);
checkBox.setChecked(true);
return true;
}
});
}
@Override
public void onBackPressed() {
if(mCheckBox.isShown()) {
mCheckBox.setVisibility(View.Gone); //i need to remove checkbox visibility here if already visible
}
super.onBackPressed();
}
}
答案 0 :(得分:0)
private boolean isAnyItemChecked = false;
private void listViewLongClick() {
mListNotes.setLongClickable(true);
mListNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
for (int index = 0; index < parent.getChildCount(); ++index) {
View nextChild = (parent.getChildAt(index));
mCheckBox = nextChild.findViewById(R.id.checkbox);
mCheckBox.setVisibility(View.VISIBLE);
}
isAnyItemChecked = true;
return true;
}
});
}
@Override
public void onBackPressed() {
if(isAnyItemChecked) {
//repopulate adapter and attach to list view, so all items will be unchecked
return;
}
super.onBackPressed();
}