2

I inflate the view in this method:

public TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tabs_layout, parent, false);
    return new TabViewHolder(view);
}

Because of "False", my LinearLayout doesn't attach to it's parent therefore there is no parent and i can't use Layer_Weight (I tested with view.getParent(). it returns null). Because when i use Layer_Weight in combination with "Layer_Width = 0dip", LinearLayout disappears.

If i use "True" as third parameter(view.getParent() returns the parent), i get this error:

"The specified child already has a parent. You must call removeView() on the child's parent first.".

This is the parent:

<android.support.v7.widget.RecyclerView android:id="@+id/tabBar" style="@style/tabBar"/>

And this is the parent style:

<style name="tabBar">
     <item name="android:layout_width">match_parent</item>
     <item name="android:layout_height">0dip</item>
     <item name="android:layout_weight">0.1</item>
     <item name="android:weightSum">1</item>
     <item name="android:background">@color/headerBg</item>
</style>

And this is LinearLayout, the child style:

<style name="tabLayout">
     <item name="android:layout_width">0dip</item>
     <item name="android:layout_height">match_parent</item>
     <item name="android:layout_weight">0.5</item>
     <item name="android:orientation">horizontal</item>
     <item name="android:gravity">center_vertical</item>
 </style>

And this is the grandchild, TextView:

<style name="tabItem">
     <item name="android:layout_width">wrap_content</item>
     <item name="android:layout_height">wrap_content</item>
</style>

How can i set the LinearLayout width, half of it's parent?

Appreciate it in advance.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Arash
  • 696
  • 8
  • 24
  • The problem is that your views don't appear as a child of `RecyclerView` - they appear as children of the views created by your layout manager. You would have to set weight sum for them, but you can't. What is what you're trying to achieve? For me it looks like your issue would be resolved if you set your entire `RecyclerView`'s width to half width of its parent – wasyl Sep 25 '15 at 17:40
  • Thank you wasyl. I have a fragment Container and some fragments to be load. Some of these fragments are changed with the something like tab but others will changed with the menu and those fragments have no tabs inside. I use the recyclerView as a tab container and in one screen should be only two tabs with equal width and the recyclerView should be horizontal. I already used layer_weight with layer_height in the recyclerView. How can i use it with layer_width? – Arash Sep 25 '15 at 18:45

1 Answers1

1

I used this way to achieve the goal.

I sent the recyclerView, the parent, as a parameter to the tabBarAdapter which inflates the linearLayout, the child, and in this adapter, set linearLayout's width to half width of recyclerView in the onCreateViewHolder method.

view.getLayoutParams().width = tabBar.getWidth()/2;

I think, this is against "Loose Coupling" (I wanted to separate styles from code completely).

Arash
  • 696
  • 8
  • 24
  • Thank you, Diogo Garcia. But i didn't change my code and my app is still working correctly and my android version is 6.0.1. or newer version of recyclerview is new syntax. – Arash Jun 20 '16 at 11:05