【问题标题】:Last row of expandable listview not full view in android可扩展列表视图的最后一行不是android中的完整视图
【发布时间】:2017-04-07 11:06:45
【问题描述】:

这里我使用视图和下面给出的代码。

 <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>

我使用 epandablelistview 作为下面的类。

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;
    }
}

我参考了这个问题,但没有得到正确的答案。 Grid of images inside ScrollView

Gridview height gets cut

当第一行高度不大于第二行或其他时,会出现主要问题。如果所有行内容长度都相同,则没有问题。

【问题讨论】:

  • 显示列表视图内容保持 xml
  • 请检查我已添加 xml @IntelliJAmiya
  • 请显示完整的xml
  • 太多视图无法在此处显示.. 仅可扩展列表和可扩展列表视图类中的问题
  • 检查是否存在底部的填充或底部的边距

标签: android listview gridview expandablelistview


【解决方案1】:

看起来您正在获取 ListView 的 LayoutParams,修改了 height 参数,但您从未调用 setLayoutParams 来告诉视图使用修改后的高度。试试这个:

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);
}

此外,我过去在使用 onMeasure() 方法时遇到过问题,当框架调用该方法时,并非所有内容都被测量。您可以尝试ViewTreeObserver,您可以在其中注册一个监听器,以便在布局完整视图时收到通知,如下所示:

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
        }
    });
}

【讨论】:

    【解决方案2】:

    如果只有一个孩子,那么最好将ExpandableListViewRecyclerView 与不同的layouts 一起使用。 Here 是一个完整且非常好的使用ExpandableListView 的教程。

    在使用ListView 时,它总是增加比原始高度略低的高度(或者可能还有另一种情况,但我不知道为什么。如果有人知道,请纠正我)。所以你必须在最后手动添加一些dp's 高度。

    我已经通过覆盖onDraw 方法来完成它。以下是我的做法。

    @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);
    }
    

    【讨论】:

      【解决方案3】:

      您正在修改高度,但没有将其传递给视图。

      使用这个方法,setLayoutParams(params);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 2023-03-08
        相关资源
        最近更新 更多