Добавил событие OnClickListener. Использую Cardview в Recyclerview. Событие работает, только если нажму на грани элемента Recyclerview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="androidhive.info.materialdesign.activity.FriendsFragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleralda"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
carder_new.xml
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/carder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="7dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
android:layout_margin="1dp"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagePreview"
android:src="@mipmap/ic_launcher"
android:layout_gravity="right"
android:layout_marginRight="8dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:textSize="16sp"
android:text="Lorem ipsum"
android:id="@+id/titleText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_marginLeft="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/dateText"
android:layout_gravity="right"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
// Адаптер
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<News> news;
private Context mContext;
public RecyclerViewAdapter(List<News> news, Context mContext) {
this.news = news;
this.mContext = mContext;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.carder_new, parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
News news = this.news.get(position);
holder.text1.setText(news.getTitle());
holder.text3.setText(news.getDate());
Picasso.with(mContext).load(this.news.get(position).getPreviewImage()).resize(150, 120).into
(holder.imageView);
}
@Override
public int getItemCount() {
return this.news.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
public View container;
public ClipData.Item currentItem;
private TextView text1;
private TextView text3;
private ImageView imageView;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
text1 = (TextView)view.findViewById(R.id.titleText);
text3 = (TextView)view.findViewById(R.id.dateText);
imageView = (ImageView)view.findViewById(R.id.imagePreview);
}
@Override
public void onClick(View view) {
Log.e("Axixa", "onClick " + getPosition());
}
}
}
getAdapterPosition()илиgetLayoutPosition()– pavlofff May 16 '15 at 16:21