我正在尝试实现ContextMenu,但通过这样做,我检查了我的listview无法点击。
我根据这个主题浏览了不同的主题并尝试了所有提议android:focusable="false" android:focusableInTouchMode="false"
甚至
descendantFocusability="blockDescendants"
但仍然没有工作
这是我的主要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个删除功能,我测试的原因我目前正在执行)。
如需其他任何详细信息,请询问。
亚历。
答案 0 :(得分:1)
我终于尝试了几件事并找到了我的解决方案(对他们来说可能有用)。
首先,我必须从我的所有xml
中删除与“clickable”属性相关的所有内容然后,如果你检查我的列表项xml,我有2个布局(2个线性,不知道这可能与Relative不同,没有测试这个)。 我做的是删除第二个,并将核心LinearLayout修改为相对布局(以显示我想要的元素)。
然后我又推出了我的应用程序,它完全正常工作。
亚历。