Expand/collapse a nested RecyclerView - android

I have a RecyclerView adapter with multiple ViewHolders. Each ViewHolder has a header TextView and a nested RecyclerView which was working fine. But I wanted to implement an expand/collapse function so that nested RecyclerView is hidden until the header is clicked. I used this method RecyclerView expand/collapse items. It works but when I click the header to expand the nested recylerview, the recyclerview doesn't populate any data. To be clear, it retrieves data but it's not visible. Any ideas why this might be?
This is my onBindViewMethod:
public class EligibilityAdapter extends RecyclerView.Adapter<EligibilityAdapter.ViewHolder> {
private Context mContext;
private List<EligibilityDetails> mEligsList;
private List<Items> mItemslist;
private LayoutInflater inflater;
private int mExpandedPosition = -1;
public EligibilityAdapter(Context context, List<EligibilityDetails> eligsList) {
mContext = context;
mEligsList = eligsList;
inflater = LayoutInflater.from(context);
}
#Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return R.layout.rv_eligs_item_domestic;
case 1:
return R.layout.rv_eligs_item_overseas;
default:
return R.layout.rv_eligs_item_military;
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(i, viewGroup, false);
ViewHolder holder = null;
switch (i) {
case R.layout.rv_eligs_item_domestic:
holder = new DomesticViewHolder(view);
break;
case R.layout.rv_eligs_item_overseas:
holder = new OverseasViewHolder(view);
break;
case R.layout.rv_eligs_item_military:
holder = new MilitaryViewHolder(view);
break;
}
return holder;
}
#Override
public int getItemCount() {
return mEligsList.size();
}
public abstract class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView itemsRv;
TextView mHeader;
ItemsAdapter adapter;
public ViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
abstract void bind(EligibilityDetails item);
}
public class DomesticViewHolder extends ViewHolder {
TextView mHeader;
RecyclerView itemsRv;
ItemsAdapter adapter;
public DomesticViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
#Override
void bind(EligibilityDetails eligibilityDetails) {
mHeader.setText(eligibilityDetails.getRequirementHeader());
mItemslist = eligibilityDetails.getItemsList();
ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
itemsRv.setHasFixedSize(true);
itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
itemsRv.setAdapter(itemsAdapter);
itemsRv.setNestedScrollingEnabled(false);
}
}
public class OverseasViewHolder extends ViewHolder {
TextView mHeader;
RecyclerView itemsRv;
ItemsAdapter adapter;
public OverseasViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
#Override
void bind(EligibilityDetails eligibilityDetails) {
mHeader.setText(eligibilityDetails.getRequirementHeader());
mItemslist = eligibilityDetails.getItemsList();
ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
itemsRv.setHasFixedSize(true);
itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
itemsRv.setAdapter(itemsAdapter);
itemsRv.setNestedScrollingEnabled(false);
}
}
public class MilitaryViewHolder extends ViewHolder {
TextView mHeader;
RecyclerView itemsRv;
ItemsAdapter adapter;
public MilitaryViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
#Override
void bind(EligibilityDetails eligibilityDetails) {
mHeader.setText(eligibilityDetails.getRequirementHeader());
mItemslist = eligibilityDetails.getItemsList();
final ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
itemsRv.setHasFixedSize(true);
itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
itemsRv.setAdapter(itemsAdapter);
itemsRv.setNestedScrollingEnabled(false);
}
}
#Override
public void onBindViewHolder(#NonNull final EligibilityAdapter.ViewHolder viewHolder, int i) {
viewHolder.bind(mEligsList.get(i));
final boolean isExpanded = i == mExpandedPosition;
viewHolder.itemsRv.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.itemView.setActivated(isExpanded);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:viewHolder.getAdapterPosition();
ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
viewHolder.itemsRv.setAdapter(itemsAdapter);
//TransitionManager.beginDelayedTransition(recyclerView);
notifyDataSetChanged();
}
});
}
where itemsRv is the nested RecyclerView.
I've tried moving this logic to the individual viewholders and moving the recyclerview logic here like setting the adapter inside of the onClick method. Each time it comes up blank.
Thanks in advance.

