【问题标题】:Recyclerview not occupying whole width when used with weight与重量一起使用时,Recyclerview 不占据整个宽度
【发布时间】:2016-06-28 22:25:28
【问题描述】:

我有一个

       1. Framelayout(frame1) in which I am inflating a RecyclerView layout with weight 0.5 

       2. Framelayout(frame2) with visibility gone and weight 0.5 

尽管 frame2 消失了,但我的 Recyclerview 只占据了一半的宽度。如果我使用 listview 而不是 RecyclerView,我就没有这个问题。谁能建议如何让 Recyclerview 占据整个宽度?

编辑: 如果我滚动 recyclerview ,它会按预期占据整个宽度。

适配器

    @Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
    ListViewHolder tvh = new ListViewHolder (v);
    return tvh;
}

main_layout

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <FrameLayout
            android:id="@+id/frame1"
            android:layout_width="0dp" // If I put match_parent here it works
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            />

        <FrameLayout
            android:id="@+id/frame2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:visibility="gone" />
    </LinearLayout>

frame1_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

recycler_item

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="5dp" >



<ImageView
    android:id="@+id/imageIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_main" />

<TextView
    android:id="@+id/textName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:layout_toRightOf="@+id/imageIcon"
    android:textSize="14sp" />

 <TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="Test"
    android:layout_below="@+id/textName"
    android:textSize="12sp" />

</RelativeLayout>

【问题讨论】:

  • 你试过wrap_content吗?

标签: android android-recyclerview


【解决方案1】:

通过关注此 SO 帖子解决了该问题。任何更好的解决方案将不胜感激。

Recyclerview with width 0dp

解决方法基本上是调用

recyclerView.setAdapter(adapter);
/* Make sure you call all the below stuff after you set the adapter or
   in your loadFinished() incase you are using loading data using Loaders*/
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setAutoMeasureEnabled(false);  // This is the key
recyclerView.setLayoutManager(llm); 

【讨论】:

    【解决方案2】:

    由于您有两个框架布局,每个布局具有相同的权重,它们将分别占据屏幕的一半。即使第二帧布局的可见性消失了。

    你需要第二个框架布局做什么?你打算什么时候让它可见?当第二个也可见时,您希望第一个回收器视图看起来像什么?

    【讨论】:

    • 同样的布局适用于 ListView,而且当我滚动 recyclerview 时它会占据整个宽度。
    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 2021-01-30
    • 2018-08-03
    • 2018-08-17
    • 2020-11-05
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多