使用重量时,Recyclerview不占用整个宽度

时间:2016-06-27 19:22:21

标签: android android-recyclerview

我有一个

       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 

我的Recyclerview只占用了宽度的一半,但是frame2已经消失了。如果我使用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>

2 个答案:

答案 0 :(得分:4)

按照此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); 

答案 1 :(得分:0)

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

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

相关问题