如何在静态类中使用getFragmentManager()

时间:2018-04-21 15:35:40

标签: firebase fragment

public static class viewholder extends RecyclerView.ViewHolder  {       
    public void yorum_fragment( final String key){
        yorum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Comment_Fragment fragment=new Comment_Fragment();
                Bundle bundle=new Bundle();
                bundle.putString("key",key);

                fragment.setArguments(bundle);

                fragmentManager = getFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.login, fragment);

                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
    }
}

如何在此课程中拨打fragmentgetFragmentManager需要一个非静态类。我试着打电话给context,但这不起作用。

2 个答案:

答案 0 :(得分:0)

我解决了 如果我们添加这些行

 FragmentActivity activity = (FragmentActivity)vieww.getContext();
                   FragmentManager manager = activity.getSupportFragmentManager();

工作。

答案 1 :(得分:0)

getFragmentManager方法属于Fragment类。 我创建了FragmentManager类的对象,并将其与getFragmentManager方法同步。

Fragment dd = new Fragment();
    FragmentManager fragmentManager ;
    fragmentManager = dd.getFragmentManager();


            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            AkisFragment NAME = new AkisFragment();
            fragmentTransaction.replace(R.id.fragment_tutucu, NAME);
            fragmentTransaction.commit();