RecyclerView ClickListener无法识别触摸

时间:2016-07-02 21:00:04

标签: java android android-recyclerview

我创建了一个RecyclerView,在Adapter中我希望创建一个ClickListener。我希望RecyclerView中的每个项目在单击后发送唯一的响应。换句话说,单击是为每个特定的RecyclerView项加载不同的片段。现在,RecyclerView没有响应,因为我已经记录它,甚至添加了一个没有任何运气的Toast:

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


    private ArrayList<LatestPoll> mDataSet;
    int lastPosition = -1;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // each data item is just a string in this case
        protected TextView pollQuestion;
        protected ImageView pollImage;
        protected RelativeLayout individualItem;


        public ViewHolder(View v) {
            super(v);
            pollQuestion = (TextView) v.findViewById(R.id.latest_item_question);
            pollImage = (ImageView) v.findViewById(R.id.pollThumbNailImage);
            individualItem = (RelativeLayout) v.findViewById(R.id.individual_item);
            individualItem.setOnClickListener(this);



        }

        @Override
        public void onClick(View v) {
            v.getId();
            int itemPosition = getLayoutPosition();
            String item = String.valueOf(mDataSet.get(itemPosition));
            Toast.makeText(getActivity().getApplicationContext(), item, Toast.LENGTH_LONG).show();
            Log.v("TESTING", "ITEM CLICKED!");
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(ArrayList<LatestPoll> myDataset) {
        mDataSet = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,
                                         int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.latest_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        return new ViewHolder(v);
    }

    // Replace the contents of a view (invoked by the layout manager)

    //The OutOfBoundsException is pointing here
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.v("ON_BIND", "ON_BINDVIEWHOLDER CALLED");
        LatestPoll latestPoll = mDataSet.get(position);
        holder.pollQuestion.setText(latestPoll.getQuestion());
        Picasso.with(getActivity())
                .load(latestPoll.getPollImage())
                .fit()
                .placeholder(R.drawable.loading_spinnter_white)
                .into(holder.pollImage);

    }
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataSet.size();
    }
}

private void onLoad() {
    if (mFragmentListener != null) {
        mFragmentListener.onFragmentLoaded();
    }
}

public interface FragmentListener {
    void onFragmentLoaded();
}


}

1 个答案:

答案 0 :(得分:-2)

您需要将onclick侦听器设置为onBindViewHolder中的父布局。

holder.parentID.setOnClickListener(mRecyclerViewListener);

在您的查看持有人

parentID = iteView.findViewById(R.id.parent);