gpt4 book ai didi

android - 旋转屏幕后 Recyclerview 不会调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:11 24 4
gpt4 key购买 nike

我有一个 Recyclerview,其中填充了用户插入的项目。我正在实现旋转功能,因为当我转动设备时,recyclerview 显示为空。

调试应用程序时,我已经检查了旋转前后的完整性,并且 ArrayList 具有相同的大小。我认为问题是 Recyclerview 的 setNestedScrollingEnabled(false),我设置它是因为我不想在 rv 中显示滚动。

问题是:我在纵向模式下添加了 3 个项目,它在 recyclerview 中显示我很完美。检查图像:

Portrait

当我将屏幕旋转到横向时,recyclerview 的数组列表有 3 个项目,但高度没有改变,所以它似乎只有一个项目。

enter image description here

那么,我该如何解决这个问题?

回收者 View :

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);

布局:

  <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>

我尝试将 android:layout_height 更改为 wrap_contentmatch_parent 但没有成功。

非常感谢!

最佳答案

我找到了解决方案。

我像这样将 Recyclerview 放在 RelativeLayout 中:

<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>

在代码中,我添加了这个

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);

它工作得很好,我从 https://stackoverflow.com/a/34543165/3200714 得到了灵感

关于android - 旋转屏幕后 Recyclerview 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com