Skip to content

Commit

Permalink
Add click sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Feb 13, 2017
1 parent 98b9c4d commit ba738bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.rubensousa.recyclerviewsnap;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -40,7 +41,6 @@ public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageResource(app.getDrawable());
holder.nameTextView.setText(app.getName());
holder.ratingTextView.setText(String.valueOf(app.getRating()));

}

@Override
Expand All @@ -53,19 +53,24 @@ public int getItemCount() {
return mApps.size();
}

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

public ImageView imageView;
public TextView nameTextView;
public TextView ratingTextView;

public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
nameTextView = (TextView) itemView.findViewById(R.id.nameTextView);
ratingTextView = (TextView) itemView.findViewById(R.id.ratingTextView);
}

@Override
public void onClick(View v) {
Log.d("App", mApps.get(getAdapterPosition()).getName());
}
}

}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical">

<LinearLayout
Expand All @@ -15,8 +17,8 @@
android:id="@+id/imageView"
android:layout_width="88dp"
android:layout_height="88dp"
android:scaleX="1.2"
android:scaleType="fitXY"
android:scaleX="1.2"
android:scaleY="1.2"
android:src="@drawable/ic_docs_48dp" />

Expand Down

0 comments on commit ba738bb

Please sign in to comment.