我正在使用一个Android应用程序,在其中我实现了一个Recyclerview,每个孩子中只有很少的视图分组(请查看此处链接的图像),RecyclerviewItems。单击最右上角的“更多选项”图标时,它将使“动作”布局可见,而该布局是不可见的。 ActionItemsVisibleWhenMoreOptionsIsClicked。我想知道的是,当用户在该布局之外触摸/单击时,如何再次使“动作”布局不可见。可以单击Recyclerview中的其他项目,也可以单击Recyclerview之外的其他项目。
下面是我的子架构的布局xml。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/pure_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/task_detail_layout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:weightSum="5">
<View
android:id="@+id/priority_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:background="@color/high" />
<TextView
android:id="@+id/task_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4.35"
android:background="@color/pure_white"
android:hint="@string/task_description_dummy"
android:padding="10dp"
android:layout_gravity="center"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|end"
android:paddingStart="20dp">
<LinearLayout
android:id="@+id/actions"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/pure_white"
android:orientation="horizontal"
android:paddingStart="20dp"
android:visibility="gone">
<ImageView
android:id="@+id/done_task"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_done_task" />
<ImageView
android:id="@+id/edit_task"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_edit_task" />
<ImageView
android:id="@+id/delete_task"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_delete_task" />
<ImageView
android:id="@+id/empty_view"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/more_options"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|end"
android:backgroundTint="@color/pure_white"
android:background="?android:attr/selectableItemBackground"
android:src="@drawable/ic_more_options" />
</FrameLayout>
<View
android:id="@+id/horizontal_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_gray" />
</LinearLayout>
下面是我的适配器
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder> {
private List<Task> tasks;
private Context context;
static class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout taskDetailLayout;
View priorityView;
TextView taskDescriptionView;
ImageView moreOptions;
LinearLayout actions;
ImageView markTaskCompleted;
ImageView editTask;
ImageView deleteTask;
MyViewHolder(View view) {
super(view);
taskDetailLayout = view.findViewById(R.id.task_detail_layout);
priorityView = view.findViewById(R.id.priority_view);
taskDescriptionView = view.findViewById(R.id.task_description);
moreOptions = view.findViewById(R.id.more_options);
actions = view.findViewById(R.id.actions);
markTaskCompleted = view.findViewById(R.id.done_task);
editTask = view.findViewById(R.id.edit_task);
deleteTask = view.findViewById(R.id.delete_task);
}
}
public TaskAdapter(List<Task> items, Context context) {
this.tasks = items;
this.context = context;
setHasStableIds(true);
}
public void refreshTaskList(List<Task> tasks) {
this.tasks = tasks;
notifyDataSetChanged();
}
@NonNull
@Override
public TaskAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.task_schema, parent, false);
return new TaskAdapter.MyViewHolder(itemView);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
Task currentTask = tasks.get(position);
if (null != currentTask) {
String taskDescription = currentTask.getDescription();
String priority = currentTask.getPriority();
boolean isTaskCompleted = currentTask.isTaskCompleted();
int priorityColor = PRIORITY_LOW.equalsIgnoreCase(priority) ? context.getResources().getColor(R.color.low) :
(PRIORITY_MEDIUM.equalsIgnoreCase(priority) ? context.getResources().getColor(R.color.medium) :
context.getResources().getColor(R.color.high));
holder.taskDescriptionView.setText(taskDescription);
holder.priorityView.setBackgroundColor(priorityColor);
// When user clicks 'More Options' icon show the action icons
holder.moreOptions.setOnClickListener(v -> {
holder.actions.setVisibility(View.VISIBLE);
holder.taskDetailLayout.setAlpha(0.7F);
holder.taskDescriptionView.setAlpha(0.7F);
holder.taskDetailLayout.setBackgroundColor(context.getResources().getColor(R.color.dim_description_color));
holder.taskDescriptionView.setBackgroundColor(context.getResources().getColor(R.color.dim_description_color));
Animation animation = AnimationUtils.loadAnimation(context, R.anim.right_to_left_anim);
holder.actions.startAnimation(animation);
});
// Mark Task related logic
// If task is already completed, show done_mark icon and hide edit icon
if (isTaskCompleted) {
holder.markTaskCompleted.setOnClickListener(null);
holder.markTaskCompleted.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_done_mark));
holder.editTask.setVisibility(View.GONE);
} else {
// When user clicks 'Mark Done' icon show the popup
holder.markTaskCompleted.setOnClickListener(v -> {
new AlertDialog.Builder(context)
.setTitle("Mark Completed")
.setMessage("Nice, Have you completed the task?")
.setNegativeButton("No", null)
.setPositiveButton("Yes", (arg0, arg1) -> {
TaskHelper.markTaskCompleted(context, currentTask.getId());
holder.markTaskCompleted.setOnClickListener(null);
holder.markTaskCompleted.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_done_mark));
holder.editTask.setVisibility(View.GONE);
}).create().show();
;
});
}
// Delete task related logic
// When user clicks 'Delete task' icon
holder.deleteTask.setOnClickListener(v -> {
new AlertDialog.Builder(context)
.setTitle("Delete task")
.setMessage("Are you sure want to delete the task?")
.setNegativeButton("No", null)
.setPositiveButton("Yes", (arg0, arg1) -> {
new HomeScreenActivity().deleteTask(context, currentTask.getId());
holder.actions.setVisibility(View.VISIBLE);
}).create().show();
});
// Edit task related logic
holder.editTask.setOnClickListener(v -> {
Intent editIntent = new Intent(context, AddNewTaskActivity.class);
editIntent.putExtra(TASK_ID, currentTask.getId());
context.startActivity(editIntent);
});
}
}
@Override
public int getItemCount() {
return tasks.size();
}
}
我需要可以在适配器或活动中实现的逻辑/功能。
关于是否隐藏选项菜单,您可以添加一个标志来跟踪ViewHolder是否在“显示”其选项。如果用户点击“更多选项”图标,这至少将为您提供一种简单的方法来再次隐藏菜单。
就知道用户何时单击视图之外...而言,这更加复杂。由于RecyclerView是一个ViewGroup,因此在您的Activity中,您可以使用onInterceptTouchEvent方法拦截RecyclerView的触摸事件。
根据您的布局,您可能还需要被其他视图拦截,或者甚至在整个屏幕上创建一个大的虚拟视图来处理此问题。