scrollview 与 listView 的显示不全问题
生活随笔
收集整理的這篇文章主要介紹了
scrollview 与 listView 的显示不全问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用兩個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:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="wrap_content"> <RelativeLayout android:layout_width="wrap_content"android:layout_height="wrap_content"tools:context=".MainActivity" ><com.gaojinshan.MyGridViewandroid:id="@+id/gv_app1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:horizontalSpacing="0dp"android:gravity="center"android:numColumns="4"android:stretchMode="columnWidth" android:verticalSpacing="0dip"/><com.gaojinshan.MyGridViewandroid:id="@+id/gv_app2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/gv_app1"android:horizontalSpacing="0dp"android:gravity="center"android:numColumns="2"android:stretchMode="columnWidth" android:verticalSpacing="0dip"android:scrollbars="none"/> </RelativeLayout> </ScrollView>?
在GridView所在頁的其他控件的XML里配置上focusableInTouchMode=true,如下所示:
?
?
?
在點擊切換到該頁面時,使用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
總結
以上是生活随笔為你收集整理的scrollview 与 listView 的显示不全问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++ 可视化界面_这些算法可视化网站助
- 下一篇: 2. with check option