Changing ArrayList shown in RecyclerView Android Java - java

I have multiple array lists inside an arraylist :
[[Object1, Object2],[Object3, Object4],[Object5, Object6]]
I display the first array list in a recyclerview that displays one arraylist at a time:
myViewHolder.bindTo(cities.get(0).get(i));
I want to click a button in another class that would show change the array list shown. How would I achieve this?
Recycler View Adapter Class:
private List<List<Country>> cities;
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
PlannerItemBinding plannerItemBinding =
DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.planner_item, viewGroup, false);
return new MyViewHolder(plannerItemBinding);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.bindTo(cities.get(0).get(i));
}
#Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public void setCityList(List<List<Country>> cities) {
this.cities = cities;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private PlannerItemBinding plannerItemBinding;
public MyViewHolder(#NonNull PlannerItemBinding plannerItemBinding) {
super(plannerItemBinding.getRoot());
this.plannerItemBinding = plannerItemBinding;
}
void bindTo(Country country) {
plannerItemBinding.setVariable(com.example.planner.BR.city, country);
plannerItemBinding.setVariable(com.example.planner.BR.adapterPosition, getLayoutPosition());
plannerItemBinding.setVariable(com.example.planner.BR.countryImageMedium, country.getImages().get(0).getSizes().getMedium());
plannerItemBinding.executePendingBindings();
}
}
}

Set a setter in your RecyclerView.Adapter:
public updateCities(List<List<Coutry>> cities) {
this.cities = cities;
notifydatasetchanged();
}
so that you can call it in onClick() of that button with the new data.
This will update the model of your adapter with the passed data and notify the recyclerview that its data has changed.

You create an onClickListener on the object of your choice, and then when it is clicked you just setCityList to the new data and use notifydatasetchanged on your adapter

Related

How to get the item position in a ListView and GridView outside the adapter?

I'm trying to get a View position through his view hierarchy.
When I have a RecyclerView, for get the current view position I do:
if(view.getParent() instanceof RecyclerView){
RecyclerView rv = (RecyclerView)view.getParent();
if(rv.getLayoutManager()!=null) {
position = rv.getLayoutManager().getPosition(view);
}
}
But when a ListView or GridView appears, I'm not able to get a Layout Manager to get the position of the view. The idea is to make it in a generic way, without knowing the Custom Adapter the view has.
I have tried to get the position through the ListView and GridView adapter but I dont see any method that do that.
Someone can help me?
Thank you a lot!
Create interface for get position for outside adapter class
here is adapter code:
public class ReportMediaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<File> reportMediaList = new ArrayList<>();
private Context context;
private int selectedPos = -1;
private OnRemoveImage onRemoveImage;
public ReportMediaAdapter(Context context, ArrayList<File> reportMediaList, OnRemoveImage onRemoveImage) {
this.reportMediaList = reportMediaList;
this.onRemoveImage = onRemoveImage;
this.context = context;
}
**public interface OnRemoveImage {
void onRemove(int pos);
}**
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView iv_image, iv_remove;
public MyViewHolder(View itemView) {
super(itemView);
iv_image = itemView.findViewById(R.id.iv_image);
iv_remove = itemView.findViewById(R.id.iv_remove);
}
}
#Override
public int getItemCount() {
return reportMediaList.size()-1;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_media, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
try {
MyViewHolder menuItemHolder = (MyViewHolder) holder;
CommonMethods.loadImage(context, reportMediaList.get(position), menuItemHolder.iv_image);
menuItemHolder.iv_remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
**onRemoveImage.onRemove(position);**
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is activity code:
#Override
public void onRemove(int pos) {
// use adapter position
}
I have found a solution:
if(view.getParent() instanceof ViewGroup) {
position = ((ViewGroup)view.getParent()).indexOfChild(view);
}

Two recyclerView and one onClick showing the content of the clicked element of the respective recyclerView

Problem
In my acitvity i have two recyclerViews showing popular movies and top rated movies respectively.
When I click on a movie in a recyclerView, that movie is sent to an activity showing its details.
I go back to the home and if I click on a movie of another recyclerView, the previously clicked movie is shown.
I need to show the details of any clicked movie in any recyclerView.
I have a single viewHolder (MovieViewHolder)
public class MovieViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//Widgets
TextView title, rating;
ImageView imageView;
//Click listener
OnMovieListener onMovieListener ;
public MovieViewHolder(#NonNull View itemView, OnMovieListener onMovieListener) {
super(itemView);
this.onMovieListener = onMovieListener;
title = itemView.findViewById(R.id.movie_title);
imageView = itemView.findViewById(R.id.movie_img);
rating = itemView.findViewById(R.id.rating_bar);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onMovieListener.onMovieClick(getAdapterPosition());
}
An interface
public interface OnMovieListener {
void onMovieClick(int position);
void onCategoryClick(String category);
}
This two Adapter:
This for popular movies
public class MovieRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MovieModel> mMovies;
private final OnMovieListener onMovieListener;
public MovieRecyclerView(OnMovieListener onMovieListener) {
this.onMovieListener = onMovieListener;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item,
parent, false);
return new MovieViewHolder(view, onMovieListener);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
((MovieViewHolder) holder).title.setText(mMovies.get(position).getTitle());
((MovieViewHolder) holder).rating.setText(String.valueOf((float) (mMovies.get(position).getVote_average())));
//ImageView: Using Glide Library
Glide.with(holder.itemView.getContext())
.load("https://image.tmdb.org/t/p/w500/"
+ mMovies.get(position).getPoster_path())
.into(((MovieViewHolder) holder).imageView);
}
#Override
public int getItemCount() {
if (mMovies != null) {
return mMovies.size();
}
return 0;
}
public void setmMovies(List<MovieModel> mMovies) {
this.mMovies = mMovies;
notifyDataSetChanged();
}
//Getting the id of the movie clicked
public MovieModel getSelectedMovie(int position) {
if (mMovies != null) {
if (mMovies.size() > 0) {
return mMovies.get(position);
}
}
return null;
}
This for top_rated
public class MovieLatestRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MovieModel> mMoviesLatest;
private OnMovieListener onMovieListener;
public MovieLatestRecyclerView(OnMovieListener onMovieListener) {
this.onMovieListener = onMovieListener;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item,
parent, false);
return new MovieViewHolder(view, onMovieListener);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
((MovieViewHolder)holder).title.setText(mMoviesLatest.get(position).getTitle());
((MovieViewHolder)holder).rating.setText(String.valueOf((float) (mMoviesLatest.get(position).getVote_average())));
//ImageView: Using Glide Library
Glide.with(holder.itemView.getContext())
.load("https://image.tmdb.org/t/p/w500/"
+ mMoviesLatest.get(position).getPoster_path())
.into(((MovieViewHolder)holder).imageView);
}
#Override
public int getItemCount() {
if(mMoviesLatest != null) {
return mMoviesLatest.size();
}
return 0;
}
public void setmMoviesLatest(List<MovieModel> mMoviesLatest) {
this.mMoviesLatest = mMoviesLatest;
notifyDataSetChanged();
}
//Getting the id of the movie clicked
public MovieModel getSelectedMovie(int position){
if(mMoviesLatest != null){
if(mMoviesLatest.size() > 0){
return mMoviesLatest.get(position);
}
}
return null;
}
In MainActivity i configure the recyclerViews with this method
private void ConfigureRecyclerView() {
movieRecyclerViewAdapter = new MovieRecyclerView(this);
movieLatestRecyclerViewAdapter = new MovieLatestRecyclerView(this);
recyclerView_popular.setAdapter(movieRecyclerViewAdapter);
recyclerView_popular.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recyclerView_latest.setAdapter(movieLatestRecyclerViewAdapter);
recyclerView_latest.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
//RecyclerView Pagination
//Loading next page of api response
recyclerView_popular.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
if (!recyclerView.canScrollVertically(1)) {
//Here we need to display the next search result
movieListViewModel.searchNextPage();
}
}
});
recyclerView_latest.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
if (!recyclerView.canScrollVertically(1)) {
//Here we need to display the next search result
movieListViewModel.searchNextPage();
}
}
});
}
And i use this for passing the selected movie to another activity
#Override
public void onMovieClick(int position) {
//WE don't need position of the movie in recyclerView
// We need the ID of the movie in order to get all it's details
Intent intent = new Intent(this, MovieDetails.class);
intent.putExtra("movie", movieRecyclerViewAdapter.getSelectedMovie(position));
startActivity(intent);
}
Whoever you are reading my question I thank you for your time and I apologize for the certain banality and length.
At this line:
intent.putExtra("movie", movieRecyclerViewAdapter.getSelectedMovie(position));
You are getting a movie from movieRecyclerViewAdapter. This adapter is only used with the 'Popular' movie recycler view.
onMovieClick is used for both the 'Popular' and the 'Top Rated' recycler views. So, clicking an item from 'Top Rated' will attempt to use an item from 'Popular'.
To fix this, a click from 'Top Rated' should not attempt to get an item from movieRecyclerViewAdapter.
You could do something like change onMovieClick(int position) to onMovieClick(MovieModel movie), so you don't have to worry about knowing which adapter to get the movie from.

Android RecyclerView prevent recycling all items

