How to make RecyclerView change the size of the list, tobish compressed.

The fact is that after adding RecyclerView, I added an ordinary view (I did something like Footer for RecyclerView).

When you delete an item from RecyclerView, it is deleted. But the value of Height does not change. Also it turns out that View which under RecyclerView lies. He doesn't move.

Found such a method hasfixedsize it will help solve my problem or is there something else?

  • hasfixedsize does not help here. You can change the size of the RecyclerView only by k-l fixed value. Those. Complex calculations are needed and this will not be the right decision. Standard components do not implement this. You will have to hang a scrolling listener and cut very securely complex calculations for dynamic positioning of the footer, depending on, probably, whether the last visible element is the last one in general and its location relative to the lower edge of the screen. - Yuriy SPb
  • @ YuriSPb, awesome !. How then to add footer? 2 lines page and everything - Andro
  • The problem is that you did not describe the footer. Footer tightly riveted should be to the bottom of the screen and should not move. The only way out is to fence the hellish code through the hellish calculations. I once tried to find a ready-made solution to such a problem and did not find it. As a result, he didn’t cut - YurySPb
  • @Yuriy SPb, why are you scaring people here? - Flippy
  • Look here. You just need to use library version 23.2.1 of stackoverflow.com/questions/26649406/… - Flippy

1 answer 1

You can add a footer as the last element of the RecyclerView itself. You can select a new RecyclerItem class with the isFooter boolean field, wrap all the items in it, put it in an array, and add recyclerItem with isFooter = true at the end. Use the resulting array to initialize the adapter. Then you need to describe an alternative layout for the footer, for example footer_item.xml and override the adapter's getItemViewType () like:

@Override public int getItemViewType(int position) { return items.get(position).isFooter ? 1 : 0; } 

in the onCreateViewHolder () of the adapter, enter a condition under which, if the viewType of the element is 1 (isFooter == true), bind the corresponding layout to the holder:

 @Override public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RelativeLayout itemLayout; switch (viewType) { case 0: itemLayout = (RelativeLayout)LayoutInflater.from(parent.getContext()) .inflate(R.layout.task_item, parent, false); break; case 1: itemLayout = (RelativeLayout)LayoutInflater.from(parent.getContext()) .inflate(R.layout.footer_item, parent, false); } return new ViewHolder(itemLayout, viewType); } 

Well, in onBindViewHolder () it is similar to linking data depending on the type of element:

 @Override public void onBindViewHolder(ViewHolder holder, int position) { switch (holder.viewType) { case 0: //для обычного элемента break; case 1: //для футера } } 
  • 2
    The code you presented will not place the last element of the list at the bottom of the screen if the other elements do not fit on the screen - YuriiSPb
  • one
    @YuriiSpb, the author just need a footer that will always be under the list - Flippy
  • @YuriiSpb, yes, this is not a secret. - Keanu_Reeves
  • LLC is what you need, thanks for all the help))) - Andro