RecylcerView项目单击侦听器

时间:2016-01-02 22:25:21

标签: android android-recyclerview

我试图捕获RecyclerView中项目的点击事件,但它不起作用。我的研究(SO)告诉我把听众放在适配器中,所以我做了。这是代码:

    public class PoliticianPagerAdapter extends RecyclerView.Adapter<PoliticianPagerAdapter.ViewHolder> {
        private Politician[] mDataset;

        // 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
            public TextView name;
            public TextView term;
            public TextView position;
            public ImageView image;
            public CardView card ;
            public ViewHolder(View layout) {
                super(layout);
                name = (TextView) layout.findViewById(R.id.politicianNameAndParty);
                term = (TextView) layout.findViewById(R.id.politicianTerm);
                position = (TextView) layout.findViewById(R.id.politicianPosition);
                image = (ImageView) layout.findViewById(R.id.politicianImage);
                card = (CardView) layout.findViewById(R.id.politicianCard);
            }

            @Override
            public void onClick(View view)
            {

                Intent intent = new Intent(getActivity(),PoliticianPage.class);
                startActivity(intent);


            }
        }

        public PoliticianPagerAdapter(Politician[] myDataset) {
            mDataset = myDataset;
        }

        @Override
        public PoliticianPagerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fragment_home_politician_block, parent, false);

            return new ViewHolder(v);

        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.name.setText(mDataset[position].name + " (" + mDataset[position].party + ")");
            holder.term.setText(Integer.toString(mDataset[position].termBegin) + " - " + Integer.toString(mDataset[position].termEnd));
            holder.term.setText(mDataset[position].position);
            holder.image.setImageResource(mDataset[position].image);


        }

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

1 个答案:

答案 0 :(得分:1)

使用CardView中的视图拦截点击事件。

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="4dp"
        android:elevation="3dp"
        card_view:cardCornerRadius="4dp">

        <!-- Your card -->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/clickable_view_card"
            android:clickable="true"/>


    </android.support.v7.widget.CardView>
</LinearLayout>

适配器:

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

        public class ViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout clickable_layout;



            public ViewHolder(View itemView) {
                super(itemView);
                clickable_layout = (RelativeLayout) itemView.findViewById(R.id.clickable_view_card);


            }
        }

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

        @Override
        public void onBindViewHolder(final Adapter.ViewHolder holder, final int position) {

            holder.clickable_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("OnClick", "position clicked: "+ position);
                }
            });

        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }