I am making a simple recyclerview with Movie items. When I try adding new items they won't display in the recyclerview right away. For some reason I have to scroll down so the first item is gone, and then up again for the new item to be displayed (it's added at index 0).
The adapter:
public class MovieCardAdapter extends RecyclerView.Adapter<MovieCardAdapter.MovieViewHolder> {
List<Movie> movieList;
public MovieCardAdapter(final List<Movie> movieList) {
this.movieList = movieList;
}
public void addItem(int position, final Movie movie){
movieList.add(position,movie);
notifyItemInserted(position);
}
public void removeItem(final Movie movie){
final int indexOf = movieList.indexOf(movie);
movieList.remove(movie);
notifyItemRemoved(indexOf);
}
public void updateItems(final List<Movie> items){
movieList = items;
notifyDataSetChanged();
}
#Override
public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.movie_row, viewGroup, false);
return new MovieViewHolder(itemView);
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public void onBindViewHolder(final MovieViewHolder movieViewHolder, final int i) {
final Movie movie = movieList.get(i);
String posterUrl = "";
final int posterHeight = 240;
if (!isEmpty(movie.getImdb_id())) {
posterUrl = "http://img.omdbapi.com/?i=" + movie.getImdb_id() + "&apikey="+apiKey+"&h=" + posterHeight;
} else if (!isEmpty(movie.getPoster())) {
posterUrl = movie.getPoster();
}
if (!isEmpty(posterUrl)) {
Picasso.with(movieViewHolder.context)
.load(posterUrl)
.resize(220, 354)
.centerCrop()
.into(movieViewHolder.poster);
}
movieViewHolder.title.setText(movie.getTittel());
movieViewHolder.format.setText(movie.getFormat());
if (movie.getRuntime().isEmpty() || movie.getRuntime().equalsIgnoreCase("0")){
movieViewHolder.runtime.setText("");
}else{
movieViewHolder.runtime.setText(movie.getRuntime()+" mins");
}
if (!movie.getTagline().isEmpty()) {
movieViewHolder.tagline.setText(movie.getTagline());
} else if (!movie.getPlot().isEmpty()) {
movieViewHolder.tagline.setText(movie.getPlot());
}
}
#Override
public int getItemCount() {
return movieList.size();
}
public static class MovieViewHolder extends RecyclerView.ViewHolder {
protected Context context;
protected LinearLayout movieRowLinearLayout;
protected ImageView poster;
protected TextView title;
protected TextView runtime;
protected TextView tagline;
protected TextView format;
public MovieViewHolder(View v) {
super(v);
context = v.getContext();
movieRowLinearLayout = (LinearLayout) v.findViewById(R.id.movieRowLinearLayout);
poster = (ImageView) v.findViewById(R.id.poster);
title = (TextView) v.findViewById(R.id.title);
runtime = (TextView) v.findViewById(R.id.runtime);
tagline = (TextView) v.findViewById(R.id.tagline);
format = (TextView) v.findViewById(R.id.format);
}
}
}
Initializing the adapter:
adapter = new MovieCardAdapter(MovieList.movies);
recyclerView.setAdapter(adapter);
Adding a sample movie:
if (id == R.id.add_movie) {
final Movie movie = new Movie();
movie.setTittel("0TEST");
movie.setFormat("HD-DVD");
MovieList.sortMoviesByTitle();
adapter.addItem(MovieList.movies.indexOf(movie), movie);
return true;
} else if (id == R.id.action_logOut) {
logOut(this);
}
Sry bad english, hope someone understands and know what the problem might be.
I have tried adding from runOnUiThread([...]), but same result.
This is happening because position 0 is above the current visible screen.
Until transcript mode is implemented, when you are adding an item to position N, try calling scrollToPosition(N) if LinearLayoutManager#findFirstCompletelyVisibleItemPosition returns N.
Also, don't make the position parameter final in your onBindViewHolder method. Instead, use ViewHolder.getPosition() if you need it in a callback. When position of the item changes, you will not receive an onBind call unless the item itself is invalidated.
Update
Also your update logic is not correct because you are not clearing values in views.
if (!movie.getTagline().isEmpty()) {
movieViewHolder.tagline.setText(movie.getTagline());
} else if (!movie.getPlot().isEmpty()) {
movieViewHolder.tagline.setText(movie.getPlot());
} else {
movieViewHolder.tagline.setText(null);
}
Same for the poster URL.
layoutManager.scrollToPosition didn't solve the problem for me.
Not a definitive solution, but the following line did the trick in my case:
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, 0);
Hope it helps...
Adding this solved the problem to me
layoutManager.scrollToPosition
Related
I am new to Android Development i am trying to sort item position in Recyclerview i am useing Realm Database and my data-set for Adapter is RealmResults<ShoppingList>
here is my Adapter please take look my moveItemDown and updateItemPosition method i m trying to update item position in updateItemPosition Method but it's not working. i also attached my view below the adapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private RealmResults<ShoppingList> dataList;
private Realm mRealm;
private LayoutInflater mInflater;
public RecyclerAdapter(Context context, RealmResults<ShoppingList> data, Realm reamDatabaseInstence){
this.dataList = data;
this.mInflater = LayoutInflater.from(context);
mRealm = reamDatabaseInstence;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i("Recycler", "onCreateViewHolder");
View view = mInflater.inflate(R.layout.shopping_list_item, parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public int getItemCount() {
return dataList.size();
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.i("Recycler", "onBindViewHolder " + position);
ShoppingList currentItem = dataList.get(position);
holder.setData(currentItem, position);
holder.setListener();
}
public void moveItemDown(int position){
if( position == (dataList.size()-1) ) { return; }
int oi = position;
int ni = position+1;
updateItemPosition(oi, ni);
notifyDataSetChanged();
}
public void moveItemUp(int position){
// Need to implement after down sort work
}
private void updateItemPosition(final int pos, final int new_pos){
mRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
ShoppingList old_item = realm.where(ShoppingList.class)
.equalTo("position", pos)
.findFirst();
old_item.setPosition(new_pos);
Log.d("UpdateData: ",old_item.getTitle()+" "+old_item.getPosition());
ShoppingList new_item = realm.where(ShoppingList.class)
.equalTo("position", new_pos)
.findFirst();
new_item.setPosition(pos);
Log.d("UpdateData: ",new_item.getTitle()+" "+new_item.getPosition());
}
});
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title;
ImageButton up, down;
ImageView image;
int position;
ShoppingList current;
MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.shopping_card_view_title);
image = (ImageView) itemView.findViewById(R.id.shopping_card_view_image);
up = (ImageButton) itemView.findViewById(R.id.shopping_card_view_up_btn);
down = (ImageButton) itemView.findViewById(R.id.shopping_card_view_down_btn);
}
void setData(ShoppingList i, int position) {
title.setText( i.getTitle() );
image.setImageResource(i.getImageId());
this.position = position;
this.current = i;
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.shopping_card_view_up_btn:
moveItemUp(position);
break;
case R.id.shopping_card_view_down_btn:
moveItemDown(position);
break;
}
}
void setListener() {
up.setOnClickListener(MyViewHolder.this);
down.setOnClickListener(MyViewHolder.this);
}
}
}
It's late reply by it would help to you and this is the combination of both answer EpicPandaForce give a great example to work with RealmRecyclerViewAdapter and make your Recyclerview automated.
Second Option
this is to use UNIQUE ID FIELD in order to make changes in list item.
Here is the problem happening in you Code
Basically when you get an item by it's position for instance position=1; and you updated to position 2 right? notice now you have two item with same position = 2 and now when you try to get first item with position = 2 Realm return the same item that you updated from position 1 to 2 now what happening here you updated dated position back to position=1;
taman neupane give you good idea but he is not describe much.
Please TRY THIS
private void updateItemPosition(final int pos, final int new_pos){
final String current_item_id = dataList.get(pos).get_id();
final String new_item_id = dataList.get(new_pos).get_id();
mRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
// Try to find the result with unique id replace this .equalTo("Your Unique ID Field name", current_item_id)
ShoppingList old_item = realm.where(ShoppingList.class).equalTo("position", pos).findFirst();
old_item.setPosition(new_pos);
// Also here Try to find the result with unique id replace this .equalTo("Your Unique ID Field name", new_item_id)
ShoppingList new_item = realm.where(ShoppingList.class).equalTo("position", new_pos).findFirst();
new_item.setPosition(pos);
//new you can tell you adapter about the changes in dataset.
notifyItemMoved(pos, new_pos);
notifyItemRangeChanged(pos, dataList.getSize());
}
});
}
and then update your both function with that
public void moveItemDown(int position){
if( position == (dataList.size()-1) ) { return; }
int oi = position;
int ni = position+1;
updateItemPosition(oi, ni);
}
public void moveItemUp(int position){
if( position == 0 ) { return; }
int oi = position;
int ni = position-1;
updateItemPosition(ni, oi);
}
And That's it.
If anyone want to modify my explanation i will appreciate because i am not good in english.
Happy Coding.
To get automatic updates, you should use RealmRecyclerViewAdapter from https://github.com/realm/realm-android-adapters
public class RecyclerAdapter extends RealmRecyclerViewAdapter<ShoppingList, RecyclerAdapter.MyViewHolder> { // <-- !
private Realm mRealm;
private LayoutInflater mInflater;
public RecyclerAdapter(Context context, RealmResults<ShoppingList> data, Realm realm){
super(data, true);
this.mInflater = LayoutInflater.from(context);
mRealm = realm;
}
and replace dataList with getData()
I think your ShoppingList item has unique id for all. Just make id field primary and use realmInsertorUpdate feature . You don't have to check for any thing else. If the id is available it will update the database and if id is not available it will just insert.
Unable to add second child in Recyclerview I am passing two different arrays to RecyclerAdapter to display two child layout with different data and views.Is there any solution to add different child layout using same header layout.I added horizontal Recyclerview in vertical Recyclerview and I want to display details like I attached the image
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter();
int[] images = new int[]{
R.drawable.finance,
R.drawable.business,
R.drawable.financejob,
R.drawable.ecomchallenges
};
ArrayList<ChildView> childViews = new ArrayList<>();
childViews.add(new ChildView(images[0], "The \"Best\" Startup Pitch Deck - How To Raise Venture Capital", "$100"));
childViews.add(new ChildView(images[1], "An Entire MBA in 1 Course:Award Winning Business School Prof", "$100"));
childViews.add(new ChildView(images[2], "What Finance Job is for You? Explanation of 14 Finance Roles", "$100"));
childViews.add(new ChildView(images[3], "Learn To Build Beautiful HTML5 And CSS3 Websites In 1 Month", "$100"));
int[] courseImage = new int[] {
R.drawable.php,
R.drawable.development,
R.drawable.web,
R.drawable.java
};
ArrayList<CourseByType> courseByTypes = new ArrayList<>();
courseByTypes.add(new CourseByType("Technology", courseImage[0]));
courseByTypes.add(new CourseByType("Business", courseImage[1]));
courseByTypes.add(new CourseByType("Photography", courseImage[2]));
courseByTypes.add(new CourseByType("Development", courseImage[3]));
Log.d("","Above adapter");
recyclerAdapter.addItem(new GroupView("Business", childViews));
Log.d("","Below Child");
recyclerAdapter.addCourseByType(new CourseByHeader("Technology", courseByTypes));
Log.d("","Below Course");
recyclerView.setAdapter(recyclerAdapter);
}
This is the main fragment where I set the values to two different
arraylist ArrayList<ChildView> childViews = new ArrayList<>()
and
ArrayList<CourseByType> courseByTypes = new ArrayList<>()
Values of child views are passing properly but CourseByType values are not passing.This is the adapter class for this fragment class.
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
ArrayList<PassValues> containerArrayList;
ArrayList<GroupView> groupViews;
ArrayList<CourseByHeader>courseByHeaders;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.group_title, parent, false);
return new ViewHolder(view);
}
public RecyclerAdapter(){
containerArrayList = new ArrayList<>();
groupViews = new ArrayList<>();
courseByHeaders = new ArrayList<>();
}
public void addContainer(PassValues container){
containerArrayList.add(container);
}
public void addItem(GroupView groupView){
Log.d("","Inside Group method");
groupViews.add(groupView);
}
public void addCourseByType(CourseByHeader courseByHeader){
Log.d("","Inside Course method");
courseByHeaders.add(courseByHeader);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("", "Pass Values out of IF" + position);
ChildViewAdapter childViewAdapter = new ChildViewAdapter();
if(position == 0){
GroupView groupView = groupViews.get(position);
holder.title.setText(groupView.getTitle());
Log.d("", "Passing Values" + groupView.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addChild(groupView.getChildViewList());
holder.recyclerView.setAdapter(childViewAdapter);
}
if (position == 1) {
CourseByHeader courseByHeader = courseByHeaders.get(position);
holder.title.setText(courseByHeader.getTitle());
Log.d("", "Passing Values" + courseByHeader.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addCourse(courseByHeader.getCourseByTypes());
holder.recyclerView.setAdapter(childViewAdapter);
}
}
#Override
public int getItemCount() {
if(getItemViewType(0) == TYPE_HEADER)
return groupViews.size() ;
if (getItemViewType(1) == TYPE_ITEM)
return courseByHeaders.size();
else return -1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
RecyclerView recyclerView;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.course_title);
recyclerView = (RecyclerView)itemView.findViewById(R.id.group_recycler);
}
}
}
This RecyclerAdapter contains one RecyclerView in that first row has one image and 3 textviews and 2nd row has 1 ImageView and 1 TextView. At position first,one image and 3 textviews are shown but it's not going on 2nd view
This is the view I getting after run on emulator.
This are two child for RecyclerViews
ChildView.java
public class ChildView {
int image;
String course, price;
public ChildView(int image, String course, String price) {
this.image = image;
this.course = course;
this.price = price;
}
public int getImage() {
return image;
}
public String getCourse() {
return course;
}
public String getPrice() {
return price;
}
}
CourseByType.java
public class CourseByType {
String courseName;
int courseImage;
public CourseByType(String courseName, int courseImage) {
this.courseName = courseName;
this.courseImage = courseImage;
}
public String getCourseName() {
return courseName;
}
public int getCourseImage() {
return courseImage;
}
}
CourseByHeader.java
public class CourseByHeader {
String title;
ArrayList<CourseByType> courseByTypes;
public CourseByHeader(String title, ArrayList<CourseByType> courseByTypes) {
this.title = title;
this.courseByTypes = courseByTypes;
}
public String getTitle() {
return title;
}
public ArrayList<CourseByType> getCourseByTypes() {
return courseByTypes;
}
}
GroupView.java
public class GroupView {
String title;
ArrayList<ChildView> childViewList;
String courseBy;
ArrayList<CourseByType> courseByTypes;
public GroupView(String title, ArrayList<ChildView> childViewList) {
this.title = title;
this.childViewList = childViewList;
}
public String getTitle() {
return title;
}
public ArrayList<ChildView> getChildViewList() {
return childViewList;
}
}
Groupview and CouseByType class have title and child list for recycleradapter
ChildViewAdapter.java
public class ChildViewAdapter extends RecyclerView.Adapter {
ArrayList<ChildView> childViewList;
ArrayList<CourseByType> courseByTypes;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
public class GroupHolder extends ViewHolder {
public ImageView iamView;
public TextView course, price;
public GroupHolder(View itemView) {
super(itemView);
iamView = (ImageView) itemView.findViewById(R.id.course_image);
course = (TextView) itemView.findViewById(R.id.course_by);
price = (TextView) itemView.findViewById(R.id.price);
}
}
public void addCourse(ArrayList<CourseByType> courseByType){
courseByTypes = courseByType;
}
public void addChild(ArrayList<ChildView> childView){
childViewList = childView;
}
public class Course extends ViewHolder {
public ImageView courseTypeImage;
public TextView courseType;
public Course(View itemView) {
super(itemView);
courseTypeImage = (ImageView)itemView.findViewById(R.id.course_image);
courseType = (TextView)itemView.findViewById(R.id.course_name_course);
}
}
public ChildViewAdapter() {
childViewList = new ArrayList<>();
courseByTypes = new ArrayList<>();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
RecyclerView.ViewHolder vh = null;
View v;
if(viewType == TYPE_HEADER){
v = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
return new GroupHolder(v);
}if(viewType == TYPE_ITEM){
v = LayoutInflater.from(context).inflate(R.layout.type_of_courses, parent, false);
return new Course(v);
}
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof GroupHolder){
Log.d("","instance of Group Holder");
ChildView childView = childViewList.get(position);
((GroupHolder)holder).iamView.setImageResource(childView.getImage());
((GroupHolder)holder).course.setText(childView.getCourse());
((GroupHolder)holder).price.setText(childView.getPrice());
return;
}
if(holder instanceof Course){
Log.d("","instance of Course ");
CourseByType courseByType = courseByTypes.get(position);
((Course)holder).courseTypeImage.setImageResource(courseByType.getCourseImage());
((Course)holder).courseType.setText(courseByType.getCourseName());
return;
}
}
#Override
public int getItemCount() {
int size;
if(childViewList.size()>0){
return size = childViewList.size();
}else return size = courseByTypes.size();
}
#Override
public int getItemViewType(int position) {
if(childViewList.size() != 0 && childViewList.size()>0){
return TYPE_HEADER;
}else return TYPE_ITEM;
}
}
This childview adapter has two view types first is one image and 3 text and second view type contain one image and one text.When I pass values from fragment only first view type get displayed and second view type not gets value from fragment.
To show multiple different views in a recyclerview, you have to override getItemViewType() in the recyclerview adapter.
//getItemViewType enables dynamic viewholder creation
#Override
public int getItemViewType(int position) {
//you will need to add a integer with variable name viewTypeCode
//for view1 set viewTypeCode = 100 and for view2 set viewTypeCode = 200
viewTypeCode = itemList.get(position).getViewTypeCode();
return viewTypeCode;
}
This is how the onCreateViewHolder will be different for multiple viewtypes. You will have to modify yours like this
#Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 100: return new FeedViewHolder(layoutInflater.inflate(R.layout.v1, parent, false),100);
case 200: return new FeedViewHolder(layoutInflater.inflate(R.layout.v2, parent, false),200);
}
return null;
}
OnBindViewHolder will be similarly modified
#Override
public void onBindViewHolder(final FeedViewHolder holder, int position) {
viewTypeCode = itemList.get(position).getViewTypeCode();
switch ( viewTypeCode) {
case 100:
//your code for v1
case 200:
//your code for v2
}
}
Similarly the ViewHolder class is modified
class FeedViewHolder extends RecyclerView.ViewHolder{
//declare variables here
public FeedViewHolder(View v, int viewType) {
super(v);
switch (viewType) {
//instead of itemView.findViewById you will have to use v.findViewById
case 100:
//your code for v1
case 200:
//your code for v2
}
}
For further reference refer to this SO answer
Don't pass two separate list.Make a custom class like this-
class MyClass {
int viewTypeCode;
CustomClass1 c1;
CustomClass2 c2;
//add the setter getter
}
In your activity while preparing the data.
List<MyClass> itemList = new ArrayList<>();
//put whatever logic you need to make the order of the list
//if CustomClass1 object is put then setViewTypeCode(100), setCustomClass2 = null
//if CustomClass2 object is put then setViewTypeCode(200), setCustomClass1 = null
After data is built, then send this to the adapter.
I have a RecyclerView which shows list of items sorted in descending order that works fine when I initially launch the screen, but when I add some more items to the existing list and setting it to the adapter with notifyDataSetChanged() method the existing list gets sorted in ascending order and the new items get added at the bottom of the view.
below is adapter
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private List<Cinema> lists;
private int savedItems;
public RecycleAdapter(List<Cinema> cinemas, int savedItems) {
this.lists = cinemas;
this.savedItems = savedItems;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView cinemaTitle;
public TextView cinemaDescription;
public TextView cinemaDate;
public LinearLayout newLayout;
public ViewHolder(View view) {
super(view);
cinemaTitle = (TextView) view.findViewById(R.id.cinema_title);
cinemaDescription = (TextView) view.findViewById(R.id.description_text);
cinemaDate = (TextView) view.findViewById(R.id.cinema_date);
newLayout = (LinearLayout) view.findViewById(R.id.new_cinema_layout);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_cinema, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Cinema cinema = cinemas.get(position);
if (cinema.getId() > savedItems) {
holder.newLayout.setVisibility(View.VISIBLE);
} else {
holder.newLayout.setVisibility(View.INVISIBLE);
}
String title = cinema.getMessage().trim();
String description = cinema.getDescription().trim();
String date = cinema.getPublishedDate().trim();
holder.cinemaTitle.setText(title);
holder.cinemaDescription.setText(description);
holder.cinemaDate.setText(date);
}
public void setCineams(List<Cinema> cinemas) {
this.cinemas = cinemas;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return cinemas.size();
}
}
and the method which updates the list from Fragment is given below:
#Override
public void onCinemaUpdated() {
cinemas = firebaseHelper.getAllCinemas();
//Method which sorts the lists in descending order after adding new items into it
sortCinemas();
if (recycleAdapterAdapter != null) {
recycleAdapterAdapter.setCineams(cinemas);
}
}
I am not sure why am I getting this behaviour. Can anyone clarify on this?
Change your constructor and setCinemas method code to : Let me know if it helps..
public RecycleAdapter(List<Cinema> cinemas, int savedItems) {
this.lists = cinemas;
this.savedItems = savedItems;
}
public void setCineams(List<Cinema> cinemas) {
this.lists = cinemas;
notifyDataSetChanged();
}
I have created a RecyclerView with Cards as its primary viewtype. What I am trying to achieve is that when user clicks on a item the ViewType of that item is changed from cards to another Viewtype(for example a list textviews placed horizontally scrollable).
My code for RecyclerView Adapter is as follows :
In the below piece of code I have created a arrraylist of enum types to keep track of the clicked state of each row in recyclerview, then I got the size of my dataset and initialized my arraylist for each row with SHOW_PRIMARY_CONTENT
public class DisplayItemsAdapter extends RecyclerView.Adapter<DisplayItemsAdapter.ViewHolder> {
private static ArrayList<clickedState> itemClickedState;
private enum clickedState {
SHOW_PRIMARY_CONTENT,
SHOW_SECONDARY_CONTENT
}
private ArrayList<ItemData> mDataset = new ArrayList<>();
public DisplayItemsAdapter(ArrayList<ItemData> dataset) {
mDataset = dataset;
itemClickedState = new ArrayList<>();
for (int i = 0; i < mDataset.size(); i++) {
itemClickedState.add(i, clickedState.SHOW_PRIMARY_CONTENT);
}
}
Below is my ViewHolder Class which holds references to all my child views, It Implements View.OnClickListener. Why it implements View.OnClickListener is so that it can toggle between clicked states to change viewTypes accordingly.
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvItemName;
TextView tvItemNumber;
public ViewHolder(View itemView) {
super(itemView);
tvItemName = (TextView) itemView.findViewById(R.id.tvItemName);
tvItemNumber = (TextView) itemView.findViewById(R.id.tvItemNumber);
tvTicketClass = (TextView) itemView.findViewById(R.id.tvTicketClass);
}
#Override
public void onClick(View v) {
itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
}
}
This is where I am getting the value of viewType returned by getItemViewType and returning inflated layout
#Override
public DisplayItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.display_Items_row, viewGroup, false);
View v2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.display_Items_ticket_class, viewGroup, false);
if (viewType == 0) {
return new ViewHolder(v1);
} else {
return new ViewHolder(v2);
}
}
Then here I m binding the views with values according to the viewType returned by the viewholderusing viewholder.getItemViewType
#Override
public void onBindViewHolder(DisplayItemsAdapter.ViewHolder viewHolder, final int position) {
if (viewHolder.getItemViewType() == 0) {
viewHolder.tvItemName.setText(mDataset.get(position).strItemName);
viewHolder.tvItemNumber.setText(mDataset.get(position).strItemNumber);
} else if (viewHolder.getItemViewType() == 1) {
for (int i = 0; i < mDataset.get(position).strClass.length; i++) {
viewHolder.tvTicketClass.setText(mDataset.get(position).strClass[i]);
}
}
}
#Override
public int getItemCount() {
return mDataset.size();
}
And finally my getItemViewType logic
#Override
public int getItemViewType(int position) {
if (itemClickedState.get(position) == clickedState.SHOW_SECONDARY_CONTENT)
return 1;
return 0;
}
}
What I could not understand is that why is not anything show when I run the code ,,,Everything seems valid to me . Help Me !!!!
see my example:
// RecycleItemObject.java
public class RecycleItemObject {
public static int LAYOUT_ITEM_PORTRAIN = 0;
public static int LAYOUT_ITEM_LANDSCAPE = 1;
private int type;
....................
/**
* #return get layout type of item
*/
public int getType() {
return type;
}
/**
* set layout type of item
* #param type
*/
public void setType(int type) {
this.type = type;
}
}
// adapter
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == RecycleItemObject.LAYOUT_ITEM_PORTRAIN) {
view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main, parent, false);
} else {
view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main_land, parent, false);
}
}
// event click item
#onClick....
curentObject.setType(RecycleItemObject.LAYOUT_ITEM_LANDSCAPE);
// notify item only
notifyItemChanged(itemPosition);
with me it work fine
Hope you have figured out your problem. If not then,I have done it f or you.
The buggy line is
#Override
public void onClick(View v) {
itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
}
Here what you are doing is adding a new Item to
itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
This will insert a new item and size of mdataList and itemClickState differs and hence mis matching happens.
Use itemClickedState.set(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
instead. And second thing I does not understand what tvTicketClass is . If it is a single TextView, then you should concatenate the string first and them do tvTicketClass.settext(concatenatedstrClass) . Hope you get helped from this post.If have any further issue , please do comment.
override getItemViewType in adapter and return itemtype you want. for more check out this link https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView
I'm using RecyclerView for a long list and when i scroll too fast, something weird happens. There are two problems.
First one is the one in the image below. One of items (like red bordered one in the image) sticks on some random part of the screen and blocks other items. It disappears when the real version of that item becomes visible on the screen.
Another problem about scrolling this long RecyclerView too fast is sticking onClick effect. Some of items become visible like someone is pressing on them.
Are these problems about my adapter or are they common problems about RecyclerView? Here is my adapter:
public class SimpleListItemRecyclerAdapter extends RecyclerView.Adapter<SimpleListItemRecyclerAdapter.ListItemViewHolder> {
Context context;
ArrayList<RecyclerItemModel> list;
OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
private int lastPosition = -1;
private boolean isClickable;
public SimpleListItemRecyclerAdapter(ArrayList<RecyclerItemModel> list, _FragmentTemplate fragment) {
this.list = list;
this.context = fragment.getActivity();
try {
this.onRecyclerViewItemClickListener = (OnRecyclerViewItemClickListener) fragment;
isClickable = true;
} catch (ClassCastException e) {
isClickable = false;
}
setHasStableIds(true);
}
#Override
public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.item_sub_main_list_single_text,
parent,
false);
return new ListItemViewHolder(itemView, onRecyclerViewItemClickListener, isClickable, list.get(0).getHeight());
}
#Override
public void onBindViewHolder(ListItemViewHolder holder, int position) {
final RecyclerItemModel recyclerItem = list.get(position);
if (recyclerItem.isBackgroundColorSpecified())
holder.itemView.setBackgroundResource(recyclerItem.getBackgroundColorResource());
else {
final int[] backgroundColors = Preferences.backgroundSelectorsGrey;
holder.itemView.setBackgroundResource(backgroundColors[position % backgroundColors.length]);
}
holder.textMain.setText(recyclerItem.getTextMain());
if (recyclerItem.isImageRightAvailable()) {
if (recyclerItem.isProgressBarAvailable())
if (recyclerItem.shouldShowDoneImage())
setDrawableFromSVG(holder.imageRight, recyclerItem.getImageRightResourceDone());
else
setDrawableFromSVG(holder.imageRight, recyclerItem.getImageRightResource());
} else
holder.imageRight.setVisibility(View.GONE);
if (recyclerItem.isTextMainBottomAvailable())
holder.textMainBottom.setText(recyclerItem.getTextMainBottom());
else
holder.textMainBottom.setVisibility(View.GONE);
if (recyclerItem.isTextRightTopAvailable())
holder.textRightTop.setText(recyclerItem.getTextRightTop());
else
holder.textRightTop.setVisibility(View.GONE);
if (recyclerItem.isTextRightBottomAvailable())
holder.textRightBottom.setText(recyclerItem.getTextRightBottom());
else
holder.textRightBottom.setVisibility(View.GONE);
if (recyclerItem.isProgressBarAvailable())
holder.progressBar.setProgress(recyclerItem.getProgress());
else
holder.progressBar.setVisibility(View.GONE);
setAnimation(holder.itemView, position);
}
#Override
public long getItemId(int position) {
return list.get(position).hashCode();
}
#Override
public int getItemCount() {
return list.size();
}
private void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.appear);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
public Drawable setDrawableFromSVG(ImageView imageView, int resource) {
SVG svg = new SVGBuilder()
.readFromResource(context.getResources(), resource)
.build();
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
imageView.setImageDrawable(svg.getDrawable());
return svg.getDrawable();
}
public final static class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
TextView textMain, textMainBottom, textRightTop, textRightBottom;
ImageView imageRight;
ProgressBar progressBar;
View itemView;
public ListItemViewHolder(View itemView, OnRecyclerViewItemClickListener onRecyclerViewItemClickListener,
boolean isClickable, int height) {
super(itemView);
this.itemView = itemView;
this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
textMain = (TextView) itemView.findViewById(R.id.list_item_text_main);
imageRight = (ImageView) itemView.findViewById(R.id.list_item_image_right);
textRightTop = (TextView) itemView.findViewById(R.id.list_item_text_right_top);
textRightBottom = (TextView) itemView.findViewById(R.id.list_item_text_right_bottom);
textMainBottom = (TextView) itemView.findViewById(R.id.list_item_text_main_bottom);
progressBar = (ProgressBar) itemView.findViewById(R.id.list_item_progress_bar);
switch (height) {
case RecyclerItemModel.HEIGHT_FULL:
itemView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Preferences.squareLength));
break;
case RecyclerItemModel.HEIGHT_HALF:
itemView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Preferences.squareLength / 2));
}
if (isClickable)
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onRecyclerViewItemClickListener.onRecyclerViewItemClick(getPosition());
}
}
}
Here is what resolved my problem:
Animating somehow causes a problem on RecyclerView. So the solution for me was removing the following line:
setAnimation(holder.itemView, position);
I didn't try to add animation again but if you really need it, here is something that can be useful: How to animate RecyclerView items when they appear.