android-fragments 如何从RecyclerView.Adapter中打开片段< CardAdapter.ViewHolder>

7tofc5zh  于 2022-11-14  发布在  Android
关注(0)|答案(6)|浏览(125)
  • 选项卡布局
  • 表1(片段1)
  • 表2(片段2)
  • 表3(片段3)
  • 回收机视图+卡片视图(OnClick)

CardView上,ClickListner在tab3中打开另一个片段。那么如何在tab3中打开片段呢?
错误位于getFragmentManager()

FragmentTransaction transaction = getFragmentManager().beginTransaction();

其给出了

/Adapter/CardAdapter.java Error:cannot find symbol method getSupportFragmentManager()

我没有这样做,而是试着:

FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
FragmentTransaction transaction = itemview.getContext().getFragmentManager().beginTransaction();

但错误是无法解决的。
下面是我的代码:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    List<NatureItem> mItems;
    private int lastPosition = -1;
    Context context;
    TaskFragment main;
    public CardAdapter(Context context,TaskFragment ma)
    {

        this.context=context;
        main=ma;
    }

    public CardAdapter() {
        super();
        mItems = new ArrayList<NatureItem>();
        NatureItem nature = new NatureItem();
        nature.setName("The Paris Attack 2015");
        nature.setDes("Lorem ipsum dolor sit amet.");
        nature.setThumbnail(R.drawable.news1);
        mItems.add(nature);

           }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.custom_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);

     return viewHolder;
    }

  
     @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        NatureItem nature = mItems.get(i);
        viewHolder.tvNature.setText(nature.getName());
        viewHolder.tvDesNature.setText(nature.getDes());
        viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
      //  setAnimation(viewHolder.card,i);
    }

   
    @Override
    public int getItemCount() {
        return mItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private int lastPosition = -1;
        public ImageView imgThumbnail;
        public TextView tvNature;
        public TextView tvDesNature;
       // Button btnclear,btncancle;
        CardView card;
        Activity activity;
        Context co;
        public ViewHolder(final View itemView) {
            super(itemView);
            imgThumbnail = (ImageView)     itemView.findViewById(R.id.img_thumbnail);
            tvNature = (TextView) itemView.findViewById(R.id.tv_nature);
            tvDesNature = (TextView) itemView.findViewById(R.id.tv_des_nature);
            card = (CardView) itemView.findViewById(R.id.card);


    card.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        
        Toast.makeText(itemView.getContext(), "Clicked Card...", Toast.LENGTH_LONG).show();
       
           ShareFragment newFragment = new ShareFragment();
            FragmentTransaction transaction =  getFragmentManager().beginTransaction();
            transaction.replace(R.id.viewFragments, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();

            }
    });
        
        }
    }
}
7rtdyuoh

7rtdyuoh1#

在onclick中按如下方式打开新片段

@Override
public void onClick(View view){
    AppCompatActivity activity = (AppCompatActivity) view.getContext();
    Fragment myFragment = new MyFragment();
    activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
}
ikfrs5lh

ikfrs5lh2#

当你使用adapter上下文时,为了访问片段管理器,你需要转换你的context。所以你应该这样使用。

YourParentActivity myActivity = (YourParentActivity)context

现在,您可以访问片段管理器方法,如下所示:

myActivity.getSupportFragmentManager();

但是请记住,您的Fragment应该作为android.support.app.v4.Fragment导入,否则可能会出现转换问题。
如果打开某个选项卡的片段,则应使用getChildSupportFragmentManager()而不是getSupportFragmentManager()

**注意:**如果要从adapter类调用fragment,则应创建interface并将侦听器传递给按钮单击方法,然后使用该接口实现Activity。
更新日期:

您也可以将FragmentManager传递给您的适配器构造函数。

public FragmentManager f_manager;
public CardAdapter(Context context,TaskFragment ma , FragmentManager f_manager)
{
    this.context = context;
    this.f_manager = f_manager;
    main=ma;
}

之后,您可以在适配器类getView()方法中使用this.f_manager

cetgtptt

cetgtptt3#

如果您使用了支持库片段或默认片段,请确保在任何地方都使用相同的片段。并小心使用“getSupportFragmentManager”或“getFragmentManager”。

在构造函数内传递上下文。

public CardAdapter(Context context) {
        super();
        mItems = new ArrayList<NatureItem>();
        NatureItem nature = new NatureItem();
        nature.setName("The Paris Attack 2015");
        nature.setDes("Lorem ipsum dolor sit amet.");
        nature.setThumbnail(R.drawable.news1);
        mItems.add(nature);
               }

单击时内部

....Your Code

ShareFragment newFragment = new ShareFragment();
FragmentTransaction transaction = /* Here is the change.*/context.getFragmentManager().beginTransaction();
    transaction.replace(R.id.viewFragments, newFragment);

...Your Code

更新

Inside onClick call mainActivity setFragment method to replace fragment.

((MainActivity) context).setFragment(yourFragment);

public void setFragment(Fragment frag){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.viewFragments, frag);
}
uinbv5nw

uinbv5nw4#

要从一个片段移动到另一个片段(从RecyclerView MyViewHolder类),请使用此

Fragment fragment = new AttendanceFragment();
FragmentManager fragmentManager = ((FragmentActivity) mContext).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
yvfmudvl

yvfmudvl5#

对于使用Kotlin的代码,假设你要启动的fragment类名为MyFragment,并且你要放置这个fragment的容器的id为fragment_container_view,你可能会在视图保持器中的bind方法中执行此操作。

itemView.setOnClickListener {
    val activity  = it.context as? AppCompatActivity
    activity?.supportFragmentManager?.commit {
        addToBackStack(MyFragment::class.toString())
        setReorderingAllowed(true)
        replace<MyFragment>(R.id.fragment_container_view)
    }
}
1mrurvl1

1mrurvl16#

试试这个狙击:

ShareFragment newFragment = new ShareFragment();
MainActivity mainActivity = (MainActivity)itemView.getContext()
mainActivity.getFragmentManager().beginTransaction()
.replace(R.id.viewFragments, newFragment)
.addToBackStack(null)
.commit();

相关问题