閱讀807 返回首頁    go 技術社區[雲棲]


Android開發-ScrollView嵌套GridView的解決辦法

前些日子在開發中用到了需要ScrollView嵌套GridView的情況,由於這兩款控件都自帶滾動條,當他們碰到一起的時候便會出問題,即GridView會顯示不全。

解決辦法,自定義一個GridView控件

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

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

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

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

int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

該自定義控件隻是重寫了GridView的onMeasure方法,使其不會出現滾動條,ScrollView嵌套ListView也是同樣的道理,不再贅述。

XML布局代碼
<ScrollView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:>
<com.yourclass.MyGridView xmlns:andro
android: android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="auto_fit"
android:horizontalSpacing="1dip" android:verticalSpacing="1dip"
android:columnWidth="150dip" android:stretchMode="columnWidth"
android:gravity="center">

</com.yourclass.MyGridView>
</ScrollView>

Java調用代碼
MyGridView gridview = (MyGridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));

最後更新:2017-04-02 17:28:38

  上一篇:go linux 修改係統時間命令
  下一篇:go Java中的異常對程序效率有無影響