saving RecyclerView images state when scrolling - java

I'm using Picasso in onBindViewHolder but when scrolling up or down it reload same images urls from internet this make the list so bad and slow
so i want when it first load save the scroll state for images and do not load it again '
how i can do that?
onBindViewHolder Method :
#Override
public void onBindViewHolder(MyCustomAdapter.CustomViewHolder holder, int position) {
Dishes di = dish.get(position);
//Download image using picasso library
Picasso.with(mContext).load(di.getDishImageUrl())
.error(R.drawable.ic_error)
.placeholder(R.drawable.fav_btn)
.into(holder.imageView);
//Setting text view title
holder.textView.setText(Html.fromHtml(di.getDishName()));
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getPosition();
Dishes di = dish.get(position);
Toast.makeText(mContext, di.getDishName(), Toast.LENGTH_SHORT).show();
}
};
//Handle click event on both title and image click
holder.textView.setOnClickListener(clickListener);
holder.imageView.setOnClickListener(clickListener);
holder.textView.setTag(dish.get(position));
holder.imageView.setTag(dish.get(position));
}

Related

Cannot makeSceneTransitionAnimation in RecyclerView Adapter, Android

I'm trying to make a transition between my RecyclewView and a activity when onClick, but the Adapter doesn't let it. Any suggestion in how could I solve this problem?
My onBindViewHolder that contains the target onClick method which I want to implement the transition animation:
public void onBindViewHolder(#NonNull DataRVAdapter.ViewHolder holder, int position) {
// setting data to our views in Recycler view items.
DataModal modal = dataModalArrayList.get(position);
holder.bookName.setText(modal.getName());
// we are using Picasso to load images
// from URL inside our image view.
Picasso.get().load(modal.getCover()).placeholder(R.drawable.book_placeholder).into(holder.bookCover);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// setting on click listener
// for our items of recycler items.
//Toast.makeText(context, "Clicked item is " + modal.getName(), Toast.LENGTH_SHORT).show();
// open activity with our book information
Intent intent = new Intent(holder.itemView.getContext(), BookDetailsActivity.class);
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(holder.itemView.getContext(),holder.bookCover, ViewCompat.getTransitionName(holder.bookCover));
intent.putExtra("name", dataModalArrayList.get(position).getName());
intent.putExtra("cover", dataModalArrayList.get(position).getCover());
intent.putExtra("author", dataModalArrayList.get(position).getAuthor());
intent.putExtra("date", dataModalArrayList.get(position).getDate());
intent.putExtra("publisher", dataModalArrayList.get(position).getPublisher());
intent.putExtra("pages", dataModalArrayList.get(position).getPages());
intent.putExtra("genre", dataModalArrayList.get(position).getGenre());
intent.putExtra("isbn", dataModalArrayList.get(position).getIsbn());
intent.putExtra("synopsis", dataModalArrayList.get(position).getSynopsis());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
holder.itemView.getContext().startActivity(intent, optionsCompat.toBundle());
}
});
}
And here's my ViewHolder:
// creating variables for our
// views of recycler items.
private TextView bookName;
private ImageView bookCover;
public ViewHolder(#NonNull View itemView) {
super(itemView);
// initializing the views of recycler views.
bookName = itemView.findViewById(R.id.idTVtext);
bookCover = itemView.findViewById(R.id.idIVimage);
}
}
These codes are all inside my RecyclewView Adapter
So thank you in advance!

How to add dynamic views to android layout in recyclerview adapter

enter image description here
I have a list of parents that each has zero or many children
I use recyclerview to show the parents.
I want to show parents as header of each recyclerview item and below show all the children
what is the best way to show the children?
is it suitable to show children in a listview or add dynamic views for each child if so how to do that?
I have attach the image of layout, please check what I mean
thanks
enter image description here
I think, may this helpful bcz, in every position there are dynamicly generate item view respectably.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
IAFTbind.rcvFilters.setLayoutManager(linearLayoutManager);
IAFTbind.rcvFilters.setAdapter(new RecyclerView.Adapter() {
public int checkedPosition = 0;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
ImageView imageView = new ImageView(getContext());
imageView.setScaleX(.9f);
imageView.setScaleY(.9f);
imageView.setLayoutParams(new ViewGroup.LayoutParams(size, size));
return new RecyclerView.ViewHolder(imageView) {
#Override
public String toString() {
return super.toString();
}
};
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
int p = holder.getAdapterPosition();
((ImageView) holder.itemView).setBackground(new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(fBits.get(p), size, size, false)));
// ((ImageView) holder.itemView).setBackground(new BitmapDrawable(getResources(), bitmaps.get(position)));
int cp = this.checkedPosition;
if (cp == -1) {
((ImageView) holder.itemView).setImageResource(0);
} else if (cp == p) {
((ImageView) holder.itemView).setImageResource(R.drawable.draw_tool_selecter_strock);
} else {
((ImageView) holder.itemView).setImageResource(0);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ImageView) holder.itemView).setImageResource(R.drawable.draw_tool_selecter_strock);
checkedPosition = p;
notifyDataSetChanged();
changeBitmapEffect(p);
}
});
}
#Override
public int getItemCount() {
return fBits.size();
}
});
Problem statement
You want to add childs dynamically as per the screenshot you shared.
Solution
So here I can see 2 recycler views, one inside another. The first recycler view is for parents which have 2 items
Parents name
Another recycler view for children
What you need to do is, you need to pass the updated list of item to the parent's recyclerview adapter, which will send the updated list of item to childs recyclerview adapter and the childs recyclerview adapter will check using the diffUtil about any changes and it will reflect the changes in the recyclerview.

