Nabin
Nabin

Reputation: 531

how to call getsupportfragmentmanager() from adapter class?

I have ViewPager as the first item inside a RecyclerView.I want to set FragmentStatePagerAdapter to the viewpager inside the onBindViewHolder() method. But i can't call getsupportfragmentmanager().Please help!!. Here's my code:

public class CustomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<RowItem> rowItemList;
    private final int VIEW_TYPE_CUSTOM = 0;
    private final int VIEW_TYPE_NORMAL = 1;
    NewPagerAdapter mCustomPagerAdapter;

    public CustomListAdapter(Context context, List<RowItem> rowItemList) {
        this.context = context;
        this.rowItemList = rowItemList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return VIEW_TYPE_CUSTOM;
        else
            return VIEW_TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {
        return rowItemList.size()+1;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_CUSTOM:
                recViewHolder1 viewHolderSecond = (recViewHolder1) holder;
                    mCustomPagerAdapter = new NewPagerAdapter(getSupportFragmentManager());
                    viewHolderSecond.vPager.setAdapter(mCustomPagerAdapter);
                break;
            case VIEW_TYPE_NORMAL:
                recViewHolder2 viewHolderFirst = (recViewHolder2) holder;
                RowItem rowItem = rowItemList.get(position-1);
                viewHolderFirst.vName.setText(rowItem.getName());
                viewHolderFirst.vImage.setImageResource(rowItem.getImageId());
                break;

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_CUSTOM:
                View itemView = LayoutInflater.
                        from(parent.getContext()).
                        inflate(R.layout.pagerlayout, parent, false);
                return new recViewHolder1(itemView);
            case VIEW_TYPE_NORMAL:
                View itemView1 = LayoutInflater.
                        from(parent.getContext()).
                        inflate(R.layout.rowlayout, parent, false);
                return new recViewHolder2(itemView1);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }


    public static class recViewHolder1 extends RecyclerView.ViewHolder {
        protected static ViewPager vPager;


        public recViewHolder1(View v) {
            super(v);
            vPager = (ViewPager) v.findViewById(R.id.vPager);
        }
    }

    public class recViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener {
        protected  TextView vName;
        protected  ImageView vImage;

        public recViewHolder2(View v) {
            super(v);
            v.setOnClickListener(this);
            vName = (TextView) v.findViewById(R.id.vName);
            vImage = (ImageView) v.findViewById(R.id.vImage);
        } } }

Upvotes: 42

Views: 66063

Answers (12)

하승민
하승민

Reputation: 1

((FragmentActivity)view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.btm_ly, new SettingFragment()).commit();

Upvotes: 0

Wiktor Kalinowski
Wiktor Kalinowski

Reputation: 79

I have implemented this way, in adapter method

override fun onBindViewHolder(holder: TestViewHolder, position: Int) { 
val appCompatActivity = holder.itemView.context as AppCompatActivity
timePickerDialog.show(appCompatActivity.supportFragmentManager, "dialog")
}

This solution works for me, getting context from holder, no need to send context to adapter also sending context in constructor to adapter as far as I know is bad approach.

Upvotes: 0

milad parao
milad parao

Reputation: 26

just pass Avtivity

   `public CustomListAdapter(AppCompatActivity context, List<RowItem> rowItemList) {
    this.context = context;
    this.rowItemList = rowItemList;
}`

and you can do everything that you can do in activities but in your adapter class

Upvotes: 1

Zanyer
Zanyer

Reputation: 61

I worked as follows:

@Override
public void onBindViewHolder(DataAdapter.ViewHolder holder, final int position) {

        holder.txtTitle.setText(list.get(position).getTitle());
        holder.txtMessage.setText(list.get(position).getMessage());

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = list.get(position).getTitle();

                if(title.equals("FRECUENTES")){
                    RequestFrequentFragment fragment = new RequestFrequentFragment(); // you fragment 
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.nav_host_fragment, fragment);// your container and your fragment
                    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                }

            }
        });
}

Upvotes: 6

payal_suthar
payal_suthar

Reputation: 455

 FragmentManager fragmentManager = ((FragmentActivity) view.getContext()).getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Try this ..it worked for me.

Upvotes: 7

Roshna Omer
Roshna Omer

Reputation: 721

For those having this error:

Inconvertible types; cannot cast 'android.content.Context' to YourFragment

I implemented this solution

Update your adapter constructor to accept the Fragment as a parameter.

Something like :

customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1
, getList, YourFragment.this);

and update the constructor of the Adapter :

public CustomAdapter(Context context, int id, YourFragmentfragment) {
    this.fragment = fragment;
}

then you call methods using the fragment variable.

fragment.doSomething();

The answer is by Shivam Verma.

Upvotes: 1

Sheharyar Ejaz
Sheharyar Ejaz

Reputation: 2858

Its Best solution is to pass the reference of your Context to the Adapter from Your Activity (or) Fragment.

    public UsersAdapter(ArrayList<User> items , Context context) {
    usersList = items;
    this.context = context;
    }

Then upcast context to AppCompatActivity like this and get the getSupportFragmentManager

    ((AppCompatActivity) context).getSupportFragmentManager()

Share to improve.

Upvotes: 34

DINA TAKLIT
DINA TAKLIT

Reputation: 8408

For whome using kotlin you can use this solution

val dialog = IntervAddFragment()
       val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
        dialog.show(ft, ContentValues.TAG)

And it will work perfectly inchAllah.

Upvotes: 2

Ignacio_aa
Ignacio_aa

Reputation: 318

Another solution...

Declare context

private Toast mssgeToast;

private Context ctxContext;

public MyViewListener(myView pView){

    this.mView = pView;
    this.ctxContext = pView.getContext();

}

Next...

 public void pushFragment(Context context){
    FragmentManager fm = ((FragmentActivity)context).getSupportFragmentManager();
    PopupClickFragment pcf = new PopupClickFragment();

    pcf.show(fm, "");
   // FragmentTransaction transaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();

}

and declare into onClickListener. Example:

  @Override
    public void onCellClicked (@NonNull RecyclerView.ViewHolder p_jCellView,int p_nXPosition,
    int p_nYPosition){
        //TODO: AQUI SE DEBE LEVANTAR EL POPUP INSTANCIANDO .SHOW();

        pushFragment(ctxContext);
    }

Upvotes: 3

Umberto Emonds
Umberto Emonds

Reputation: 141

Here you can set transition ;)

public void pushFragment(Fragment newFragment, Context context){

    FragmentTransaction transaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, newFragment);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.addToBackStack(null);
    transaction.commit();

}

Upvotes: 12

Anter
Anter

Reputation: 3625

i solved it by casting mContext

((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction()
                            .replace(R.id.item_detail_container, fragment)
                            .commit();

Upvotes: 94

Arun Shankar
Arun Shankar

Reputation: 622

First you have to pass context to adapter constructor.

Then you have to use that context reference to get getSupportFragmentManager.

mCustomPagerAdapter = new NewPagerAdapter(context.getSupportFragmentManager());
viewHolderSecond.vPager.setAdapter(mCustomPagerAdapter);

Upvotes: 4

Related Questions