【问题标题】:Can't get CoordinatorLayout inside RecylerAdapter via findViewById无法通过 findViewById 在 RecylerAdapter 中获取 CoordinatorLayout
【发布时间】:2023-03-18 23:30:01
【问题描述】:

我想做什么?

我有一个RecyclerView,它保留行并且看起来像ListView。这些列表的行在feed_listview_row.xml 中定义。我有一个RecyclerAdapter 用于这个RecyclerView,它被称为Feed_Recycler_Adapter.java,你可以在下面看到。

当用户 Long 单击其中一行时,我想显示 AlertDialog。有一个关于用户是要删除还是保留此行的问题。用户接受后删除该行。将有SnackBar 通知用户并给她/他最后一次取回的机会。

问题

我无法查看SnackBar,因为我无法显示CoordinatorLayout

代码

Feed_Recycler_Adapter.java

holder.layout.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
 public boolean onLongClick(View v) {

  cl = (CoordinatorLayout) v.findViewById(R.id.FeedCoordinatorLayout); 
   /*this row above is failing to fill the cl variable. 
    This cl variable returns null! I need to get the coordinatorlayout for snackbar*/
    String selectedListId = mDataset.get(position).getListId();
    String selectedPostId = mDataset.get(position).getPostId();
    Push_Options.ownerOrfollower(v.getContext(), selectedListId, selectedPostId, cl);
              return true;
                    }
                });

我试过了

我尝试在 Feed_Recycler_Adapter.ViewHolder onCreateViewHolder 中为 content_feed 充气,并将其用作获取 FeedCoordinatorLayout 的视图,但没有解决问题,但我没有收到任何错误,因为 cl 不为空。但也没有显示SnackBar

【问题讨论】:

    标签: android android-recyclerview android-context android-snackbar snackbar


    【解决方案1】:

    我在 dariush f 的回答的帮助下找到了解决方案。

    我修改了现有的构造函数,使其具有 CoordinatorLayout 字段。

    class Feed_Recycler_Adapter extends RecyclerView.Adapter<Feed_Recycler_Adapter.ViewHolder> {
    
    public static String TAG = "Feed Recycler Adapter";
    private CoordinatorLayout cl;
    private ArrayList<Feed_List_Class> mDataset;
    //CoordinatorLayout cl;
    // Provide a suitable constructor (depends on the kind of dataset)
    Feed_Recycler_Adapter(ArrayList<Feed_List_Class> myDataset,CoordinatorLayout coordinatorLayout) {
        cl=coordinatorLayout;
        mDataset = myDataset;
    }
    

    当我实例化这个适配器时,我正在发送协调器布局。像这样:

    CoordinatorLayout cl = (CoordinatorLayout) findViewById(R.id.FeedCoordinatorLayout) ;
    mAdapter = new Feed_Recycler_Adapter(posts,cl);
    

    由于我的 onLongClick 方法已经在我的 Adapter 类中,我可以使用我的构造函数设置的 cl 变量创建一个快餐栏。

    Push_Options.ownerOrfollower(v.getContext(), selectedListId, selectedPostId, cl);
    

    【讨论】:

      【解决方案2】:

      我阅读了您的大问题的一部分,发现您需要 rootView (coordinatorLayout)
      这可能对你有帮助:
      使用Context 参数将构造函数添加到YourAdapter

      public yourAdapter(Context activity){
          context = activity // context is local object
      }
      

      现在在您创建 YourAdapter 实例的活动中执行以下操作:

      adapter = new YourAdapter(YourActivity.this); 
      

      当你想显示 snakBar 时:

      Snackbar.make(((YourActivity) context).coordinatorLayout, "Post deleted.", Snackbar.LENGTH_LONG).show()
      

      确保在 YourActivity 类中定义 coordinatorLayout。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-10
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-22
        相关资源
        最近更新 更多