中原郑州网站建设,免费下载ppt的网站,wordpress主题 笑话,卖东西的网站怎么做使用两个GridView#xff0c;两个GridView一起上下滚动#xff1b;如果直接将两个GridView添加到同一个界面上#xff0c;它们是各自滚动的。因此#xff0c;我考虑使用SrollView#xff0c;将它们包装一下#xff01;但这样做会提示如下信息#xff1a;The vertically … 使用两个GridView两个GridView一起上下滚动如果直接将两个GridView添加到同一个界面上它们是各自滚动的。因此我考虑使用SrollView将它们包装一下但这样做会提示如下信息The vertically scrolling ScrollView should not contain another vertically scrolling widget (GridView)并且GridView的界面也显示不全只显示了一部分。 网上搜了一下使用下面这个方法效果挺好的能满足我的需求从GridView派生出一个自定义的类MyGridView重载其测量方法onMeasure /*** Type: MyGridView* 让GridView可以做ScrollView的子控件但尺寸不会减小*/
public class MyGridView extends GridView {public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } 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); }
} 在XML中将原来使用系统的GridView替换成自定义的MyGridView即可 ScrollView xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthfill_parentandroid:layout_heightwrap_content
RelativeLayout android:layout_widthwrap_contentandroid:layout_heightwrap_contenttools:context.MainActivity com.gaojinshan.MyGridViewandroid:idid/gv_app1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:horizontalSpacing0dpandroid:gravitycenterandroid:numColumns4android:stretchModecolumnWidth android:verticalSpacing0dip/com.gaojinshan.MyGridViewandroid:idid/gv_app2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_belowid/gv_app1android:horizontalSpacing0dpandroid:gravitycenterandroid:numColumns2android:stretchModecolumnWidth android:verticalSpacing0dipandroid:scrollbarsnone/
/RelativeLayout
/ScrollView 在GridView所在页的其他控件的XML里配置上focusableInTouchModetrue如下所示 android:focusabletrue android:focusableInTouchModetrue 在点击切换到该页面时使用ScrollView的smoothScrollTo(0, 0)方法将滚动条置顶 public void onClick(View v) {mViewPager.setCurrentItem(item);View view mViewPager.getChildAt(item);if (view ! null) {((ScrollView) view).smoothScrollTo(0, 0);}} 转载于:https://www.cnblogs.com/regalys168/p/4848270.html