[Java] RecyclerView.ViewHolder中的onClickListener


Answers

您可以通过在适配器内为itemclicklistener创建一个接口来实现此itemclicklistener ,然后您可以从PlacesActivity设置PlacesActivity

您的PlacesAdapter内部某处需要以下内容:

private onRecyclerViewItemClickListener mItemClickListener;

 public void setOnItemClickListener(onRecyclerViewItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public interface onRecyclerViewItemClickListener {
        void onItemClickListener(View view, int position, String places_name);
    }

然后在你的ViewHolder (我已经在适配器内部添加了一个内部类)的内部,你可以将这个监听器应用到你希望用户点击的组件上,如下所示:

class PlacesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        PlacesViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClickListener(v, getAdapterPosition(), PlacesDetail.PLACES_NAME);
            }
        }
    }

这个例子展示了一个onClickListener被应用到PlacesViewHolder的视图。

recyclerView.setAdapter(adapter);// set adapter on recyclerview
            adapter.notifyDataSetChanged();// Notify the adapter
            adapter.setOnItemClickListener(new PlacesAdapter.onRecyclerViewItemClickListener() {
                @Override
                public void onItemClickListener(View view, int position, String places_name) { 
//perform click logic here (places_name is passed)
                    }
                });

要实现此代码,您需要将setOnItemClickListenersetOnItemClickListener中的适配器,如上所示。

Question

我有一个RecyclerView.ViewHolder和RecyclerView.Adapter,我需要点击后的项目,然后发送有关此项目的信息到另一个Activity

PlacesAdapter.java

public class PlacesAdapter extends RecyclerView.Adapter<PlacesViewHolder> {
private PlacesActivity placesActivity;

    Context context;
    private int position;
    List<Places> places;

    public PlacesAdapter(List<Places> places) {
        this.places = places;}

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PlacesViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.places_view, viewGroup, false);
        PlacesViewHolder pvh = new PlacesViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PlacesViewHolder personViewHolder, int i) {
        personViewHolder.name.setText(places.get(i).name);
        personViewHolder.address.setText(places.get(i).address);
        Picasso.with(personViewHolder.itemView.getContext())
                .load(places.get(i).photo)
                .into(personViewHolder.getPhoto());
       }

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

}

PlacesViewHolder.java

在这一行"intent.putExtra(PlacesDetail.PLACES_NAME,);" 我怎样才能发送名字?

 public  class PlacesViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    public TextView name;
    public TextView address;
    public ImageView photo;
    public PlacesViewHolder(final View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        name = (TextView)itemView.findViewById(R.id.person_name);
        address = (TextView)itemView.findViewById(R.id.person_age);
        photo = (ImageView)itemView.findViewById(R.id.person_photo);

        itemView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, PlacesDetail.class);
                intent.putExtra(PlacesDetail.PLACES_NAME,);
                context.startActivity(intent);
            }
        });

    }

    public TextView getAddress() {
        return address;
    }

    public TextView getName() {
        return name;
    }
    public ImageView getPhoto() {
        return photo;
    }
}