处理RecyclerView OnClick方法上的项目

时间:2015-11-14 20:39:26

标签: android onclick android-recyclerview onitemclick

我正在尝试制作包含卡片视图的传记应用。当我点击RecyclerView上的每个项目时,我无法实现这一点,必须打开包含详细信息的第二个活动。这是我的代码。 我哪里做错了?非常感谢你......

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

    private static final String TAG = "debug";
    private Context mContext;
    List<Person> list = new ArrayList<>();

    public SimpleRecyclerAdapter(Context mContext, List<Person>list){
    this.list=list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView person_name;
        public TextView person_year;
        public ImageView person_img;
        public CardView card_view;

        public ViewHolder(final View view) {
        super(view);

            card_view = (CardView) view.findViewById(R.id.card_view);
            person_name = (TextView) view.findViewById(R.id.person_name);
            person_year = (TextView) view.findViewById(R.id.person_year);
            person_img = (ImageView) view.findViewById(R.id.person_photo);

            view.setOnClickListener(this);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(v.getContext(), DetailActivity.class);

                    v.getContext().startActivity(intent);
                }
            });

        }

        @Override
        public void onClick(View v) {

        }

    } // Closing inner class: Viewholder

    @Override
    public SimpleRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
        // create ViewHolder
        ViewHolder view_holder = new ViewHolder(v);
        return view_holder;
    }


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

        holder.person_name.setText(list.get(position).getName());
        holder.person_year.setText(list.get(position).getYear());
        holder.person_img.setImageResource(list.get(position).getPhoto_id());
    }

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


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
} // Closing outer class: SimpleRecyclerAdapter

3 个答案:

答案 0 :(得分:2)

您可能忘记将可点击属性添加到父行项目布局中。尝试添加它,如

android:clickable="true"

同时尝试删除视图中的一个onClickMethods,因为您将其设置了两次。

view.setOnClickListener(this);

view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(v.getContext(), DetailActivity.class);

            v.getContext().startActivity(intent);
        }
    });

答案 1 :(得分:0)

在RecycleView中 获取clickview项目的单击iem您可以进入设置recycle Adapter的活动。 像这样,

MutableContainers

我希望你能获得RecycleView的点击事件......

答案 2 :(得分:0)

从ViewHolder类中删除它:

view.setOnClickListener(this);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(v.getContext(), DetailActivity.class);

                v.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View v) {

    }

onBindViewHolder(ViewHolder holder, int position)中这样做:

        holder.card_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "inside viewholder position = " + position, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext,DetailActivity.class);
                    mContext.startActivity(intent);
                }
            });