【问题标题】:How to hide a sub view in one of the child views in Recyclerview when clicked outside of the view or anywhere else on screen?在视图之外或屏幕上的任何其他位置单击时,如何在 Recyclerview 的一个子视图中隐藏子视图?
【发布时间】:2020-05-21 15:16:21
【问题描述】:

我正在开发一个 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();
}

}

我需要可以在适配器或活动中实现的逻辑/功能。

【问题讨论】:

  • Hey Agniraj,请注意别人的时间:不要让别人写代码。如果回答的人给了你一个有效的关于如何解决问题的高级描述,那是一个可以接受的答案。如果他们还想提供代码示例,那真的取决于他们。无论如何,极不可能有人可以为您提供无需任何更改即可为您工作的可复制粘贴代码。
  • 是的..对@rock3r。我不需要复制粘贴代码。我只是想知道我们如何实现它。因为我尝试了几乎所有可能的方法,但对我来说没有任何效果。感谢您的补充。

标签: android android-layout android-recyclerview onclicklistener


【解决方案1】:

在知道是否隐藏选项菜单方面,您可以添加一个标志来跟踪 ViewHolder 是否“显示”其选项。如果用户点击“更多选项”图标,这至少会为您提供一种再次隐藏菜单的简单方法。

就知道用户何时在视图之外单击...这更复杂。由于 RecyclerView 是一个 ViewGroup,因此在您的 Activity 中,您可以使用 onInterceptTouchEvent 方法拦截 RecyclerView 的触摸事件。

根据您的布局,您可能还需要被其他视图拦截,或者甚至在整个屏幕上创建一个大的虚拟视图来处理这个..

【讨论】:

  • 感谢您的回答瑞恩。来到你的第 1 点。是的,对。当用户再次单击“更多操作”图标时,标志将更容易隐藏布局。但用户不必总是遵循这一点。所以我需要一种整体方法来检测特定操作布局之外的点击。 Point 2. OnInterceptTouch 将处理整个视图的触摸,如果我没记错的话,如何在单击另一个孩子时隐藏一个孩子的动作布局?
猜你喜欢
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多