Recyclerview doesn't resize after screen rotation

I have a Recyclerview populated with elements inserted by the user. I implement the funtionality of rotation, because when I rotate the device, recyclerview shows an empty one.

Debugging the application, I already check the integrity after and before the rotation, and the ArrayList are the same size. I think the problem is setNestedScrollingEnabled(false) Recyclerview, I set this because I don't want to show scroll in rv.

The problem is : I'm in portrait mode, and I'm adding 3 elements, this shows that I'm perfect in recyclerview. Check image:

Portrait

When I rotate the screen to landscape, the arraylist recyclerview has 3 elements, but the height does not change, so it has only one element.

enter image description here

So how do I resolve this?

Recyclerview:

 itemsRv = (RecyclerView) findViewById(R.id.itemsRv); itemsRv.setNestedScrollingEnabled(false); itemAutoCompleteAdapter = new ItemAutoCompleteAdapter(this); if(items ==null){ items = new ArrayList<>(); } itemsAdapter = new ItemsRowAdapter(this, items, new ItemsRowAdapter.itemsRowListener() { @Override public void editarItemOnClick(View v, int position) { editar_item(items.get(position), position); } @Override public void eliminarItemOnClick(View v, final int position) { } }); itemsRv.setHasFixedSize(true); LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); itemsRv.setLayoutManager(mLayoutManager); itemsRv.setAdapter(itemsAdapter); 

Markup:

  <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp"> <android.support.v7.widget.AppCompatTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_marginBottom="4dp" android:text="Items" android:textSize="16sp" android:textStyle="bold" /> <LinearLayout android:id="@+id/header_rv_items" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="visible"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="4"> <TextView android:id="@+id/textView3" android:layout_width="@dimen/widthColumnTable" android:layout_height="wrap_content" android:layout_weight="2.8" android:textStyle="bold" android:text="Descripción" /> <TextView android:id="@+id/textView5" android:layout_width="@dimen/widthColumnTable" android:layout_height="wrap_content" android:layout_weight="0.5" android:gravity="right" android:textStyle="bold" android:text="Cantidad" /> <TextView android:id="@+id/textView8" android:layout_width="@dimen/widthColumnTable" android:layout_height="wrap_content" android:layout_weight="0.7" android:gravity="right" android:textStyle="bold" android:text="Precio total" /> <TextView android:text="Acciones" android:textStyle="bold" android:layout_width="100dp" android:gravity="right" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <android.support.v7.widget.RecyclerView <----- HERE IT IS android:id="@+id/itemsRv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_01_agregar_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_marginTop="20dp" android:background="@drawable/border_spinner" android:gravity="left|center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:text="Ingrese el código o descrpción del producto" android:textAllCaps="false" android:textColor="#616161" /> </LinearLayout> </android.support.v7.widget.CardView> 

I tried to easily change android:layout_height to wrap_content and match_parent .

Thank you very much!

+5
source share
1 answer

I have found a solution.

I will put a Recyclerview inside a RelativeLayout as follows:

 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/itemsRv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> 

In the code I will add this

 itemsRv.setHasFixedSize(true); LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); itemsRv.setLayoutManager(mLayoutManager); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //<--- ADD itemsRv.setLayoutParams(lp);//<--- ADD itemsRv.setAdapter(itemsAdapter); 

And it works great, I got the idea from fooobar.com/questions/553496 / ...

+3
source

Source: https://habr.com/ru/post/1263745/


All Articles