Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我在recyclerView中的片段事务中出错_Java_Android_Android Fragments_Android Recyclerview - Fatal编程技术网

Java 我在recyclerView中的片段事务中出错

Java 我在recyclerView中的片段事务中出错,java,android,android-fragments,android-recyclerview,Java,Android,Android Fragments,Android Recyclerview,我的片段在我的RecyclerView.Adapter中不起作用 我已经为此工作了很多天,但无法解决它。 我的碎片s在我的应用程序中的所有其他位置都有效,但在我的RecyclerView中不起作用。 it事件无法在我的应用程序中调用(GetSupplourtFragmentManager)。 我想我的代码错了。 有人能帮我吗 package com.anilpervaiz.saiyahtourisms; import android.content.Context; import androi

我的片段在我的RecyclerView.Adapter中不起作用

我已经为此工作了很多天,但无法解决它。 我的碎片s在我的应用程序中的所有其他位置都有效,但在我的RecyclerView中不起作用。 it事件无法在我的应用程序中调用(GetSupplourtFragmentManager)。 我想我的代码错了。 有人能帮我吗

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


        }
    }
}   
package com.anilpervaiz.saiyahtourism;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.support.v4.app.FragmentTransaction;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.Toast;
导入java.util.ArrayList;
/**
*由e.xone于2016年8月10日创建。
*/
公共类RvAdapter扩展了RecyclerView.Adapter{
//碎片管理器;
//零碎交易;
//私人碎片;
//私有静态片段;
语境;
ArrayList images=新的ArrayList();
公共RvAdapter(上下文){
this.context=上下文;
add(BitmapFactory.decodeResource(context.getResources(),R.drawable.explorepakistan));
add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tracking));
add(BitmapFactory.decodeResource(context.getResources(),R.drawable.international));
add(BitmapFactory.decodeResource(context.getResources(),R.drawable.bikerides));
}
@凌驾
public RvAdapter.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图视图=LayoutFlater.from(上下文)。充气(R.layout.home\u card\u视图,父视图,false);
返回新的ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(最终RvAdapter.ViewHolder,最终int位置){
holder.imageView.setImageBitmap(images.get(position));
holder.imageView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
如果(位置==0)
{
FragmentTransaction FragmentTransaction=MFFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear_布局,new SentFragment()).commit();
}否则如果(位置==1)
{
FragmentTransaction FragmentTransaction=MFFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear_布局,new SentFragment()).commit();
}
}
});
}
@凌驾
public int getItemCount(){
返回图像。size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
图像视图图像视图;
公共视图持有者(视图项视图){
超级(项目视图);
}
}
}   

请尝试此代码,它将帮助您

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

            }
        }
    });



    }
}
公共类RvAdapter扩展了RecyclerView.Adapter{ ArrayList images=新的ArrayList(); 公共RvAdapter(上下文){ add(BitmapFactory.decodeResource(context.getResources(),R.drawable.explorepakistan)); add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tracking)); add(BitmapFactory.decodeResource(context.getResources(),R.drawable.international)); add(BitmapFactory.decodeResource(context.getResources(),R.drawable.bikerides)); } @凌驾 public RvAdapter.ViewHolder onCreateViewHolder(视图组父级,int-viewType){ 视图视图=LayoutFlater.from(上下文)。充气(R.layout.home\u card\u视图,父视图,false); 返回新的ViewHolder(视图); } @凌驾 public void onBindViewHolder(最终RvAdapter.ViewHolder,最终int位置){ holder.imageView.setImageBitmap(images.get(position)); holder.imageView.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图){ 如果(位置==0) { FragmentTransaction FragmentTransaction=MFFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.linear_布局,new SentFragment()).commit(); }否则如果(位置==1) { FragmentTransaction FragmentTransaction=MFFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.linear_布局,new SentFragment()).commit(); } } }); } @凌驾 public int getItemCount(){ 返回图像。size(); } 公共类ViewHolder扩展了RecyclerView.ViewHolder{ 图像视图图像视图; 语境; 公共视图持有者(视图项视图){ this.context=itemView.getContext(); 超级(项目视图); //将您的单击列表添加到此处 itemView.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图){ if(getposition==0) { FragmentTransaction FragmentTransaction=((您的活动名称)上下文)。getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.linear_布局,new SentFragment()).commit(); }else if(getposition==1) { FragmentTransaction FragmentTransaction=((您的活动名称)上下文)。getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.linear_布局,new SentFragment()).commit(); } } }); } }
}

@AnilPervaiz我很高兴现在将其标记为已接受