现在的位置: 首页 > 综合 > 正文

ListView/GridView与ScrollView Scrollbar冲突问题。

2013年02月08日 ⁄ 综合 ⁄ 共 1282字 ⁄ 字号 评论关闭

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

public class MyGridView extends GridView{

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }

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

    

}

也可以动态设置ListView的高度来满足自己的需求。

备注:

scrollview和listview嵌套会造成不能滚动的问题,listview只会显示一行多一点

解决方法:在scrollview中添加属性:android:fillviewport="true",这就可以让listview显示全屏了。

scrollview滚动是因为高度超出了手机屏幕的高度,然而listview的高度是不确定的,所以导致scrollview无法滚动。

如果listview的高度超过了屏幕高度,又想要scrollview能够滚动,这需要在设置adapter后重现计算listview的高度

public void setListViewHeightBasedOnChildren(ListView listView) {
ImageAdapter listAdapter = (ImageAdapter) listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int width = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
width = listItem.getMeasuredWidth();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
params.width = width;
listView.setLayoutParams(params);
}

抱歉!评论已关闭.