如何从RecyclerView.Adapter打开片段<cardadapter.viewholder>

时间:2015-12-16 11:15:51

标签: android android-fragments android-recyclerview android-viewholder

1.TabLayout

- tab1 (Fragment1)
- tab2 (Fragment2)
- tab3 (Fragment3)
     * RecyclerView + CardView (OnClick)

CardView ClickListner上打开tab3中的另一个片段。那么如何在tab3中打开片段。

错误发生在getFragmentManager()

FragmentTransaction transaction = getFragmentManager().beginTransaction();

而不是这样,我试过:

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();

            }
    });

        }
    }
}

6 个答案:

答案 0 :(得分:18)

在你的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();


        }

答案 1 :(得分:15)

问题:

Error:cannot find symbol method getSupportFragmentManager() 

解决方案:

当您使用adapter上下文然后访问片段管理器时,您需要投射context。所以你应该这样使用。

YourParentActivity myActivity = (YourParentActivity)context

现在您可以访问片段管理器的方法,如

myActivity.getSupportFragmentManager();

但请记住,您的Fragment应该导入为android.support.app.v4.Fragment,否则可能会出现投射问题。

如果您为特定的一个标签打开片段,则应使用getChildSupportFragmentManager()代替getSupportFragmentManager()

注意:如果您想从fragment课程致电adapter,那么您应该制作interface并将听众传递给您的按钮点击方法并实施您的活动用那个界面。

更新:

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

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

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

答案 2 :(得分:7)

如果您使用过支持库片段或默认片段,请确保每个地方都使用相同的片段。并使用&#34; getSupportFragmentManager&#34;或&#34; getFragmentManager&#34;小心。

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

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);
               }

在onClick内

....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);
}

答案 3 :(得分:1)

要从一个片段移动到另一个片段(从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();

答案 4 :(得分:0)

试试这个sniped:

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

答案 5 :(得分:0)

对于使用Kotlin的那些,假定要启动的片段类称为MyFragment,放置该片段的容器的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)
    }
}