Instead of having a new sub Recyclerview for every header you can create a multi view-type adapter that will have a view-type for your header and a view-type for your child-item.
And instead of using a just the header data-item for your list of data, use a generic type that will allow casting each data-type to its own view-type.
To do that we need to create an empty interface that all of our data-types will implement that way they are all generic.
public interface GenericDataType {}
so then your data-type will look like this
class HeaderItem implements GenericDataType {
//All of your pojo data
List<ChildrenItem> childrens; //ChildrenItem will also implement the GenericDataType that way both of the items are acceptable
}
So ones we did that we can replace the current items from
private List<EligibilityDetails> mEligsList;
private List<Items> mItemslist;
To
private List<GenericDataType> adapterItems;
Now we need to make sure that whenever we have a certain ViewHolder the item of that position will be of the right type. In order to do that we need to change our getItemViewType
#Override
public int getItemViewType(int position) {
GenericItem currentItem = adapterItems.get(position);
if (currentItem instanceOf HeaderItem) { //We have to use if else becasue java's switch does not supprt checking instancesOf
return R.layout.your_header_item_layout_id;
} else if(currentItem instanceOf ChildrenItem) {
return R.layout.your_children_item_layout_id;
} else if(repeat for any other types you have) {
return R.layout.your_other_item_layout_id;
} else {
throw new Throwable("Unsupported type"); //This should never happen but we add it to make the compiler compile
}
}
Now we can finally change our onBind method to support both types
#Override
public void onBindViewHolder(#NonNull final EligibilityAdapter.ViewHolder viewHolder, int i) {
if (viewHolder instanceOf HeaderViewHolder) {
HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
headerItem = (HeaderItem) items.get(i);
//Do rest of binding here
holder.viewToAddMoreItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!headerItem.isEpanded()) { //Add to your header type a Boolean to check if it is expanded or not
adapterItems.addAll(headerItem.getChildrens()); //They are the same generic type but the getItemViewType will handle it for us
notifyItemRangeInserted(); //Provied item start index and size of the list
} else {
adapterItems.removeAll(headerItem.getChildrens());
notifyItemRangeRemoved(); I don't remember what it require but you'll figure that out
}
headerItem.setExpanded(!headerItem.isExpanded()); //Flipping the value
}
});
} else if (viewHolder instanceOf ChildrenViewHolder) {
ChildrenViewHolder holder = (ChildrenViewHolder) viewHolder;
childrenItem = (ChildrenItem) items.get(i);
//Do your binding here
} else if(repeate for other view-types) {}
}

Related

NestedRecyclerView Problem - Last item of the second RecyclerView is duplicated into the first one after the keyboard appears

