4

I have a custom ViewGroup that inflates a ListView from an xml layout. The list item layout is inflated from another xml file. All of the views are set to fill_parent. The ListView fills its parent, but the ListView items don't.

I've tried putting the ListView in a LinearLayout and assigning weight to it. Tried RelativeLayout as well. Also, I've built the ListView programmaticaly, without using the xml layout. Even changed the LayoutParams before adding the view to the ViewGroup.

I've also taken in consideration these posts as well: Width of clickable area in ListView w/ onListItemClick, In Android, how can I set a ListView item's height and width?, Android Listview width prob.

Any ideas to why the items don't extend to fill width? And how to extend them?

MyViewGroup class:

public class MyViewGroup extends ViewGroup {

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        generateMyViewGroup();

    }

    private void generateMyViewGroup()
    {
        ListView main = (ListView) View.inflate(getContext(), R.layout.layout_main, null);

        main.setAdapter(new MyAdapter(getContext()));

        this.addView(main);     
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        this.getChildAt(0).layout(l, t, r, b);
    }
}

ListView xml layout:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dp"
    android:background="#77000000"
    android:cacheColorHint="#00000000"
    android:divider="#00000000"
    android:dividerHeight="0dp"
    android:drawSelectorOnTop="false"
    android:scrollbars="vertical" >
</ListView>

ListView item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main_category"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/mainBackground"
    android:gravity="fill_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/main_category"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:paddingBottom="7dp"
        android:paddingLeft="20dp"
        android:paddingRight="5dp"
        android:paddingTop="20dp"
        android:text="test"
        android:textColor="@color/mainCategory"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

Part of MyAddapter class:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if(null == convertView)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.layout_main_category, null);

        viewHolder = new ViewHolder();

        viewHolder.mainItemTitle = (TextView) convertView.findViewById(R.id.main_category);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag(); 
    }

    viewHolder.mainItemTitle.setText("text");

    return convertView;
}

private static class ViewHolder {
        public TextView menuItemTitle;
}
Community
  • 1
  • 1
slybloty
  • 6,346
  • 6
  • 49
  • 70

2 Answers2

1

The ViewGroup didn't end up working properly. I've also tried to use LinearLayout. But RelativeLayout ended up properly inflating the layout. So, my xml layouts and the adapter stayed unchanged, but I changed the main class to extend RelativeLayout. Also removed the onLayout().

MyView class:

public class MyView extends RelativeLayout {
    //Extra code unchanged ....

    private void generateMyView()
    {
        ListView menu = (ListView) View.inflate(getContext(), R.layout.layout_main, null);

        menu.setAdapter(new MyAdapter(getContext()));

        this.addView(menu);
    }

    //Removed onLayout()
}
slybloty
  • 6,346
  • 6
  • 49
  • 70
0

If you override the onLayout, you should override the onMeasure as well.. almost always..

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
   int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
   int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
   this.setMeasuredDimension(parentWidth, parentHeight);
   // this.setLayoutParams(new 
          //   *ParentLayoutType*.LayoutParams(parentWidth,parentHeight));
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

If it still doesn't work, use the commented line too..

Update:

Another option for you is to forget onLayout and onMeasure, and override onFinishInflate() and add these lines to it. If you are inflating MyViewGroup from xml.

protected void onFinishInflate () {
    super.onFinishInflate();
    ListView main = (ListView) View.inflate(getContext(), 
                                    R.layout.layout_main,null);
    main.setAdapter(new MyAdapter(getContext()));
    this.addView(main); 
}
Ron
  • 24,175
  • 8
  • 56
  • 97