当前位置:  开发笔记 > Android > 正文

如何从RecyclerView.Adapter打开片段<CardAdapter.ViewHolder>

如何解决《如何从RecyclerView.Adapter打开片段<CardAdapter.ViewHolder>》经验,为你挑选了3个好方法。

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 {

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

            }
    });

        }
    }
}

Parinda Raja.. 25

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


        }


Piyush.. 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()方法中使用.



1> Parinda Raja..:

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


        }



2> Piyush..:

问题:

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()方法中使用.



3> Tushar Sheth..:

如果您使用了支持库片段或默认片段,请务必在每个位置使用相同的片段.并仔细使用"getSupportFragmentManager"或"getFragmentManager".

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

public CardAdapter(Context context) {
        super();
        mItems = new ArrayList();
        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);
}

推荐阅读
linjiabin43
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有