ListView即使具有DescendantFocusability也无法单击
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView即使具有DescendantFocusability也无法单击相关的知识,希望对你有一定的参考价值。
我正在尝试实现ContextMenu,但通过这样做,我检查了列表视图是否不可单击。
我根据该主题遍历了不同的主题,并尝试了所有建议的android:focusable="false" android:focusableInTouchMode="false"
甚至descendantFocusability="blocksDescendants"
但仍然无法正常工作
这是我的主要xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomePage">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnAddTask"
android:id="@+id/btnAddTask"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="goToAddTask" />
<ListView
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listViewTasks"
android:layout_alignParentStart="true"
android:layout_above="@+id/btnAddTask"
android:layout_below="@+id/txtTaskList"
android:longClickable="true"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtTaskList"
android:id="@+id/txtTaskList"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
这是我的list_item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentStart="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:src="@drawable/no_cross"
android:onClick="goToDeleteTask"
android:layout_gravity="center_vertical"
android:contentDescription="Delete button" />
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/txtTaskListName"
android:id="@+id/txtTaskListName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/txtTaskListDate"
android:id="@+id/txtTaskListDate"
android:layout_below="@+id/txtTaskListName"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/txtTaskListTime"
android:id="@+id/txtTaskListTime"
android:layout_marginLeft="10dp"
android:layout_below="@+id/txtTaskListName"
android:layout_toEndOf="@+id/txtTaskListDate"
android:layout_marginStart="24dp" />
</LinearLayout>
</LinearLayout>
最后是我的Java代码:
public class HomePage extends ActionBarActivity {
final Context context = this;
private static final int EDIT = 0, DELETE = 1;
DatabaseHandler dbHandler;
ListView taskListView;
ArrayAdapter<Task> taskAdapter;
int longClickedItemIndex;
List<Task> Tasks = new ArrayList<Task>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
dbHandler = new DatabaseHandler(getApplicationContext());
taskListView = (ListView) findViewById(R.id.listViewTasks);
// The following piece of code allows to empty database before testing when reinstalling the app for test.
//getApplicationContext().deleteDatabase("myTasks");
registerForContextMenu(taskListView);
taskListView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
taskListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemIndex = position;
return false;
}
});
if (dbHandler.getTasksCount() != 0)
Tasks.addAll(dbHandler.getAllTasks());
populateTasksList();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class TaskListAdapter extends ArrayAdapter<Task> {
public TaskListAdapter(){
super(HomePage.this, R.layout.tasklist_item, Tasks);
}
@Override
public View getView(int position, View view, ViewGroup parent){
if (view == null) {
view = getLayoutInflater().inflate(R.layout.tasklist_item, parent, false);
}
Task currentTask = Tasks.get(position);
TextView taskListName = (TextView) view.findViewById(R.id.txtTaskListName);
taskListName.setText(currentTask.getTitle());
TextView taskListDate = (TextView) view.findViewById(R.id.txtTaskListDate);
taskListDate.setText(currentTask.getDate());
TextView taskListTime = (TextView) view.findViewById(R.id.txtTaskListTime);
taskListTime.setText(currentTask.getTime());
/*ImageButton deleteButton = (ImageButton) view.findViewById(R.id.btnDelete);
//deleteButton.setTag(currentTask.getId());
deleteButton.setTag(R.id.taskId,currentTask.getId());
deleteButton.setTag(R.id.position,position);*/
return view;
}
}
private void populateTasksList() {
taskAdapter = new TaskListAdapter();
taskListView.setAdapter(taskAdapter);
}
public void goToAddTask(View view){
Intent intent = new Intent(this, AddTask.class);
startActivity(intent);
}
public void goToDeleteTask(final View view){
AlertDialog alertDialog = new AlertDialog.Builder(context)
// Set Dialog Icon
.setIcon(R.drawable.ic_bullet_key_permission)
// Set Dialog Title
.setTitle(R.string.removeAlert)
// Set Dialog Message
.setMessage(R.string.deleteMessage)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ImageButton deleteButton = (ImageButton) view.findViewById(R.id.btnDelete);
// Retrieving the task to delete
int taskId = (int) deleteButton.getTag(R.id.taskId);
Task taskToDelete = dbHandler.getTask(taskId);
// Removing the task from the database and from the calendar
dbHandler.deleteTask(taskToDelete, context);
// Removing the task from the listView
TaskListAdapter tAdapter = (TaskListAdapter)taskListView.getAdapter();
int position = (int)deleteButton.getTag(R.id.position);
tAdapter.remove(tAdapter.getItem(position));
tAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), R.string.removedTask, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
}
})
.create();
alertDialog.show();
}
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderIcon(R.drawable.edit_pencil_icon);
menu.setHeaderTitle(R.string.contextMenu_title);
menu.add(menu.NONE, EDIT, menu.NONE, R.string.contextMenu_edit);
menu.add(menu.NONE, DELETE, menu.NONE, R.string.contextMenu_delete);
}
public boolean onContextItemSelected (MenuItem item) {
switch (item.getItemId()) {
case EDIT:
// TODO : Implement editing a task
break;
case DELETE:
deleteTask();
//TODO : to be tested
/*
dbHandler.deleteTask(Tasks.get(longClickedItemIndex);
Tasks.remove(longClickedItemIndex);
taskAdapter.notifyDataSetChanged();
*/
break;
}
return super.onContextItemSelected(item);
}
public void deleteTask(){
AlertDialog alertDialog = new AlertDialog.Builder(context)
// Set Dialog Icon
.setIcon(R.drawable.ic_bullet_key_permission)
// Set Dialog Title
.setTitle(R.string.removeAlert)
// Set Dialog Message
.setMessage(R.string.deleteMessage)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ImageButton deleteButton = (ImageButton) findViewById(R.id.btnDelete);
// Retrieving the task to delete
int taskId = (int) deleteButton.getTag(R.id.taskId);
Task taskToDelete = dbHandler.getTask(taskId);
// Removing the task from the database and from the calendar
dbHandler.deleteTask(taskToDelete, context);
// Removing the task from the listView
TaskListAdapter tAdapter = (TaskListAdapter) taskListView.getAdapter();
int position = (int) deleteButton.getTag(R.id.position);
tAdapter.remove(tAdapter.getItem(position));
tAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), R.string.removedTask, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
alertDialog.show();
}
}
((2个删除功能,我当前正在执行的测试原因)。>>
我正在尝试实现ContextMenu,但是通过这样做,我检查了列表视图是否不可单击。我根据此主题遍历了不同的线程,并尝试了所有建议的android:...
答案
我终于尝试了几件事,找到了解决方案(可能对谁有用)。
以上是关于ListView即使具有DescendantFocusability也无法单击的主要内容,如果未能解决你的问题,请参考以下文章
即使使用 Sql 数据库和游标调用 notifyDataSetChanged(),Listview 也不会自动更新
即使收到更新的数据,Flutter ListView.builder 的 UI 也不会被 getx 更新
风味“nativescript-ui-listview”具有未知维度“nativescript-ui-listview”