在RecyclerView OnClick方法上处理项目



我正在尝试制作一个包含卡片视图的传记应用程序。当我点击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

您可能忘记将可点击属性添加到父行项目布局中。试着像一样添加它

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);
        }
    });

在RecycleView中,要获得recycleview项目的点击iem,您可以在设置recycleAdapter的活动中获得。像这样,

private RecycleAdapter adapter;
Adapter.SetOnItemClickListener(new Recycle_FrameAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
           Toast.makeText(getApplicationContext(),""+position,Toast.LENGTH_SHORT).show();
        }
    });

我希望你能得到点击事件的RecycleView。。。

将其从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);
                }
            });

相关内容

  • 没有找到相关文章

最新更新