3

I have a simple linear layout which I'm inflating in an adapter:

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.category_listview_row, parent, false);
        } else {
            ((LinearLayout)convertView).removeAllViews();
        }

        if (LAYOUT_TYPES.GRID.equals(layoutType)) {
            convertView = CategoryFragment.getViewForGridLayout(context, displayArray, position, convertView, listener);
        } else {
            convertView = CategoryFragment.getViewForListLayout(context, displayArray, position, convertView, listener);
        }

        return convertView;
    }

Here, category_listview_row is the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

The getViewForGridLayout method programmatically creates one or more views and adds them to convertView.

I would like all the child views to match this parent view in height, however I can't get this to work. This is the outer linear layout of the child views that are added programmatically:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/base_listview_style_one_layout"
    android:orientation="horizontal"
    android:padding="@dimen/node_default_spacing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:background="#5ab9c6">

This layout has subviews which are removed according to the actual data to be displayed, so some will be longer than others. However I want them all to be the height of the parent so it doesn't look weird.

This is a screenshot of what I'm seeing:

  • The first row is ok since both items have a title and a subtext
  • On the second row, the second item doesn't have a subtext so that view is smaller. However, I'd like it to take the full height of the row so all items in a single row will have the same height.

enter image description here

Any tips? Thanks!

zundi
  • 2,361
  • 1
  • 28
  • 45
  • Looks like some horizontal scroller to me. Not sure what's your problem but I see that probably the android:layout_width for the LinearLayout subviews should be wrap_content. – MarkSkayff Dec 30 '16 at 20:36
  • "however I can't get this to work" -- what are you seeing? What are the actual views that are actually not the right height? When you use Android Studio's Layout Inspector to examine this stuff, what does it show you? "This is the outer linear layout of the child views that are added programmatically" -- does your background color have the right height? – CommonsWare Jan 01 '17 at 21:23
  • @CommonsWare I've added a screenshot to better describe what I'm seeing. Thanks – zundi Jan 02 '17 at 14:14
  • 2
    You may have an easier time if you use something actually set up to implement a grid (e.g., `RecyclerView` with `GridLayoutManager`), rather than trying to fake it with a `LinearLayout`. Otherwise, all I can suggest is using Android Studio's LayoutInspector to try to determine why your rules are not working as you would expect. – CommonsWare Jan 02 '17 at 14:19
  • If I recall correctly, I tried using a `RecyclerView` with a `GridLayoutManager` but being able to have some rows with 2 items and other rows with 1 item (and that fills the whole row) wasn't straightforward. I'll re-read the `GridLayoutManager` docs. – zundi Jan 02 '17 at 14:39
  • what if you dont reuse convert view? – RadekJ Jan 02 '17 at 16:11
  • "but being able to have some rows with 2 items and other rows with 1 item (and that fills the whole row) wasn't straightforward" -- attach a `SpanSizeLookup` to the `GridLayoutManager`, where you provide a `getSpanSize()` method to indicate the number of cells to span for a given position. And you're done. There is a section on this in my book's chapter on `RecyclerView`, with [a `VideoTable` sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.1/RecyclerView/VideoTable) demonstrating its use. – CommonsWare Jan 03 '17 at 12:28
  • @zundi How you pass data.there is image heading description.. Can you show one item with data that you pass into your list – Charuක Jan 09 '17 at 03:57

8 Answers8

3

I would advise using a Recyclerview with GridLayoutManager.

If you want certain items spanning multiple columns, you can do so by setting SpanSizeLookup on the GridLayoutManager.

Here is a simple example https://stackoverflow.com/a/26907508/4498224.

Community
  • 1
  • 1
SnyersK
  • 1,296
  • 8
  • 23
1

Propably your layout params are ignored. Be sure that you are adding child views to your convertView like this:

View view = inflater.inflate( R.layout.item /* resource id */,
                                     convertView /* parent */,
                                     true /*attachToRoot, you dont need to call addView then*/);
RadekJ
  • 2,835
  • 1
  • 19
  • 25
1

Instead of removing the textview, why not just make it invisible or set its color to white to it occupies the space?

John Quasar
  • 186
  • 2
  • 14
1

Please change your linearlayout height from wrap content to "match_parent".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />`
Parin Parikh
  • 385
  • 1
  • 6
0
  1. Set the height of parent layout to wrap_content.
  2. Set the height of child view to match-parent.

Try this i hope it will resolve your issue.

user320676
  • 394
  • 2
  • 19
0

@zundi you can use view holder design pattern with recycler view adapter and define which row needs to have two images and which one needs to have one. Its pretty much simpler than doing it with linear layout.

Recycleview show different view types has a pretty decent explanation on how to do this.

And also to answer you question of making

some rows with 2 items and other rows with 1 item

you can use setSpanSizeLookup (GridLayoutManager.SpanSizeLookup spanSizeLookup) method of GridLayoutManager and define it in the activity.

RecycleView's span size gives you more info on how to achieve that.

mLayoutManager.setSpanSizeLookUp(new GridLayoutManager.SpanSizeLookUp() {
     @Override
     public int getSpanSize(int position) {
         if(position == 0) 
            return 2; //here the view takes up two spaces in a row(header)
         else return 1; //here view takes 1 space i.e., 2 views in a total row
    } });

In the above example my grid layout manager takes 2 view holders based on different positions and decides if its header or not and populates the data.

Other Solution:

Rather than giving match parent and wrap parent inside your layout give a fixed 'dp' for height. That should make the views look consistent.

Community
  • 1
  • 1
shreknit
  • 117
  • 9
0

@zundi if doing it right way with RecyclerView and LayoutManager which would simplify you life dramatically but have a bit of learning curve doesn't suits you, here is an option that hasn't been mentioned yet:

You can define android:lines=x on TextView which contains subtext.

Just match_parent is not going to work for you because you want LinearLayout to wrap height of its children and its children to match_parent which is a circular dependency

Happy Dev
  • 573
  • 10
  • 17
0

This method can be performed on both Linear Layout or TextView you are using

You have set

android:layout_height="match_parent"

but not the height, so it is simple case

android:layout_height="match_parent"

So, in case even if there will be no value in the string it will still contain the height allotted to it

Kushal Ramola
  • 151
  • 1
  • 5