RecyclerView点击监听器没有得到?

时间:2015-12-02 09:13:48

标签: android android-recyclerview

我有600个项目的数组。我已将所有数组项插入RecyclerView并且它们滚动得很好。但是,当我尝试访问onClick监听器时,它没有被调用。我还尝试在View.OnClickListener课程中实施onClick和覆盖ViewHolder,但它无效。

当用户点击视图中的某个项目时,我想要向列表中的点击项目干燥数据

适配器代码

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

    public static class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        private String mItem;
        private TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
            mTextView = (TextView) view.findViewById(R.id.item_title);
        }

        public void setItem(String item) {
            mItem = item;
            mTextView.setText(item);
        }

        @Override
        public void onClick(View view) {
            Log.d("TAG", "onClick " + getPosition() + " " + mItem);
        }
    }

    private String[] mDataset;

    public MyAdapter(String[] dataset) {
        mDataset = dataset;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setItem(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

0 个答案:

没有答案