我在recyclerView中的片段事务中有错误

时间:2016-10-11 13:32:16

标签: java android android-fragments android-recyclerview recycler-adapter

My Fragment在我的RecyclerView.Adapter

中不起作用

我已经在这方面工作了很多天但是无法解决它。 我的片段在我的应用程序中的其他位置工作,但不在我的RecyclerView中。 事件不要在我的应用程序中调用(getSuppourtFragmentManager)。 我认为我的代码是错误的。 任何人都可以帮助我。

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


        }
    }
}   

1 个答案:

答案 0 :(得分:0)

请尝试此代码它会帮助您

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

            }
        }
    });



    }
}

}