I'm desperate.
I have a nested recycler view. Each outer element has an array of inner elements. A different adapter has been created for the inner elements. I am creating an array of external elements of class "KairosWithEvents", each of which contains internal elements of class "Event". Everything is displayed well. When elements are added, everything is also updated. For testing, I created two objects of the "KairosWithEvent" class. In the first object I have placed two objects of the "Event" class, and in the second - three objects. But when I want to change the EditText value, the keyboard appears. And the last element of the second object appears in the first object. How can I fix it? Objects are not moved or duplicated, but showed incorrectly.
This is what a nestled recycler view looks like initially.
And this is what a nested recycler view looks like after the keyboard appears. The "Эвент5" element is duplicated to the first element for some reason.
Here's my code: Outer Adapter:
public class EventAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static List<Item> items;
private static RecyclerViewClickInterface listener;
private RecyclerView.RecycledViewPool sharedPool = new RecyclerView.RecycledViewPool();
private Context context;
public EventAdapter() {
items = new ArrayList<>();
this.context = context;
}
public EventAdapter(List<Item> newItems){
items = newItems;
}
class KairosViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private TextView title;
private CheckBox iv;
private RecyclerView rv;
public KairosViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv);
iv = itemView.findViewById(R.id.iv);
rv = itemView.findViewById(R.id.rvSteaks);
itemView.setBackgroundColor(Color.parseColor("#91b3f2"));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#RequiresApi(api = Build.VERSION_CODES.N)
void bind(KairosWithEvents kairosWithEvents){
title.setText(kairosWithEvents.kairos.kairosId + " = " + kairosWithEvents.kairos.title);
LinearLayoutManager layoutManager = new LinearLayoutManager(
rv.getContext(),
LinearLayoutManager.VERTICAL,
false);
List<Item> itemsEvents = new ArrayList<>();
kairosWithEvents.events.forEach(i -> itemsEvents.add(new Item(Constants.EVENT_KAIROS, i)));
SubAdapter childAdapter = new SubAdapter(itemsEvents);
rv.setLayoutManager(layoutManager);
rv.setAdapter(childAdapter);
rv.setRecycledViewPool(sharedPool);
}
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION){
listener.onItemKairosWithEvents( ((KairosWithEvents) items.get(position).object) );
}
}
#Override
public boolean onLongClick(View view) {
return false;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType){
case Constants.KAIROS:
return new KairosViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_ex, parent, false));
}
return null;
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case Constants.KAIROS:
KairosWithEvents kairos = ((KairosWithEvents) items.get(position).object);
((KairosViewHolder) holder).bind(kairos);
break;
}
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return items.get(position).type;
}
}
Inner Adapter:
public class SubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static List<Item> items;
public SubAdapter(List<Item> newItems) {
items = newItems;
}
static class EventViewHolder extends RecyclerView.ViewHolder {
private TextView title;
public EventViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv);
itemView.setBackgroundColor(Color.parseColor("#ffc8a8"));
}
void bind(Event event){
title.setText(event.eventId + " = " + event.title);
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case Constants.EVENT_KAIROS:
return new EventViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_sub, parent, false));
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case Constants.EVENT_KAIROS:
Event event = (Event) items.get(position).object;
((EventViewHolder) holder).bind(event);
break;
}
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return items.get(position).type;
}
}
In SubAdapter, remove the static keyword from your items field:
private static List<Item> items;
Should be this instead:
private List<Item> items;
You should make the same change to your outer adapter, but since there's only one of them at a time it doesn't wind up causing issues.

RecyclerView Multiple View types (View Holders) Handle item Selection count state

