如何从RecyclerView Adapter打开片段?

时间:2019-01-20 13:30:27

标签: android fragment adapter

我想将数据从适配器传递到我的片段, 所以基本上当我尝试单击一个项目时,片段应该打开, 但是,我的应用程序什么也没做。 那么我该如何解决呢?

适配器



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

    private static String TAG = "Cat Adapter";

    private ArrayList<String> catUrlImage = new ArrayList<>();
    private ArrayList<String> cuteCatName = new ArrayList<>();
    private Context catContext;
    private FragmentManager fragmentManager;
    private OnRecyclerViewItemClickListener listenerthis;


    public CatAdapter(ArrayList<String> catUrlImage, ArrayList<String> cuteCatName, Context catContext, OnRecyclerViewItemClickListener listener) {
        this.catUrlImage = catUrlImage;
        this.cuteCatName = cuteCatName;
        this.catContext = catContext;
        this.listenerthis = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false);
        ViewHolder catViewHolder = new ViewHolder(v);


        return catViewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        Log.d(TAG, "OnBinding: called");

        Glide.with(catContext)
                .asBitmap()
                .load(catUrlImage.get(position))
                .into(viewHolder.catImage);

        viewHolder.catName.setText(cuteCatName.get(position));

        viewHolder.catItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(catContext, cuteCatName.get(position), Toast.LENGTH_SHORT).show();
                listenerthis.onClick(position);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView catImage;
        TextView catName;
        RelativeLayout catItemLayout;


        public ViewHolder(View itemView) {
            super(itemView);

            catImage = itemView.findViewById(R.id.iLikeCat);
            catName = itemView.findViewById(R.id.catVeryCuteName);
            catItemLayout = itemView.findViewById(R.id.cat_container);

        }
    }

    public interface OnRecyclerViewItemClickListener{
        void onClick(int parameter);
    }

}

我已经在发布之前更改了我的活动,因此它进一步更改了onRecycleViewOnItemClickListener

MainActivity



public class MainActivity extends BaseActivity implements CatAdapter.OnRecyclerViewItemClickListener{
    private static final String TAG = "MainActivity";

    private ArrayList<String> catArray = new ArrayList<>();
    private ArrayList<String> catArrayImageUrl = new ArrayList<>();
    DataCat dataCat = new DataCat();

    private RecyclerView recyclerView;
    private RecyclerView.Adapter rAdapter;
    private RecyclerView.LayoutManager rLayoutManager;
    GalleryFragment galleryFragment = new GalleryFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Cat Has Create OnCreated");
        findView();
        initView();
        catData();
    }

    private void catData() {
        Log.d(TAG, "Preparing A Bitmap for a Cat");
        for (int i = 0; i < dataCat.catUrl.length; i++) {
           catArrayImageUrl.add(dataCat.catUrl[i]);
           catArray.add(dataCat.catName[i]);
        }

        initRecycleView();

    }

    private void initRecycleView() {
        CatAdapter catAdapter = new CatAdapter(catArrayImageUrl, catArray, this, this);
        recyclerView.setAdapter(catAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void findView() {
        Log.d(TAG, "initRecylce Vieq");
        recyclerView = findViewById(R.id.recyclerViewList);
        rLayoutManager = new LinearLayoutManager(this);

    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {
        recyclerView.setHasFixedSize(true);
    }

    @Override
    public void onClick(int parameter) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container_galery, galleryFragment)
                    .addToBackStack(null)
                    .commit();
    }
}

我输入了一些布局,也许布局上有一些不好的结构

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

<FrameLayout
    android:id="@+id/container_galery"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>    

我已经尝试了以下注释中的解决方案,但我的片段仍然没有显示出来。当我尝试单击时,我没有从logCat中得到任何错误。

3 个答案:

答案 0 :(得分:1)

您的界面/监听器尚未初始化,为空。

您必须在您的活动中实现click方法,在该活动中您初始化了适配器并为侦听器“ listenerThis”设置了一个值。

在适配器中为侦听器创建设置器:

  public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener){
        listenerthis=listener;
    }

,并在您的活动中实现侦听器:

 adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener(int parameter){

@override OnClick(int Parameter){
//do whatever you want with that parameter.

})

答案 1 :(得分:0)

在代码中做两个小改动-

适配器

public CatAdapter(ArrayList<String> catUrlImage, ArrayList<String> cuteCatName, Context catContext, OnRecyclerViewItemClickListener listenerthis) {
    this.catUrlImage = catUrlImage;
    this.cuteCatName = cuteCatName;
    this.catContext = catContext;
    this.listenerthis = listenerthis;
}

MainActivity

private void initRecycleView() {
    CatAdapter catAdapter = new CatAdapter(catArrayImageUrl, catArray, this, this);
    recyclerView.setAdapter(catAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

就这样。希望对您有所帮助。

答案 2 :(得分:0)

尝试从适配器调用片段的以下代码。在这段代码中,我有来自recylerview适配器的调用片段。

代码

myholder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    FragmentManager manager=((AppCompatActivity)context).getSupportFragmentManager();
    FragmentTransaction Ft=manager.beginTransaction();
    Ft.replace(R.id.container_fl,new ProfileFragment());

    Ft.commit();
}

});

请检查您的片段支持 android.support.v7.app.AppCompatActivity

尝试一下:

((AppCompatActivity)activity).getSupportFragmentManager()

和支持: support.v4.app.FragmentManager 试试这个:

FragmentManager manager = ((Activity)context).getFragmentManager();

((AppCompatActivity)activity).getSupportFragmentManager()

此代码对我有用。