android recyclerview项目点击

时间:2015-10-20 06:39:52

标签: android android-recyclerview

我在我的android项目中创建了一个recyclerView。我有11个项目。每个项目由一个imageview和两个textviews组成。当我点击任何项目时,它应该去他们的corressponding活动。我如何实现这一点。下面是我的recyclerView适配器的代码。请帮助我。

RecyclerAdapter.java:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Sabudaniel61 on 2015-10-15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>  {
    HotelData[] hoteldata;View view;
    RecyclerAdapter(HotelData[] hoteldata){
        this.hoteldata=hoteldata;
    }
    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout,null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
        viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
        viewHolder.textView1.setText(hoteldata[i].getHotel());
        viewHolder.textView2.setText(hoteldata[i].getPlace());


    }

    @Override
    public int getItemCount() {
        return hoteldata.length;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView1,textView2;ImageView imageView;
        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            textView1=(TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2=(TextView) itemLayoutView.findViewById(R.id.textView2);
            imageView=(ImageView) itemLayoutView.findViewById(R.id.imageView1);


        }

    }
}

4 个答案:

答案 0 :(得分:3)

在您的RecyclerAdapter中创建一个onclicklistener。

 private final OnClickListener mOnClickListener = new MyOnClickListener();

@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int position) {
    View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout,null);
    view.setOnClickListener(mOnClickListener);
    return new MyViewHolder(view);
}

onClick方法:

@Override
public void onClick(final View view) {
    int itemPosition = mRecyclerView.getChildPosition(view);
    String item = mList.get(itemPosition);//if you want to pass the data 
   //Navigate to your desired activity.
}

这是最简单的方法。

答案 1 :(得分:1)

  public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView textView1,textView2;ImageView imageView;
        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            textView1=(TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2=(TextView) itemLayoutView.findViewById(R.id.textView2);
            imageView=(ImageView) itemLayoutView.findViewById(R.id.imageView1);

            itemLayoutView.setOnClickListener(this);

        }
        @Override
        public void onClick(View v) {


            Intent intent = new Intent(mContext, ContentDetails.class);
            itemView.getContext().startActivity(intent);
        }
    }

试试这个

答案 2 :(得分:1)

您可以像这样填充recyclerview:

首先,在你的布局中,为linearlayout设置一个id

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/linearView" //add this line
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/imageView1"
        android:layout_gravity="top" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView1"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_gravity="top"
        android:layout_marginLeft="10dp" />
</LinearLayout>

上面的布局将在onCreateViewHolder函数上从你的适配器调用。

然后,调用您在viewHolder类上创建的线性布局:

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView textView;ImageView imageView;LinearLayout linearLayout;
    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        textView1=(TextView) itemLayoutView.findViewById(R.id.textView1);
        imageView=(ImageView) itemLayoutView.findViewById(R.id.imageView1);
        linearLayout=(LinearLayout) itemLayoutView.findViewById(R.id.linearView);
    }

}

终于,将onclicklistener设置为您的recyclerview项目,如下所示:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    //i use arraylist of type Hotel instead of array
    final Hotel hotel = HotelData.get(position);

    holder.textView1.setText(hotel.getName());
    //if you want to add clicklistener to recyclerview item
    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             //you can call activity here
        }
    });
    //if you want to add click listener to textview inside recyclerview item
    holder.textView1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             Toast.makeText(MainActivity.class,"textview clicked",Toast.LENGTH_LONG).show();
        }
    });
}

答案 3 :(得分:0)

在适配器中放置一个接口并从activity:

中调用它
private ArrayList<Inbox> list;
private Activity context;
private onItemClickedListener onItemClickedListener;

public InboxListAdapter(Activity context, ArrayList<Inbox> list) {
    this.list = list;
    this.context = context;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView image;
    private TextView name;
    private TextView date;
    private ImageButton btn_play;
    private ImageButton btn_share;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        image = (ImageView) itemLayoutView.findViewById(R.id.image);
        name = (TextView) itemLayoutView.findViewById(R.id.name);
        date = (TextView) itemLayoutView.findViewById(R.id.date);
    }
}

@Override
public InboxListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_inbox_list, parent, false);

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

来自活动

InboxListAdapter adapter = new InboxListAdapter(getActivity(), list);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickedListener(new InboxListAdapter.onItemClickedListener() {
            @Override
            public void onItemClicked(Inbox inbox) {

             }
        });