【问题标题】:getSupportFragmentManager() undefined in adapter when using AppCompatActivity使用 AppCompatActivity 时适配器中未定义 getSupportFragmentManager()
【发布时间】:2016-08-05 02:56:34
【问题描述】:

我有一个扩展 AppCompatActivity 的活动 (MainActivity),因为我在我的应用中使用了一些材料设计元素。

然后我有一个带有几个字段和一个按钮的数组适配器。这个适配器有一个单独的视图,并被注入到我的 MainActivity 布局中。

当我单击适配器视图上的按钮时,我想打开一个显示一堆文本的新片段,但是,我似乎不能这样做,我认为这是因为我没有在我的主要活动?我在另一篇文章中读到,我应该能够扩展 AppCompatActivity 并且仍然能够引用片段管理器......这是我打开片段的代码:

在我的自定义数组适配器中,一个按钮的 onClick():

holder.desc.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
      JobDescFragment fragment= new JobDescFragment();
      FragmentTransaction transaction =      getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.fragment_container, fragment);
      transaction.addToBackStack(null);  
      transaction.commit();
  }
});

我得到的错误是它无法解析 getSupportFragmentManager()。我究竟做错了什么?

我正在适配器中导入 android.support.v4.app.Fragment 和 .FragmentManager。

提前感谢您的帮助!

编辑:

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:background="@color/laborswipe_lightgray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".MainActivity"
        android:layout_gravity="top"   />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</merge>

【问题讨论】:

  • 分享包含 fragment_container 的布局文件可能会有所帮助

标签: android android-layout android-fragments android-arrayadapter android-fragmentactivity


【解决方案1】:

你可以试试这个

FragmentTransaction ft = ((AppCompatActivity) mContext).getSupportFragmentManager()
                        .beginTransaction();

【讨论】:

    【解决方案2】:

    例如,在 kotlin 中会是这样。我试过了,效果很好

    val dialog = IntervAddFragment()//The fragment that u want to open for example
           val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
            dialog.show(ft, ContentValues.TAG)
    

    【讨论】:

      【解决方案3】:

      嗨,这很简单,只需传递 YourParent Activity 的引用 只需在您的适配器类中使用如下所示的 sn-p 即可打开片段

      Fragment fragment = new YourFragment();
      FragmentManager fm = ((MainActivity) mContext).getSupportFragmentManager();
                      FragmentTransaction ft = fm.beginTransaction();
                      ft.add(R.id.frame_container, fragment);
                      ft.commit();
      

      【讨论】:

        【解决方案4】:

        在适配器中使用

        appCompatActivity 对象而不是上下文

        会有用的

        【讨论】:

        • 我在适配器的什么地方使用它?我无法扩展它,所以我正在尝试这样 ((AppCompatActivity)context).getSupportFragmentManager() ?
        • 我将添加另一个答案检查它.. 使用这种方式
        • ArrayAdapter里面的上下文可能是ApplicationContext,所以投出来的风险很大。
        【解决方案5】:

        在适配器中传递上下文并使用

        context.getSupportFragmentManager().beginTransaction();
        

        【讨论】:

        • 我试过了,但没有运气!它仍然说它无法解析 getSupportFragmentManager()。
        • 使用 AppCompatActivity appCompatActivity;
        【解决方案6】:

        避免不需要的代码

         public class AdapterRecyclerViewDoc extends RecyclerView.Adapter<AdapterRecyclerViewDoc.ShareDocViewHolder> {
                ArrayList<ModelDoc> arrayList;
        
                AppCompatActivity appCompatActivity;
        
                public AdapterRecyclerViewDoc(ArrayList<ModelDoc> arrayList, AppCompatActivity appCompatActivity) {
                    this.arrayList=arrayList;
                    this.appCompatActivity=appCompatActivity;
                }
        
                @Override
                public ShareDocViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        
                    View v = LayoutInflater.from(parent.getContext()).inflate
                            (R.layout.recyclerview_item_doc, parent, false);
                    ShareDocViewHolder eventViewHolder = new ShareDocViewHolder(v);
                    this.appCompatActivity.getSupportFragmentManager();
                     return eventViewHolder;
                }
        

        【讨论】:

        • 该代码如何打开片段?如果我这样做 this.appCompatActivity 我仍然无法获得片段管理器。这样做 ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction() 可以解决无法解决的错误,但我仍然没有看到片段打开。
        【解决方案7】:

        要使代码更加灵活和独立的逻辑,您应该了解组件的职责。很明显,您的适配器不应该知道 Fragment 以及 Activity 的基类是什么。那你该怎么办?

        在您的活动中,您应该为适配器内的项目单击事件设置一个侦听器:

        public class YourActivity extends AppCompatActivity {
        
             @Override
             protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_your);
                  //... some findViewById, etc.
                  YourAdapter adapter = new YourAdapter(this, data, new OnDetailsRequestListener(){
                       public void onDetailsRequested(int itemId){
                            DetailsFragment fragment = DetailsFragment.newInstance(itemId);
                            getSupportFragmentManager().beginTransaction()
                                 .replace(R.id.fragment_container, fragment)
                                 .addToBackStack(null)
                                 .commit();
                       });
                    //... recyclerView.setAdapter ... etc.
        
             }
        
         public interface onDetailsRequestListener{
              void onDetailsRequestLister(int itemId);//here could be whatever you want, itemId, details, whatever...
         }
        }
        

        在您的适配器内部,您将拥有:

        //...
            public AdapterConstructor(Context context, ArrayList data, YourActivity.OnDetailsRequestListener detailsRequestListener) {
               this.data = data;
               this.context = context;
               this.detailsRequestListener = detailsRequestListener;
               //...
            }
            //Inside bindView
            holder.desc.setTag(position);
            holder.desc.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                  int position = (int)view.getTag();
                  int itemId = data.get(position).getId();
                  detailsRequestListener.onDetailsRequested(itemId);
               }
            });
        

        【讨论】:

        • 感谢您的帮助!我正在 AsyncTask 中创建适配器,这是我的主要活动的一部分,我尝试调整您的代码以将 onCreate 中的内容移动到 AsyncTask 的 onPostExecute 部分,但这不起作用,它无法解析我的 OnDetailsRequestListener 方法在我的活动中创建。有什么想法吗?
        【解决方案8】:

        来自你的班级

          YourAdapter=new YourAdapter(context, list, YourClass.this.getSupportFragmentManager)
        

        适配器

          public Adapter(context, list, FragmentManager manager){ 
          this.manager=manager;
        

        动作

         FragmentTransaction ft = fragmentcontext.beginTransaction();
                    SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
                    newFragment.setArguments(bundle);
                    newFragment.show(ft, "slideshow");
        

        【讨论】:

          【解决方案9】:
          FragmentTransaction trans =((FragmentActivity)context).getSupportFragmentManager()
                                  .beginTransaction();
          

          如果你试试这个,你会发现它有效。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多