I have a RecyclerView in which the user can drag and drop an item to a different position in the RecyclerView.
So far everything works fine.
My problem begins when the RecyclerView has more items than it can display. So when it recycle his content. When the user drag and drop an item to a different position and than scroll away from this part the position changes are not saved. The user just see the old positions for the items.
You can see this issue in the .gif below.
I already tried several things out like:
recyclerViewItem.getRecycledViewPool().setMaxRecycledViews(0,0);
recyclerViewItem.setItemViewCacheSize(30);
and
holder.setIsRecyclable(false);
inside of my onBindViewHolder.
But nothing seems to work for me.
Does anyone know a solution for this problem?
My Fragment class:
public class ShoppinglistFragment extends Fragment {
private ViewModel viewModel;
private ShoppinglistAdapter adapterShoppingItem = new ShoppinglistAdapter();
private RecyclerView recyclerViewItem;
private Boolean fragmentStarted = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shoppinglist_layout, container, false);
recyclerViewItem = view.findViewById(R.id.recyclerViewShoppinglist);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewModel = ViewModelProviders.of(getActivity()).get(ViewModel.class);
fragmentStarted = true;
recyclerViewItem.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerViewItem.setHasFixedSize(false);
recyclerViewItem.getRecycledViewPool().setMaxRecycledViews(0,0);
recyclerViewItem.setItemViewCacheSize(30);
recyclerViewItem.setAdapter(adapterShoppingItem);
ItemTouchHelper.Callback callback =
new ItemMoveCallback(adapterShoppingItem);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerViewItem);
//fetch all Shoppinglist Items from local storage
viewModel.getAllShoppinglistItems().observe((LifecycleOwner) this, new Observer <List<Shoppinglist>>() {
#Override
public void onChanged(#Nullable List<Shoppinglist> items) {
if (fragmentStarted){
adapterShoppingItem.submitList(items);
fragmentStarted = false;
}
}
});
adapterShoppingItem.setOnPositionChanged(new ShoppinglistAdapter.onPositionChanged() {
#Override
public void onPositionChanged(Shoppinglist fromItem, Shoppinglist toItem, int fromPosition, int toPosition) {
int fromPositionServer = fromItem.getPosition();
int toPositionServer = toItem.getPosition();
fromItem.setPosition(toPositionServer);
toItem.setPosition(fromPositionServer);
//updating Shoppinglist Item locally
viewModel.updateItem(fromItem);
viewModel.updateItem(toItem);
}
});
}
}}
and my Adapter class:
public class ShoppinglistAdapter extends ListAdapter<Shoppinglist, ShoppinglistAdapter.NoteHolder> implements ItemMoveCallback.ItemTouchHelperContract {
public Integer ressourceType;
private onPositionChanged onPositionChanged;
private ArrayList<Shoppinglist> globalItemList = new ArrayList<Shoppinglist>();
public ShoppinglistAdapter() {
super(DIFF_CALLBACK);
Integer valueOf = Integer.valueOf(0);
this.ressourceType = valueOf;
}
private static final DiffUtil.ItemCallback<Shoppinglist> DIFF_CALLBACK = new DiffUtil.ItemCallback<Shoppinglist>() {
#Override
public boolean areItemsTheSame(Shoppinglist oldItem, Shoppinglist newItem) {
return oldItem.getSqlid() == newItem.getSqlid();
}
#Override
public boolean areContentsTheSame(Shoppinglist oldItem, Shoppinglist newItem) {
return oldItem.getTitle().equals(newItem.getTitle());
}
};
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.shoppinglist_item, parent, false);
return new NoteHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull NoteHolder holder, int position) {
Shoppinglist currentItem = getItem(position);
holder.setIsRecyclable(false);
holder.tv.setText(currentItem.getTitle());
...
}
public Shoppinglist getNoteAt(int position) {
return getItem(position);
}
public class NoteHolder extends RecyclerView.ViewHolder {
private TextView tv;
public NoteHolder(View itemView) {
super(itemView);
tv= itemView.findViewById(R.id.tv);
globalItemList .add(currentNote);
}
}
public interface onPositionChanged {
void onPositionChanged(Shoppinglist fromItem, Shoppinglist toItem, int fromPosition, int toPosition);
}
public void setOnPositionChanged(ShoppinglistAdapter.onPositionChanged listener) {
this.onPositionChanged = listener;
}
#Override
public void onRowMoved(int fromPosition, int toPosition) {
//send switched Items to Fragment
onPositionChanged.onPositionChanged(globalItemList.get(fromPosition), globalItemList.get(toPosition), fromPosition, toPosition);
//switch Items inside of the global Item List
Collections.swap(globalItemList, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
}
#Override
public void onRowSelected(NoteHolder myViewHolder) {}
#Override
public void onRowClear(NoteHolder myViewHolder) {}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}
The problem is that you should swap items in ListAdapter.mDiffer which holds the current showing list. So, globalItemList in your code is unused. Replace onRowMoved of your adapter with the following one:
#Override
public void onRowMoved(int fromPosition, int toPosition) {
ArrayList<Shoppinglist> list = new ArrayList(getCurrentList());
Collections.swap(list, fromPosition, toPosition);
submitList(list);
}

