【问题标题】:popBackStack() and pass argument in AdapterpopBackStack() 并在适配器中传递参数
【发布时间】:2016-05-23 10:31:57
【问题描述】:

点击 RecyclerView 中的行时,我想回到上一个片段。那么如何在我的适配器中调用该函数呢?

 @Override
    public void onBindViewHolder(final CountryViewHolder holder, int position) {
        String country = countries.get(position);
        holder.tvCountrySelection.setText(country);
        holder.tvCountrySelection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.tvCountrySelection.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_tick_17, 0);
                //I want to run popBackStack() here and send String country to previous fragment.
            }
        });
    }

【问题讨论】:

    标签: android android-fragments android-recyclerview


    【解决方案1】:
    • 通过适配器构造函数传递活动上下文

      来自片段

         new YourAdaptor(getActivity())
      

      来自活动

        new YourAdaptor(this)
      
    • 在适配器类中处理上下文

        globalinstance = passedContext;
      
    • 使用该上下文,您可以在点击事件中弹出片段,例如

        ((YourActivity)context).getSupportFragmentManager().popBackStackImmediate();
      

    根据您的需要,您可以使用popBackStack()popBackStackImmediate()

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2019-08-30
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多