Anil Pervaiz Anil Pervaiz - 1 month ago 24
Android Question

I have an error in fragment transaction in a recyclerView

My Fragment does not work in my RecyclerView.Adapter

I have been working on this for many days but couldn't resolve it .
My fragment s works in al other place in my app but not in my RecyclerView.
it event don't get the (getSuppourtFragmentManager) be called in my app.
i think my code is wrong.
can anybody help me with this.

package com.anilpervaiz.saiyahtourisms;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

/**
* Created by e.xone on 10/8/2016.
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {


// FragmentManager mFragmentManager;
//FragmentTransaction mFragmentTransaction;
// private Fragment mFragment;
// private static Fragment fragment;



Context context;
ArrayList<Bitmap> images= new ArrayList<Bitmap>();


public RvAdapter(Context context){

this.context = context;

images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.explorepakistan));
images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tracking));
images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.international));
images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.bikerides));


}

@Override
public RvAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.home_card_view, parent ,false);



return new ViewHolder(view);
}

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

holder.imageView.setImageBitmap(images.get(position));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position == 0)
{
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear_layout,new SentFragment()).commit();

}else if(position == 1)
{
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear_layout,new SentFragment()).commit();

}
}
});


}

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


public class ViewHolder extends RecyclerView.ViewHolder {

ImageView imageView;
public ViewHolder(View itemView) {

super(itemView);


}
}
}

Answer

Please Try this code It will help you

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


ArrayList<Bitmap>  images= new ArrayList<Bitmap>();


public RvAdapter(Context context){

    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.explorepakistan));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tracking));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.international));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.bikerides));


}

@Override
public RvAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.home_card_view, parent ,false);



    return new ViewHolder(view);
}

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

    holder.imageView.setImageBitmap(images.get(position));
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(position == 0)
            {
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.linear_layout,new SentFragment()).commit();

            }else if(position == 1)
            {
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.linear_layout,new SentFragment()).commit();

            }
        }
    });


}

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


public class ViewHolder extends RecyclerView.ViewHolder {

   ImageView imageView;
   Context context;
    public ViewHolder(View itemView) {

    this.context = itemView.getContext();

        super(itemView);
// add your click listner here insted
itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(getPposition == 0)
            {
                FragmentTransaction fragmentTransaction = ((Your Activity Name) context).getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.linear_layout,new SentFragment()).commit();

            }else if(getPposition == 1)
            {
                FragmentTransaction fragmentTransaction = ((Your Activity Name) context).getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.linear_layout,new SentFragment()).commit();

            }
        }
    });



    }
}

}

Comments