I'm having a sectioned recyclerview with title header and items under that catergoty. I have two viewHolders one for the title, and other for the items that come under that category. Here is my Adapter.
public class MenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final String TAG = MenuAdapter.class.getSimpleName();
private Context mContext;
private ItemClickListener itemClickListener;
private List<HeterogeneousObject> heterogeneousObjects;
public MenuAdapter(Context mContext, CartHelper helper) {
this.mContext = mContext;
heterogeneousObjects = new ArrayList<>();
}
public void itemClickListenerCallback(ItemClickListener itemClickListenerCallback) {
this.itemClickListener = menuItemClickListenerCallback;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == 0) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single_cuisine_rv, parent, false);
return new MenuAdapterViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single_cuisine_menu_items, parent, false);
return new RecycledGridViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int position) {
HeterogeneousObject object = heterogeneousObjects.get(position);
if (object.getItemType() == 0) {
if (object.getCuisine() != null) {
MenuAdapterViewHolder holder = (MenuAdapterViewHolder) viewHolder;
holder.cuisineName.setText(object.getCuisine().getCuisine_name());
}
} else {
if (object.getMenuItem() != null) {
RecycledGridViewHolder holder = (RecycledGridViewHolder) viewHolder;
holder.dishName.setText(object.getMenuItem().getItemName());
holder.dishPrice.setText(mContext.getString(R.string.rupee_symbol, object.getMenuItem().getItemPrice() * 0.01));
if (object.getMenuItem().getIsVeg()) {
holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.veg_symbol));
//holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_veg_indicator));
} else if (!object.getMenuItem().getIsVeg()) {
holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.non_veg_symbol));
//holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_non_veg_indicator));
}
Single<CartSelection> selection = cartHelper.getCartSelectionById(Long.parseLong(object.getMenuItem().getItemId()));
selection.subscribe(new DisposableSingleObserver<CartSelection>() {
#Override
public void onSuccess(CartSelection cartSelection) {
if (cartSelection != null) {
holder.incDecButton.setNumber(cartSelection.getQty(), true);
}
}
#Override
public void onError(Throwable e) {
}
});
holder.bind(object.getMenuItem(), menuItemClickListener);
}
}
}
#Override
public int getItemCount() {
return heterogeneousObjects.size();
}
#Override
public int getItemViewType(int position) {
super.getItemViewType(position);
if (heterogeneousObjects.get(position).getItemType() == 0) {
return 0;
} else {
return 1;
}
}
}
Here is my View Holder 1
public class RecycledGridViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.tv_si_dish_name)
TextView dishName;
#BindView(R.id.tv_si_dish_price)
TextView dishPrice;
#BindView(R.id.btn_id_item_dish_grid)
IncDecButton incDecButton;
#BindView(R.id.iv_veg_non_veg_grid)
AppCompatImageView vegNonVegIndicator;
public RecycledGridViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(final MenuItem menuItem, final MenuItemClickListener menuItemClickListener) {
incDecButton.setOnButtonsClickedListener(new IncDecButton.OnButtonsClickedListener() {
#Override
public void onPlusClicked(int num) {
if (menuItem.getCustomizable()) {
menuItemClickListener.onCustomizableItemClicked(menuItem, 1);
if (incDecButton.getNumber() == 0) {
incDecButton.setNumber(0, true);
}
} else {
menuItemClickListener.onPlusButtonClicked(menuItem, num);
}
}
#Override
public void onMinusClicked(int num) {
if (menuItem.getCustomizable()) {
menuItemClickListener.onCustomizableItemClicked(menuItem, 0);
} else {
menuItemClickListener.onMinusButtonClicked(menuItem, num);
}
}
});
}
View Holder 2
class MenuAdapterViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.tv_cuisine_name_menu_v2)
TextView cuisineName;
public MenuAdapterViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
I'm maintaining a state of item count that the list item in ViewHolder 1 has to show in IncDecButton outside of the ViewHolder in a ROOM Db Dao, which i query as onBindViewHolder is called for each item but the views gets recycled and item count gets misplaced.
when i only have a single view holder, i.e only View Holder 1, item counts have no issues, but it gets recycled and misplaced when there are 2 View Holders.
I don't really seem to understand what am i doing wrong.
This code works fine with only a single Viewholder (Single Item Type RecyclerView).
Views in a RecyclerView are recycled when dissapear in the screen. You should manage the state outside the ViewHolder and just use it for the presentation. You could store the current value of the count in the item data you pass to the adapter and then just show this current value in the ViewHolder.
Another important thing is to set always the value. If you do not put the correct value, the ViewHolder will maintain the recycled view, so if the count for new cells is 0, just put 0 in the binding.
Hope it helps.

android: Sort List after Adding items to the existing list using RecyclerView.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();
}

Android: why is RecyclerView list destroyed after navigating back to the list?

