单击侦听器可用于回收器视图但适用于其他视图

时间:2017-02-18 08:09:04

标签: android android-recyclerview

我有一个带有列表的循环器视图,并且在视图单元格上有一个进度条。当我点击其他视图时,单击事件发生。但是当按下回收者查看物品时没有任何反应。请告诉我原因。我正在粘贴oncreateview holder下面的代码。

class MockSeriesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView examMeta;
    RecyclerView subjectBubbles;
    BubbleAdapter adapter;
    NumberProgressBar numberProgressBar;


    public MockSeriesViewHolder(final View itemView) {
        super(itemView);
        examMeta = (TextView) itemView.findViewById(R.id.tv_exam_meta);
        numberProgressBar = (NumberProgressBar) itemView.findViewById(R.id.pb_exam_progress);

        subjectBubbles = (RecyclerView) itemView.findViewById(R.id.rv_subject_bubbles);
        subjectBubbles.setHasFixedSize(true);
        subjectBubbles.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new BubbleAdapter(itemView.getContext());
        subjectBubbles.setAdapter(adapter);

        itemView.setOnClickListener(this);

    }

    public void renderMockTestSeries(TestSeriesTest test) {
        adapter.setItems(test.getSubjects());
        examMeta.setText(test.getNoOfQuestions() + " QUESTIONS     " + (test.getTimeLimitInSeconds() / 60) + " MINUTES");
        numberProgressBar.setProgress(test.getPercentageOfCompletion());
    }

    @Override
    public void onClick(View view) {
        TestSeriesTest series = getItem(getLayoutPosition());
        mListener.onTestSeriesTestClicked(series);
    }
}

点击listner类

public interface TestSeriesTestClickListener {
public void onTestSeriesTestClicked(TestSeriesTest mockTest);}

3 个答案:

答案 0 :(得分:0)

创建一个名为RecyclerItemClickListener的类:

params[:stock][:ticket]

然后将以下代码添加到您的活动中:

import android.content.Context;  
import android.support.v7.widget.RecyclerView;  
import android.view.GestureDetector;  
import android.view.MotionEvent;  
import android.view.View;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {  
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}

这将是全部。您可以在OnItemClick中输入您的代码,您也可以在其中获得项目位置

答案 1 :(得分:0)

  

RecyclerItemClickListener

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {  
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}
  

ClickListener

recyclerView.addOnItemTouchListener(  
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
  @Override public void onItemClick(View view, int position) {
    // TODO Handle item click
  }
})
);

答案 2 :(得分:0)

我的处置是你的subjectBubbles必须包含可点击的项目,以防止点击事件传播到父视图。
检查您的BubbleAdapter,看看您是否有任何点击事件。