RecyclerView with Piccaso
up vote
0
down vote
favorite
Looking for some help to see what I'm doing wrong with this code. I'm stock and been at it for a while now. This example started from here.
Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)
Gradle
implementation 'com.squareup.picasso:picasso:2.71828'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNumbers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent" />
</android.support.constraint.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements
MyRecyclerViewAdapter.ItemClickListener
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
String data = "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48";
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
int numberOfColumns = 6;
recyclerView.setLayoutManager(new GridLayoutManager(this,
numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
@Override
public void onItemClick(View view, int position)
Log.i("TAG", "You clicked number " + adapter.getItem(position) + ",
which is at cell position " + position);
MyRecyclerViewAdapter
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>
private String mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, String data)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
// inflates the cell layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType)
View view = mInflater.inflate(R.layout.recyclerview_item, parent,
false);
return new ViewHolder(view);
// binds the data to the TextView in each cell
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
Picasso.get()
.load(R.drawable.androidvsapple)
.into(holder.myImageView);
// total number of cells
@Override
public int getItemCount()
return mData.length;
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
ImageView myImageView;
ViewHolder(View itemView)
super(itemView);
myImageView = itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view,
getAdapterPosition());
String getItem(int id)
return mData[id];
void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
// parent activity will implement this method to respond to click events
public interface ItemClickListener
void onItemClick(View view, int position);
}
android-recyclerview picasso
add a comment |
up vote
0
down vote
favorite
Looking for some help to see what I'm doing wrong with this code. I'm stock and been at it for a while now. This example started from here.
Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)
Gradle
implementation 'com.squareup.picasso:picasso:2.71828'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNumbers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent" />
</android.support.constraint.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements
MyRecyclerViewAdapter.ItemClickListener
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
String data = "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48";
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
int numberOfColumns = 6;
recyclerView.setLayoutManager(new GridLayoutManager(this,
numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
@Override
public void onItemClick(View view, int position)
Log.i("TAG", "You clicked number " + adapter.getItem(position) + ",
which is at cell position " + position);
MyRecyclerViewAdapter
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>
private String mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, String data)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
// inflates the cell layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType)
View view = mInflater.inflate(R.layout.recyclerview_item, parent,
false);
return new ViewHolder(view);
// binds the data to the TextView in each cell
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
Picasso.get()
.load(R.drawable.androidvsapple)
.into(holder.myImageView);
// total number of cells
@Override
public int getItemCount()
return mData.length;
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
ImageView myImageView;
ViewHolder(View itemView)
super(itemView);
myImageView = itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view,
getAdapterPosition());
String getItem(int id)
return mData[id];
void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
// parent activity will implement this method to respond to click events
public interface ItemClickListener
void onItemClick(View view, int position);
}
android-recyclerview picasso
You posted your activity twice instead of adapter code. Also clarify what problem you are experiencing.
– Pawel
Nov 11 at 21:31
I've updated the adapter.
– morefaster
Nov 11 at 22:23
You still haven't posted what issues You're experiencing.
– Pawel
Nov 11 at 23:09
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Looking for some help to see what I'm doing wrong with this code. I'm stock and been at it for a while now. This example started from here.
Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)
Gradle
implementation 'com.squareup.picasso:picasso:2.71828'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNumbers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent" />
</android.support.constraint.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements
MyRecyclerViewAdapter.ItemClickListener
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
String data = "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48";
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
int numberOfColumns = 6;
recyclerView.setLayoutManager(new GridLayoutManager(this,
numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
@Override
public void onItemClick(View view, int position)
Log.i("TAG", "You clicked number " + adapter.getItem(position) + ",
which is at cell position " + position);
MyRecyclerViewAdapter
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>
private String mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, String data)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
// inflates the cell layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType)
View view = mInflater.inflate(R.layout.recyclerview_item, parent,
false);
return new ViewHolder(view);
// binds the data to the TextView in each cell
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
Picasso.get()
.load(R.drawable.androidvsapple)
.into(holder.myImageView);
// total number of cells
@Override
public int getItemCount()
return mData.length;
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
ImageView myImageView;
ViewHolder(View itemView)
super(itemView);
myImageView = itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view,
getAdapterPosition());
String getItem(int id)
return mData[id];
void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
// parent activity will implement this method to respond to click events
public interface ItemClickListener
void onItemClick(View view, int position);
}
android-recyclerview picasso
Looking for some help to see what I'm doing wrong with this code. I'm stock and been at it for a while now. This example started from here.
Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)
Gradle
implementation 'com.squareup.picasso:picasso:2.71828'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNumbers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent" />
</android.support.constraint.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements
MyRecyclerViewAdapter.ItemClickListener
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
String data = "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48";
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
int numberOfColumns = 6;
recyclerView.setLayoutManager(new GridLayoutManager(this,
numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
@Override
public void onItemClick(View view, int position)
Log.i("TAG", "You clicked number " + adapter.getItem(position) + ",
which is at cell position " + position);
MyRecyclerViewAdapter
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>
private String mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, String data)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
// inflates the cell layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType)
View view = mInflater.inflate(R.layout.recyclerview_item, parent,
false);
return new ViewHolder(view);
// binds the data to the TextView in each cell
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
Picasso.get()
.load(R.drawable.androidvsapple)
.into(holder.myImageView);
// total number of cells
@Override
public int getItemCount()
return mData.length;
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
ImageView myImageView;
ViewHolder(View itemView)
super(itemView);
myImageView = itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view,
getAdapterPosition());
String getItem(int id)
return mData[id];
void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
// parent activity will implement this method to respond to click events
public interface ItemClickListener
void onItemClick(View view, int position);
}
android-recyclerview picasso
android-recyclerview picasso
edited Nov 11 at 22:22
asked Nov 11 at 20:50
morefaster
498
498
You posted your activity twice instead of adapter code. Also clarify what problem you are experiencing.
– Pawel
Nov 11 at 21:31
I've updated the adapter.
– morefaster
Nov 11 at 22:23
You still haven't posted what issues You're experiencing.
– Pawel
Nov 11 at 23:09
add a comment |
You posted your activity twice instead of adapter code. Also clarify what problem you are experiencing.
– Pawel
Nov 11 at 21:31
I've updated the adapter.
– morefaster
Nov 11 at 22:23
You still haven't posted what issues You're experiencing.
– Pawel
Nov 11 at 23:09
You posted your activity twice instead of adapter code. Also clarify what problem you are experiencing.
– Pawel
Nov 11 at 21:31
You posted your activity twice instead of adapter code. Also clarify what problem you are experiencing.
– Pawel
Nov 11 at 21:31
I've updated the adapter.
– morefaster
Nov 11 at 22:23
I've updated the adapter.
– morefaster
Nov 11 at 22:23
You still haven't posted what issues You're experiencing.
– Pawel
Nov 11 at 23:09
You still haven't posted what issues You're experiencing.
– Pawel
Nov 11 at 23:09
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253109%2frecyclerview-with-piccaso%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253109%2frecyclerview-with-piccaso%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You posted your activity twice instead of adapter code. Also clarify what problem you are experiencing.
– Pawel
Nov 11 at 21:31
I've updated the adapter.
– morefaster
Nov 11 at 22:23
You still haven't posted what issues You're experiencing.
– Pawel
Nov 11 at 23:09