9

Red mark Row view

Here I uses view and there code as per given below.

 <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">

                            <TableRow
                                android:id="@+id/tab_stone_info"
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:layout_marginTop="1dp"
                                android:background="@color/styleBreakTabColor"
                                android:weightSum="9">

                                <TextView
                                    android:id="@+id/stone_Type"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Type"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_Shape"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Shape"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_Qlty"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Quality"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_Grade"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Grade"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_Qty"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Qty"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_Wt"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Wt"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_MMSize"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/MMSize"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_Setting"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/Setting"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                                <TextView
                                    android:id="@+id/stone_SettMode"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_weight="1"
                                    android:background="@drawable/cell_shape"
                                    android:gravity="center"
                                    android:text="@string/SettingMode"
                                    android:textColor="@color/txtStyleBreakup"
                                    android:textSize="12sp" />

                            </TableRow>

                            <RelativeLayout
                                android:id="@+id/layout_tab_stone_info_value"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"

                               >

                                <com.mindtech.ExpandableHeightListView
                                    android:id="@+id/listviewstone"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:divider="#00000000"
                                    android:gravity="center"
                                    android:horizontalSpacing="2dp"
                                    android:isScrollContainer="false"
                                    android:stretchMode="columnWidth"
                                    android:verticalSpacing="20dp"
                                     />
                            </RelativeLayout>
                        </LinearLayout>

And I uses epandablelistview as below class.

public class ExpandableHeightListView extends ListView
{

    boolean expanded = false;

    public ExpandableHeightListView(Context context, AttributeSet attrs,
                                    int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public ExpandableHeightListView(Context context)
    {
        super(context);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }



    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {

//        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
//                MeasureSpec.AT_MOST);

//
//        super.onMeasure(widthMeasureSpec, expandSpec);


//        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
//                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
//        ViewGroup.LayoutParams params = getLayoutParams();
//        params.height = getMeasuredHeight();


        if (isExpanded())
        {

            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

I refer this question but did not get correct. Grid of images inside ScrollView

Gridview height gets cut

The main problem occur when first row height is not biger than second or other. if all row content length are same then no issues.

Community
  • 1
  • 1
Ramchandra Singh
  • 530
  • 4
  • 15

3 Answers3

1

It looks like you are getting the LayoutParams for your ListView, modifying the height parameter, but you are never calling setLayoutParams to tell the view to use the modified height. Try this:

if (isExpanded()) {

    int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
            MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);

    ViewGroup.LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();
//Add this line
    setLayoutParams(params);
}

Also, I've ran into problems in the past with the onMeasure() method, not everything is measured when the framework calls that method. You might try a ViewTreeObserver, where you can register a listener to be notified when the full view is laid out, like this:

final LinearLayout layout = (LinearLayout) findViewById(R.id.layout_id);
ViewTreeObserver vto;
if (layout != null) {
    vto = layout.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            //Modify your LayoutParams here
        }
    });
}
Jason Powell
  • 361
  • 4
  • 12
0

If there's only one child, then it's better to use ExpandableListView or RecyclerView with different layouts. Here is a complete and very good tutorial on using ExpandableListView.

While using ListView, it always adds height which is a bit less than the original one (or may be there's another case, but I don't know why. If someone knows, than kindly correct me). So you have to add some dp's height manually at the end.

I've done it by overriding onDraw method. Below is how I did it.

@Override
protected void onDraw(Canvas canvas) {
    params = getLayoutParams();
    if (getCount() != old_count) {
        old_count = getCount();
        params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight()+5 : 0);
        //I've added 5 pixels with each item to get proper height, you can alter it as per your need
        setLayoutParams(params);
    }
    super.onDraw(canvas);
}
Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
0

You are modifying the height, but you are not passing it to the view.

Use this method, setLayoutParams(params);