【问题标题】:Hiding toolbar on long press长按隐藏工具栏
【发布时间】:2021-03-06 07:56:23
【问题描述】:

我想在适配器类中长按时隐藏工具栏。但无论我选择哪种方式,它总是给我错误,即尝试调用虚拟方法'void androidx.appcompat.widget.Toolbar.setVisibility(int)' on a null object reference 或类似的东西。这是我的方法

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

androidx.appcompat.widget.Toolbar toolbar;

  public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main,parent,false);
      public void onBindViewHolder(MyAdapter.MyViewHolder holder, int  position) {

        holder.item.setOnLongClickListener(new View.OnLongClickListener() {
           @Override
           public boolean onLongClick(View v) {
               MenuItem menuItem  = mode.getMenu().findItem(R.id.my_toolbar);
               menuItem.setVisible(false); or
               toolbar = view.findViewById(R.id.my_toolbar);
               toolbar.setVisibility(View.GONE);

这是工具栏所在的片段活动的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".FragmentOne">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary" />

         <androidx.recyclerview.widget.RecyclerView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:listitem="@layout/main"
             android:id="@+id/RV"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • modeview 代表什么?
  • onCreateActionMode(ActionMode mode, Menu menu) 在长按方法和视图在适配器类的onCreateViewHolder中创建。
  • 适配器的每一行都有Toolbar吗?
  • 我只是在长按监听器中初始化它,如上图所示,但另一个片段中有一个工具栏具有相同的 id,所以我更改了它们但结果相同
  • 在您的代码示例中实际上没有专门针对view 的声明?唯一引用它的行是view.findViewById(R.id.my_toolbar);

标签: android android-toolbar show-hide


【解决方案1】:

这里有一些问题,根据您的 cmets,似乎 view 变量正在为 onCreateViewHolder 上的每一行重新分配,所以当涉及到 onBindViewHolder 时,您无法保证当前引用是相关的到您要绑定的视图。相反,您应该从视图持有者中检索绑定视图,例如:

holder.itemView

无论如何,您希望通过在您的RecyclerView 的一个子节点上调用findViewById 来找到与您的RecyclerView 位于同一片段中的Toolbar。如果您查看findViewById 的文档或源代码,您会发现它向下遍历视图层次结构,查看子视图以进行匹配,因此它永远不会到达您的Toolbar


一个肮脏的解决方案是在设置 RecyclerView 时传递对 Toolbar 的引用,但推荐的方法是将回调传递给适配器,该回调可以由片段设置并在何时调用长按你的itemView

【讨论】:

  • view = inflater.inflate(R.layout.fragment_One, container, false);此视图位于 FragmentOne 类的 onCreateView 中,该类包含具有该工具栏的 fragment_One.xml。根据您的回答,我认为我需要这种观点。但是如何在适配器类中使用该视图?
  • 如果您阅读了我回答中的建议,我会将回调传递给您的适配器,而不是视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2023-03-26
  • 2023-03-27
相关资源
最近更新 更多