为什么setOnclicklistener在endlessrecyclerview中的特定行中不起作用?

时间:2019-06-13 08:13:59

标签: android android-recyclerview

我想为无休止的recyclerview行中的特定项目设置点击事件。我在适配器内部编写了on-click功能,并通过使用接口实现了该活动。但是我的问题是on-click功能无法正常工作。

我尝试使用行位置,但是它不起作用。

 public interface ViewOnMapAdapterListener {

    void viewOnMapOnClick(ModelTripHistoryDetails data);

}



 @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    try {
        if (position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) {
            isLoading = true;
            loadMoreListener.onLoadMore();
        }

        if (getItemViewType(position) == TYPE_LIST) {
            ((TripHolder) holder).bindData(tripDetailsList.get(position), position, viewOnMapAdapterListener);
        }
    } catch (ArrayIndexOutOfBoundsException ex) {

    } catch (ParseException e) {
        e.printStackTrace();
    }

    //No else part needed as load holder doesn't bind any data
}

 void bindData(final ModelTripHistoryDetails tripDetais, final int position, final AdapterTripHistory.ViewOnMapAdapterListener viewOnMapAdapterListener) throws ParseException {

        try {

            setDatas(tripDetais);

            tvViewOnMap.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                        viewOnMapAdapterListener.viewOnMapOnClick(tripDetais);

                }
            });

        } catch (Exception e) {

        }
    }
  

活动

 private void initView() {
    tripDetailsList = new ArrayList<>();
    rvTripHistory = (RecyclerView) findViewById(R.id.rvTripHistory);
    context = this;

    adapterTripHistory = new AdapterTripHistory(this, tripDetailsList, new AdapterTripHistory.ViewOnMapAdapterListener() {
        @Override
        public void viewOnMapOnClick(ModelTripHistoryDetails data) {
            Toast.makeText(TripHistoryActivity.this,"clicked",Toast.LENGTH_SHORT).show();
        }
    });
    adapterTripHistory.setLoadMoreListener(new AdapterTripHistory.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {

            rvTripHistory.post(new Runnable() {
                @Override
                public void run() {
                    index++;
                    loadMore(index);
                }
            });
            //Calling loadMore function in Runnable to fix the
            // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling error
        }
    });

2 个答案:

答案 0 :(得分:1)

我将建议您在每个try-catch块内添加日志。您可能在setDatas(tripDetais)方法中遇到了一些异常,并且由于该异常而从未将侦听器设置为视图。

答案 1 :(得分:0)

据我所知,您需要实现一个Holder类,该类扩展了ViewHolder并实现了View.OnClickListener(),如下所示:

    public static  class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView someView;
    OnYourListener onYourListener;

    public Holder(@NonNull View itemView,OnYourListener onYourListener) {
        super(itemView);
        this.onYourListener = onYourListener;
        someView = itemView.findViewById(R.id.some_view);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        onYourListener.onYourClicked(getAdapterPosition());
    }
}

并将实现接口的活动作为侦听器传递给适配器。
您的适配器构造函数应如下所示:

    public Adapter(Context context, ArrayList<somthing> arrayList, OnYourListener onYourListener) {
    this.context = context;
    this.ArrayList = ArrayList;
    this.mOnYourListener = onYourListener;

}

,在createViewHolder上,您应该像这样传递侦听器:

    @Override
public Holder onCreateViewHolder(@ViewGroup viewGroup, int i) {

  View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.your_layout,null);
  Holder holder =new Holder(layout,mOnYourListener);
  return holder;
}

通过这种方式,您可以按照需要的方式创建一个支架,并且onclick应该起作用