使用自定义适配器和scrollview获取listview的所有高度

时间:2015-04-16 12:10:55

标签: android listview scrollview

我想将listview与自定义适配器一起使用并动态更改。 我想要(必须滚动ListView以上的元素):

enter image description here

我找到了两种主要方法:

  • 使用ScrollView并使用listView.measure(0,0);代码动态设置listview高度(但不起作用,裁剪列表视图); 例如:listView有3个项目,但它的高度为2个项目(隐藏了1个项目);

enter image description here

  • 不要使用ScrollView,请使用setHeaderView(但它也不起作用,ListView没有滚动) enter image description here

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

我的一个应用程序中有一个看起来非常相似的片段.. 我遇到了和你一样的问题,我在我的案例中所做的是为每个列表视图单元格设置了固定大小的大小。并用这个逻辑来衡量高度。

不确定这是否是最佳方法,但它对我有用。

filterListView.getLayoutParams().height = (searchLabelsList.size() * (int) (43 * getScale() + 0.5f)) + (filterListView.getDividerHeight() * (searchLabelsList.size() - 1));

                            filterListView.setAdapter(searchLabelsAdapter);

在公式中,43是每个单元格的高度,当然是dp。

getScale是我编写的一种方法,用于获取当前用户手机屏幕大小的比例:

private float scale;

private float getScale() {
    if (scale == 0) {
        if (getActivity() != null) {
            scale = getActivity().getResources().getDisplayMetrics().density;
        }
    }
    return scale;
}

希望这会有所帮助,任何问题随时都可以问:)

祝你好运

答案 1 :(得分:0)

使用自定义ListView ExpandableHeightListView 此处

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.ViewGroup;
       import android.widget.ListView;

      public class ExpandableHeightListView extends ListView
    {

    boolean expanded = false;

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

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

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

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // But do not use the highest 2 bits of this integer; those are
        // reserved for the MeasureSpec mode.
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 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;
}
} 

使用

 list.setExpanded(true);

在onCreate()方法中。

答案 2 :(得分:0)

你可以试试这个:

首先:在你的xml中把所有其他视图放在ScrollView中,包括ListView

    <ScrollView
            android:id="@+id/scrollViewId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true" >             

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

                // Add your other views over here....

                    <ListView
                        android:id="@+id/list"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" /> 

            </LinearLayout>

        </ScrollView>

第二:在你的java文件中,

     Just use this custom method setListViewHeightBasedOnChildren(listview)

怎么??

      list = (ListView) findViewById(R.id.listview);
      list.setAdapter(YOUR CUSTOM ADAPTER);
      setListViewHeightBasedOnChildren(list);

这是您的自定义方法。

        public static void setListViewHeightBasedOnChildren(ListView listView) 
{
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight=0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) 
    {
        view = listAdapter.getView(i, view, listView);

        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,  
                                      LayoutParams.MATCH_PARENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

希望这能以某种方式帮助你。