是否可以在ScrollView中使用ViewPager?

时间:2011-09-11 21:26:45

标签: android scrollview android-viewpager

我正在尝试使用ViewPager内的ScrollView,但ViewPager未显示。如果我删除了ScrollView,则ViewPager显示正常。

我创建了一个简单的测试项目,其中包含以下内容:

main.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <android.support.v4.view.ViewPager
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:id="@+id/viewpager" />

    </ScrollView>
</LinearLayout>

活动类:

public class ScrollViewWithViewPagerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
        vp.setAdapter(new MyPagerAdapter(this));
    }
}

class MyPagerAdapter extends PagerAdapter {

    private Context ctx;

    public MyPagerAdapter(Context context) {
        ctx = context;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object instantiateItem(View collection, int position) {

        TextView tv =  new TextView(ctx);
        tv.setTextSize(50);
        tv.setTextColor(Color.WHITE);
        tv.setText("SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " +
                "SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " +
                "SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " +
                "SMILE DUDE, SMILE DUDE, SMILE DUDE");

        ((ViewPager) collection).addView(tv);

        return tv;

    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
         ((ViewPager) collection).removeView((View) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

    @Override
    public void startUpdate(View arg0) {
    }

    @Override
    public void finishUpdate(View arg0) {
    }
}

感谢您的时间。

5 个答案:

答案 0 :(得分:110)

我已经明白了;

我需要将android:fillViewport="true"添加到ScrollView元素。

答案 1 :(得分:19)

是的,可以在scrollview中使用viewpager。

使用以下代码,您将实现目标,我也在代码中也这样做。

 public class WrapContentHeightViewPager extends ViewPager {

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = 0;

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);

            child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

            int h = child.getMeasuredHeight();

            if (h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
    int dragthreshold = 30;

    int downX = 0;

    int downY = 0;

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            downX = (int) event.getRawX();

            downY = (int) event.getRawY();

            break;

        case MotionEvent.ACTION_MOVE:
            int distanceX = Math.abs((int) event.getRawX() - downX);

            int distanceY = Math.abs((int) event.getRawY() - downY);

            if (distanceY > distanceX && distanceY > dragthreshold) {
                mViewPager.getParent().requestDisallowInterceptTouchEvent(false);

                mScrollView.getParent().requestDisallowInterceptTouchEvent(true);
            } else if (distanceX > distanceY && distanceX > dragthreshold) {
                mViewPager.getParent().requestDisallowInterceptTouchEvent(true);

                mScrollView.getParent().requestDisallowInterceptTouchEvent(false);
            }

            break;
        case MotionEvent.ACTION_UP:
            mScrollView.getParent().requestDisallowInterceptTouchEvent(false);

            mViewPager.getParent().requestDisallowInterceptTouchEvent(false);

            break;
    }

    return false;
 }

希望这有助于解决ScrollView中嵌套ViewPager的滚动问题。

答案 2 :(得分:3)

此解决方案适用于我,具有动态内容。

http://www.henning.ms/2013/09/09/viewpager-that-simply-dont-measure-up/

在xml ScrollView放置android:fillViewport=trueandroid:layout_height=wrap_content

在自定义ViewPager中也放android:layout_height=wrap_content

答案 3 :(得分:0)

如果有人遇到相同的问题,当我使用下面的类时,它可以正常工作。

class CustomWrapContentViewPager(cont: Context, attr: AttributeSet?) : ViewPager(cont,attr) {

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    var height = 0
    for (i in 0 until childCount) {
        val child: View = getChildAt(i)
        child.measure(
            widthMeasureSpec,
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        )
        val h: Int = child.measuredHeight
        if (h > height) height = h
    }
    val heightMeasure = MeasureSpec.makeMeasureSpec(
        height,
        MeasureSpec.EXACTLY
    )
    super.onMeasure(widthMeasureSpec, heightMeasure)
    }
}

答案 4 :(得分:-8)

你为什么要这样做?如果需要,更自然的方法是将每个页面包装在ScrollView中。