2012-08-29 108 views
0

我的xml样式我在ScrollView中使用了android.support.v4.view.ViewPager。问题是我没有得到一个滚动条。当我从一个页面滑动到另一个页面时,ViewPager本身的行为也很奇怪。ViewPager in ScrollView no Scrollbar

将ScrollView设置为固定高度,例如1200dip有助于滚动,但不显示ViewPager。 这里是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fadeScrollbars="true" 
    android:fillViewport="true" 
    android:scrollbars="vertical" > 


    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 



     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/menu" 
      /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </android.support.v4.view.ViewPager> 
    </RelativeLayout> 

</ScrollView> 

在此先感谢

+0

http://stackoverflow.com/questions/2646028/android-horizo​​ntalscrollview-within-scrollview-touch-handling找到了解决办法 – user1324936

+1

邮报回答你的问题,并验证自己。我失去了时间到这个页面,认为它没有解决。谢谢。 – shkschneider

回答

1

你没有得到一个滚动条的原因是因为你的ViewPager(和RelativeLayout)都有它的高度设置为match_parent而不是wrap_content。这会导致它与您的ScrollView的尺寸相匹配,这种方式首先会破坏ScrollView的目的。您的ScrollView的内容应该比您的ScrollView本身更高/更高。

总之,您应该将RelativeLayout的高度和ViewPager的高度设置为wrap_content。此外,您没有设置要让RelativeLayout的孩子出现的顺序/位置;你可能想改变它(或者改用LinearLayout)。例如:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fadeScrollbars="true" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/menu" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

</ScrollView> 
+0

我复制了你的xml,但它也没有滚动。我认为这是ViewView在scrollView中的根本问题? – user1324936

+0

是的,就像你在上面提供的链接中的回答说的那样,你可以通过扩展ScrollView来覆盖它的onInterceptTouchEvent(...)方法来解决这个问题。您应该为自己的帖子提供答案并接受它。 – Reinier

0

我有同样的问题,即ViewPager行为怪异,我发现了,这是因为somethimes的原因了滚动恢复焦点然后ViewPager失去了重点。如果你在ScrollView中有一个ViewPager,并且你希望它始终保持焦点,那么当你触摸它并且ScrollView永远不会获得焦点时,就是这样做的。

mViewPager= (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(adapter); 
    mViewPager.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      mViewPager.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    });