I create a RecyclerView list that shows a default layout. I then add a new item to the list and the layout updates to show the new item (a CardView). I then navigate back to a previous activity. When I return to the RecyclerView activity I am returned to the generic, default list and my new CardView item does not show.
I have the normal onCreate(Bundle savedInstanceState) code in the Activity. It seems like the adapter just creates a new RecyclerView list upon returning to it rather than returning to the previously created list. What am I missing here?
Activity:
public class ListContactsActivity extends AppCompatActivity {
private ListContactsAdapter mContactsAdapter;
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerviewlist);
final List<Contact> mContacts;
mContacts = new ArrayList<>();
mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mContactsAdapter = new ListContactsAdapter(this, mContacts);
mContactsAdapter.setOnItemTapListener(new ListContactsAdapter.OnItemTapListener() {
#Override
public void onItemTap(Contact contact, int position) {
mContactsAdapter.removeItem(contact, position);
}
});
mRecyclerView.setAdapter(mContactsAdapter);
...
Adapter:
public class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}
private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView cardBlankText2;
public ContactViewHolder(View itemView) {
super(itemView);
cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mOnItemTapListener != null) {
Contact contact = mContacts.get(getLayoutPosition());
mOnItemTapListener.onItemTap(contact, getLayoutPosition());
}
}
}
private Context mContext;
private LayoutInflater mLayoutInflater;
private List<Contact> mContacts;
private List<ListItem> mItems;
private OnItemTapListener mOnItemTapListener;
public ListContactsAdapter(Context context, List<Contact> contacts) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContacts = contacts;
mItems = buildItemsList();
}
public void setOnItemTapListener(OnItemTapListener listener) {
mOnItemTapListener = listener;
}
private List<ListItem> buildItemsList() {
List<ListItem> items = new ArrayList<>();
if (mContacts.size() > 0) {
for (Contact contact : mContacts) {
items.add(new ContactItem(contact));
}
} else {
// when R. list is first created or when the number of cards
// is deleted until there are zero, show the defaultcard_layout
// and "Click the + above to get started".
for (int i=0; i<1; i++) {
items.add(new EmptyItem());
}
}
return items;
}
public void addItem(Contact contact) {
if (mContacts.size()==0) {
// if list is empty
// remove empty cards first
mItems.clear();
notifyDataSetChanged();
}
// add item on the top of the list and scroll to the top position
mContacts.add(contact);
mItems.add(new ContactItem(contact));
notifyItemInserted(0);
}
public void removeItem(Contact contact, int position) {
mContacts.remove(contact);
if (mContacts.size()==0) {
// if no more contacts in list,
// rebuild from scratch
mItems.clear();
mItems.addAll(buildItemsList());
notifyDataSetChanged();
} else {
// else remove one item
mItems.remove(position);
notifyItemRemoved(position);
}
}
#Override
public int getItemCount() {
return mItems.size();
}
#Override
public int getItemViewType(int position) {
return mItems.get(position).getType();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ListItem.EMPTY_TYPE) {
View itemView = mLayoutInflater.inflate(R.layout.defaultcard_layout, parent, false);
return new EmptyViewHolder(itemView);
} else {
View itemView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
return new ContactViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
int type = getItemViewType(position);
if (type == ListItem.CONTACT_TYPE) {
ContactItem item = (ContactItem) mItems.get(position);
final Contact contact = item.getContact();
ContactViewHolder holder = (ContactViewHolder) viewHolder;
holder.cardBlankText2.setText(contact.getName() + " " + contact.getSurname());
}
}
}
When an activity is popped off the stack by navigating back, it doesn't retain any of its views or state. The activity object and all its views and state are destroyed. If you need to somehow save what an activity was doing and need to restore that, you need to write the code to do that.

how to make the header of Recycle view invisible

