Reputation: 5904
I'm trying to use the RecyclerView and put eight images in the adapter dynamically. I have also the titles of each image so i create 2 arrays and with a for loop with multiple condition i can set what i want. It works but not as i want. Infact i get in the log:
Skipped 46 frames! The application may be doing too much work on its main thread.
And the scroll of the list has a very strong lag. How can i solve? This is the code
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
List<GridItem> mItems;
private static WallpaperManager wallpaper;
String[] title = {
"Material1",
"Material2",
"Wallpaper1",
"Wallpaper2",
"Wallpaper3",
"Wallpaper4",
"Wallpaper5",
"Wallpaper6",
"Wallpaper7"
};
private static int[] sDrawables = {
R.drawable.material,
R.drawable.materialuno,
R.drawable.materialdue,
R.drawable.materialtre,
R.drawable.materialquattro,
R.drawable.materialcinque,
R.drawable.materialsei,
R.drawable.materialsette,
R.drawable.materialotto
};
public ImageAdapter() {
super();
mItems = new ArrayList<GridItem>();
for (int i=0, j=0; i < title.length && j < sDrawables.length; i++, j++) {
GridItem wallTitle = new GridItem();
wallTitle.setName(title[i]);
wallTitle.setThumbnail(sDrawables[j]);
mItems.add(wallTitle);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_grid_layout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
final GridItem nature = mItems.get(i);
viewHolder.tvspecies.setText(nature.getName());
viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
viewHolder.imgThumbnail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("CLick",nature.toString());
}
});
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imgThumbnail;
public TextView tvspecies;
public ViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
}
}
}
Edited with entire adapter
Upvotes: 1
Views: 3787
Reputation: 12073
I ran into the same issue/error-message with very long delays using RecyclerView. I read on SO different answers about thread optimization, adapter optimization, etc.
Finally in tracing down where the issue started, I found out this Xml in TextView
was the culprit
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:maxLength="300"
android:maxLines="5"
Removing all that and just using standard values for width and height, the issue for me was resolved
Upvotes: 0
Reputation: 1006839
the real walls will be from web so that array of drawable will be an array of links or a json
You absolutely do not want to be loading full-size wallpaper images to use for thumbnails in a grid. That will be very slow, and you will run out of heap space.
For your short-term tests, I'd just prepare some thumbnails using a graphics editor, then use those.
For your "for realz" app, where your wallpapers are downloaded, you will want to use a third-party library, like Picasso or Universal Image Loader, that can load the images in the background, downsample them to thumbnail size, and so on. This sample app demonstrates ListView
with Picasso (reading in Stack Overflow questions). This sample app demonstrates RecyclerView
with Universal Image Loader (reading in video thumbnails from the videos on the device).
Upvotes: 2