2

I have a

       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 

My Recyclerview is occupying only half the width though frame2 is gone. If I use listview instead of RecyclerView, I dont have this issue. Can anyone suggest how to make Recyclerview occupy whole width ?

EDIT: If I scroll the recyclerview , it is taking up the whole width as expected.

Adapter

    @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>
Siju
  • 2,585
  • 4
  • 29
  • 53

2 Answers2

4

Solved the issue by following this SO post . Any better solution will be appreciated.

Recyclerview with width 0dp

Solution is to basically call

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); 
Community
  • 1
  • 1
Siju
  • 2,585
  • 4
  • 29
  • 53
1

Since you have two frame layouts with each having an equal weight, they will each occupy half the screen. Even if the visibility of the second frame layout is gone.

What do you need the second frame layout for? When are you planning to make it visible? And what do you want the first recycler view to look like when the second one is also visible?

Nishita
  • 870
  • 1
  • 9
  • 33
  • The same layout works for ListView and also when I scroll recyclerview it takes up whole width. – Siju Jun 28 '16 at 04:28