I have a Recyclerview with header view as slider images and remaining view as normal recycler items.I am wondering if there is any way around to make the header view invisible depending upon some sort of condition.The recycler view consists of two separate layout files for this purpose: layout1 for header items and layout2 for normal recycler items and adapter will pick a layout and binds corresponding data at runtime.
This is my RecyclerView adapter RestaurantAdapter.java
public class RestaurantAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = RestaurantAdapter.class.getName();
private List<Restaurant> mList;
private Context mContext;
private RestaurantType mRestaurantType;
private static final int RECYCLER_HEADER=0,RECYCLER_ITEMS=1;
private LayoutInflater inflater;
private SlideItemViewHolder slideItemViewHolder;
private List<ImageSliderPOJO> mData;
public RestaurantAdapter(Context context, List<Restaurant> list, RestaurantType restaurantType) {
this.mContext = context;
this.mList = list;
this.mRestaurantType = restaurantType;
inflater=LayoutInflater.from(context);
}
public void updateAdapter(List<ImageSliderPOJO> data){
this.mData = data;
this.notifyDataSetChanged();
}
#Override
public int getItemCount() {
return mList.size();
}
#Override
public int getItemViewType(int position) {
return position == 0 ? RECYCLER_HEADER : RECYCLER_ITEMS;
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
int viewType=viewHolder.getItemViewType();
switch (viewType){
case RECYCLER_HEADER:
slideItemViewHolder = (SlideItemViewHolder) viewHolder;
slideItemViewHolder.updateHeader();
break;
case RECYCLER_ITEMS:
final RecyclerItemViewHolder holder = (RecyclerItemViewHolder) viewHolder;
final Restaurant restaurant = mList.get(i);
Picasso.with(mContext)
.load(restaurant.getVendorLogo())
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(holder.restaurentImageView);
holder.restaurentNameTextView.setText(restaurant.getName());
//Remaining code here
break;
default:
throw new RuntimeException(TAG+":Unable to bind the viewType"+viewType);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch (viewType){
case RECYCLER_HEADER:
return new SlideItemViewHolder(inflater.inflate(R.layout.slide_show_restaurant_fragment_list,viewGroup,false));
case RECYCLER_ITEMS:
return new RecyclerItemViewHolder(inflater.inflate(R.layout.new_restautant_list_items, viewGroup, false));
default:
throw new RuntimeException(TAG+":Invalid ViewType "+viewType);
}
}
public static class RecyclerItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ClickListener clickListener;
//Initialization here.
public RecyclerItemViewHolder(View v) {
super(v);
ButterKnife.inject(this, v);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
clickListener.onClick(v, getPosition(), false);
}
public interface ClickListener {
/**
* Called when the view is clicked.
*
* #param v view that is clicked
* #param position of the clicked item
* #param isLongClick true if long click, false otherwise
*/
public void onClick(View v, int position, boolean isLongClick);
}
/* Setter for listener. */
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
}
// id = 87,170
private class SlideItemViewHolder extends RecyclerView.ViewHolder {
SliderLayout sliderLayout;
LinearLayout rootLinearLayout;
public SlideItemViewHolder(View recyclerHeader) {
super(recyclerHeader);
sliderLayout = (SliderLayout) recyclerHeader.findViewById(R.id.home_slider);
rootLinearLayout = (LinearLayout) recyclerHeader.findViewById(R.id.rootLinearLayout);
}
private void updateHeader() {
if(Util.isNetworkAvailable(mContext)){
for (int i = 0; i < mData.size(); i++) {
DefaultSliderView defaultSliderView = new DefaultSliderView(mContext);
final int finalI = i;
defaultSliderView.image(mData.get(finalI).getImageUrl())
.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
#Override
public void onSliderClick(BaseSliderView slider) {
Restaurant restaurantById = Restaurant.searchByRestaurantId(mData.get(finalI).getTargetVendorId());
if(restaurantById != null)
openDetailFragment(restaurantById);
}
});
sliderLayout.addSlider(defaultSliderView);
}
}
}
}
public void openDetailFragment(Restaurant restaurant) {
Intent intent = new Intent(mContext, DetailTabActivity.class);
intent.putExtra(DetailTabActivity.INTENT_RESTAURANT_DATA, restaurant);
mContext.startActivity(intent);
}
public SliderLayout getSliderLayout(){
return slideItemViewHolder.sliderLayout;
}
}
And this adapter is set and updated from this fragment RestaurantFragment.java as:
private void setAdapter() {
dismissDialog();
if (getActivity() != null)
getActivity().runOnUiThread(new Runnable(){
#Override
public void run() {
if (restaurantList != null && restaurantList.size() > 0) {
restaurantRecyclerView.setVisibility(View.VISIBLE);
mEmptyListTextView.setVisibility(View.GONE);
restaurentListAdapter = new RestaurantAdapter(getActivity(), restaurantList, mRestaurantType);
restaurantRecyclerView.setAdapter(restaurentListAdapter);
restaurentListAdapter.updateAdapter(mData);
restaurantRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
#Override
public void onLoadMore(int current_page) {
mCurrentPage = mCurrentPage + 1;
getHttpResturantData();
}
});
}
}
});
}
Is this much of an explanation helpful or should I paste more code?
Based on your code, it's possible to remove the header based on a certain condition.
Adjust your code to cater for the following:
#Override
public int getItemViewType(int position) {
if(hasHeaeder()) { // where you add the header
return position == 0 ? RECYCLER_HEADER : RECYCLER_ITEMS;
} else { // where you don't add the header
return RECYCLER_ITEMS;
}
}
This code also needs changing (currently it's wrong since it doesn't take care of the fact that the header adds 1 to the position).
final Restaurant restaurant = mList.get(i);
Replace it with
final Restaurant restaurant = hasHeader() ? mList.get(i +1) : mList.get(i);
Where hasHeader() is the code you need to write in order to determine whether or not the recycler should contain a header.

Categories

Resources