当我从片段中调用它时,我的接口类不起作用 [英] My interface class don't work when I call it from a fragment

查看:178
本文介绍了当我从片段中调用它时,我的接口类不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中实现此代码,但无法正常工作 RecyclerView:如何在ImageView上捕获onClick?

I'm trying to implement this code in my project but is not working RecyclerView: how to catch the onClick on an ImageView?:

我已经实现了一个界面来处理onclick imageview和onclick row in a recyclerview。问题是,当我尝试单击imageview或行时,不会发生任何事情。如果我在我的ViewHolder类中尝试相同的工作,因为我可以看到Toast。问题是我需要从我的片段中完成这项工作。

I've implemented a interface to handle onclick imageview and onclick row in a recyclerview. The problem is that when I try to do a click on the imageview or in the row don't happen nothing. If I try the same in my ViewHolder class, works, because I can see the Toast. The problem is that I need that this work from my fragment.

我认为可能问题是当我将监听器传递给我的ViewHolder的构造函数或者从我传递监听器时我的片段。

I think that maybe the problem is when I pass listener to my ViewHolder's constructor or when I pass the listener from my fragment.

一些帮助将是apreciate:

Some help will be apreciate:

我的适配器类:

 private static List<DatabaseModel> dbList;
 private static Context context;
 private RecyclerViewClickListener listener;


public class MainTimelineRecyclerAdapter extends RecyclerView.Adapter<MainTimelineRecyclerAdapter.AnViewHolder> {

    public MainTimelineRecyclerAdapter(Context context, List<DatabaseModel> dbList, RecyclerViewClickListener listener ){
        this.dbList = new ArrayList<DatabaseModel>();
        this.context = context;
        this.dbList = dbList;
        this.listener = listener;

  }

public static class AnViewHolder extends RecyclerView.ViewHolder {

        public TextView name;
        public ImageView contact;

        public AnViewHolder(final View itemLayoutView, final RecyclerViewClickListener listener) {
            super(itemLayoutView);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null)
                        listener.onRowClicked(getAdapterPosition());
                }
            });

            contact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null)
                        listener.onViewClicked(v, getAdapterPosition());
                }
            });

        }
    }

 public interface RecyclerViewClickListener {
        void onRowClicked(int position);
        void onViewClicked(View v, int position);
    }
}

我的片段:

public class MainTimeline extends Fragment implements RecyclerViewClickListener {
    private RecyclerViewClickListener listener;
.
.
.

        mAdapter = new MainTimelineRecyclerAdapter(getActivity(), dbList, listener);


   @Override
    public void onViewClicked(View view, int position) {
        Toast.makeText(getActivity(), "photo", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRowClicked(int position) {
        Toast.makeText(getActivity(), "view", Toast.LENGTH_SHORT).show();

        }
}


推荐答案

如果您希望片段侦听适配器中发生的操作,那么您需要将其注册为适配器中发生的事件的侦听器(通过实现接口并将片段作为侦听器传递):

If you want the fragment to listen for the actions happening in the adapter then you need to register it as the listener for the events happening in the adapter(by implementing the interface and passing the fragment as a listener):

public class MainTimeline extends Fragment implements RecyclerViewClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // you don't need a private RecyclerViewClickListener listener; reference as you have the fragment itself
        mAdapter = new MainTimelineRecyclerAdapter(getActivity(), dbList, this); 
    }
    // implement the methods from the interface
}

这篇关于当我从片段中调用它时,我的接口类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