change data in recycler view with click of button

So I am trying to create a challenge game where you can have 2 options to pick from, I have 2 card views setup in a recycler view setup, how would I change the values in them to get a new challenge with the click of a button from the main activity if a user wanted a new challenge?
I am fetching the data from each battle at random using an SQLite database.
My onBindViewHolder
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
SQLiteDatabase sqLiteDatabase = faceMashDatabaseHelper.getReadableDatabase();
cursor = sqLiteDatabase.query("images", new String[]{"id","filename","name"},null,null,null,null,null);
MyViewHolder myViewHolder = (MyViewHolder) holder;
int position1 = (int)(Math.random()*(faceMashDatabaseHelper.getCount()-0+1)+0);
cursor.moveToPosition(position1);
String filename = cursor.getString(cursor.getColumnIndex("filename"));
String name = cursor.getString(cursor.getColumnIndex("name"));
CardView cardView = myViewHolder.getCardView();
ImageView imageView = (ImageView) cardView.findViewById(R.id.imageView);
TextView nameView = (TextView) cardView.findViewById(R.id.nameView);
nameView.setText(name);
File file = context.getFileStreamPath(filename);
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
}
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = nameView.getText().toString();
listener.onClick(position,name);
}
}); //This on click is for the card views themselves.
}
My onClick at the current moment, I'm using a toolbar option
case R.id.newbattle:
faceMashAdapter.notifyDataSetChanged();
First of all, you should never do heavy work in onBindViewHolder since it will be called every time your View is recreated after it was recycled (because it was unused).
You should query your Database outside of the Adapter instead and occupy a List with your queried data. Your List should have as many entries as your RecyclerView has entries. Then you can use the position parameter in onBindViewHolder to always get the correct data for the entry (list.get(position)).
This way you can just change the content of your List and notify the Adapter which items have changed via notifyItemChanged (or if all items should be refreshed, use notifyDataSetChanged). That way the content of the Adapter is refreshed.
So what you should do in your onClick is update the content of your List and notify the Adapter.

clickable icons in RecyclerView

I am having some trouble understanding the pattern here. I have a view and an adaptor mixed in too much. Where is my logic going wrong ?
Trying to get the icons similar to clickable markers. If checked set to true and if checked again back to false.
However even if it is set to true / or false the view doesn't refresh automatically.
public class MyPojo {
String title;
String desc;
int image;
boolean isChecked;
My Adaptor Class ~
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
TextView title = (TextView) holder.mView.findViewById(R.id.textView);
TextView desc = (TextView) holder.mView.findViewById(R.id.textView2);
final ImageView image = (ImageView) holder.mView.findViewById(R.id.imageView);
title.setText(pojos.get(position).getTitle());
desc.setText(pojos.get(position).getDesc());
if (!(pojos.get(position).isChecked())) {
image.setImageResource(pojos.get(position).getImage());
} else {
image.setImageResource(R.mipmap.ic_launcher);
}
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setImageResource(R.mipmap.ic_launcher);
//todo Where do I set my is clicked ?
if (pojos.get(position).isChecked) {
pojos.get(position).setChecked(false);
//image.setImageResource(R.mipmap.ic_launcher);
}else{
pojos.get(position).setChecked(true);
//image.setImageResource(pojos.get(position).getImage());
}
}
});
}
you need to set setOnclickListener() in ViewHolder class instead of onBindViewHolder()

How to use setVisibility on integer array in adapter list view by onclick on ImageView in android

I'm trying to invisible an image when it is clicked by storing images from drawable into integer array using adapter list view, but I'm unable to get it. This is the code I'm using:
When i click on an image it should get invisible.I am storing images in int array and applying setVisibilty invisible but its not working.i want an image to be displayed in centre of screen and the one which is clicked should get invisible.i am trying to store images in an integer array and setting it up in adapter list.i am calling this function
imageIDs[position].setVisible(false);
Integer[] imageIDs = {
R.drawable.c2,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Note that Gallery view is deprecated in Android 4.1---
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//Adapter list
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
//onclick event
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{//displaying image clicked i am trying to invisible this pic when click
Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",//dispplpaying msg
Toast.LENGTH_SHORT).show();
//imageIDs[position].setVisible(false);
// display the images selected
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
//setting image on screen from using xml
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
// sets a grey background; wraps around the images
TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//imageIDs[position].setVisible(false);
//i am trying it here but its not working
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
I'm assuming you're trying to use this code:
//imageIDs[position].setVisible(false);
If so then what you're doing is calling setVisible on an Integer, which does not have that method. What you need to do is get a reference to the ImageView in which the image is being displayed and then call setVisibility(View.INVISIBLE) or setVisibility(View.GONE) on it.
Also it seems like you're trying to set the image to invisible but then you go and put the same resource back into the ImageView so I'm not sure what you're trying to do there.

Categories