How to get items from recycler view adapter

I have a following class(I pass another recycler view adapter to a constructor):
class BaseAdapter<T>(private var items: ArrayList<T?>, private val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>): RecyclerView.Adapter<RecyclerView.ViewHolder>()
Is it possible to not pass items directly and get them from adapter?
I don't want to pass items again and want to get them from an adapter.
private var adapter: RecyclerViewAdapter? = null
adapter = RecyclerViewAdapter(items!!, applicationContext) // another adapter, can be any
recyclerView.layoutManager = layoutManager
recyclerView.adapter = BaseAdapter(items, adapter!!)
As you see, I pass items two times, because I need to work with them in a new adapter, like
override fun showAd(firstVisibleItemPosition: Int) {
if(isInsertionNeeded){
items.add(firstVisibleItemPosition + 1, null)
setVisibleItemPos(firstVisibleItemPosition + 1)
notifyItemInserted(firstVisibleItemPosition+1)
isInsertionNeeded = false
}
}
You have to create BaseRecyclerViewAdapter like this:-
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter implements AppConstants {
public SmartArrayList<T> mArrayList;
public Context mContext;
private int margin;
public BaseRecyclerViewAdapter(Context mContext) {
this.mArrayList = new SmartArrayList<>();
this.mContext = mContext;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(getView(), parent, false);
return getViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, int position) {
if (mArrayList != null && mArrayList.size() > 0) {
final T obj = mArrayList.get(holder.getAdapterPosition());
setData(holder, obj, position);
}
}
#Override
public int getItemCount() {
return mArrayList.size();
}
public abstract RecyclerView.ViewHolder getViewHolder(View view);
public abstract int getView();
public abstract void setData(RecyclerView.ViewHolder holder, T data, int position);
public void addAll(ArrayList<T> mArrayList) {
if (null != mArrayList) {
this.mArrayList.addAll(mArrayList);
notifyItemRangeInserted(getItemCount(), mArrayList.size());
}
}
public void updateAll(ArrayList<T> mArrayList) {
this.mArrayList = new SmartArrayList<>();
this.mArrayList.addAll(mArrayList);
notifyDataSetChanged();
// notifyItemRangeInserted(getItemCount(), mArrayList.size());
}
public void clear() {
this.mArrayList.clear();
notifyDataSetChanged();
}
public void update(int position, T obj) {
this.mArrayList.set(position, obj);
notifyItemChanged(position);
}
public void remove(int position) {
this.mArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mArrayList.size());
}
public void add(T obj) {
int position = mArrayList.size();
this.mArrayList.add(obj);
notifyItemInserted(position);
}
public void add(int position, T obj) {
this.mArrayList.add(obj);
notifyItemInserted(position);
}
public SmartArrayList<T> getData() {
return mArrayList;
}
}
Using......
public class YourAdapterName extends BaseRecyclerViewAdapter{
Your Code......
}
And you easily get the data.
public T getItem(int position) {
return items.get(position);
}
In RecyclerView Adapter Get The List Item Using RecyclerView Adapter getItem() Method.
Send The Position And Get The Item From List.

Android RecyclerView, recycling not working properly

I have a RecyclerView that I am using. I have used RecyclerView before but never had this problem.
When I scroll up and down some of the items disappear, and some of the items that disappear appear again in the bottom.
Code:
ViewHolder:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txt;
public ViewHolder(View view) {
super(view);
txt = (TextView) view.findViewById(R.id.txt);
}
}
Adapter:
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
private final Activity activity;
private final ArrayList<HashMap<String, String>> mItems;
public MyAdapter (Activity activity, ArrayList<HashMap<String, String>> mItems) {
this.activity = activity;
this.mItems= mItems;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(activity).inflate(R.layout.items, viewGroup, false));
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
HashMap<String, String> item = mItems.get(position);
String info = item.get("info ");
if (info!= null) {
viewHolder.txt.setText(info);
} else {
viewHolder.txt.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return (null != mItems? mItems.size() : 0);
}
}
onBindViewHolder reuses Views so let's say the first time onBindViewHolder() is called, info is null. This will cause that row to have visibility of View.GONE.
When onBindViewHolder is called again to bind a new row, the view for that row is still View.GONE - nothing is reset between rows being bound.
Therefore your if statement should reset the state completely:
if (info!= null) {
viewHolder.txt.setText(info);
viewHolder.txt.setVisibility(View.VISIBLE);
} else {
viewHolder.txt.setVisibility(View.GONE);
}
This will ensure that each row's visibility is set correctly.

Categories