在ScrollView中嵌套一个ListView发现ListView只显示一行多一点的数据,这肯定是ScrollView与ListView冲突导致的,网上看了些资料有些解决方案并不能解决这个冲突,最后在stackoverflow上看到了一个解决方法,最后采用了他这种解决方案,原文是全英文的,简单的翻译下,本人英文不是太好翻译不好的地方还希望大家能指点一二。阅读原文
怎样让ScrollView里面ListView或者GridView的高度得到扩大。
我们都知道嵌套在ScrollView里面的ListView或者GridView会使他们滑动变得困难并且不能高度不能扩大。Androdi也没有提供内置的API来处理这个问题,所以我们需要基于这两点做一些处理。
这个解决方法在stackoverflow Neil Traft的回答。
ExpandableHeightListView
package net.londatiga.android.widget;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;
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;
}
ExpandableHeightGridView
package net.londatiga.android.widget;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
import android.content.Context;
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(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;
}
}
使用方法:
//ListView
ExpandableHeightListView listView = new ExpandableHeightListView(this);
listView.setAdapter(adapter);
listView.setExpanded(true);
//GridView
ExpandableHeightGridView gridView = new ExpandableHeightGridView(this);
gridView.setNumColumns(4);
gridView.setAdapter(adapter);
gridView.setExpanded(true);
正如你看到的,使嵌套在ScrollViewView里面的ListView和GridVew的高度得到扩大,仅仅只需要调用setExapanded(true)方法就行。