ScrollView 内的 ViewPager 无法正确滚动 [英] ViewPager inside a ScrollView does not scroll correclty

查看:25
本文介绍了ScrollView 内的 ViewPager 无法正确滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面",上面有许多组件,谁的内容长于设备的高度.很好,只需将所有布局(整个页面)放在 ScrollView 中,没问题.

I have a 'page' that has a number of components on it, and who's content is longer than the height of the device. Fine, just put all of the layout (the entire page) inside a ScrollView, no problem.

其中一个组件是 ViewPager.这可以正确呈现,但是对滑动/甩动的响应没有正确执行,它很紧张并且并不总是有效.它似乎与 ScrollView 变得混淆",所以当你在一个精确的水平线上投掷时,它只能 100% 工作.

One of the components is a ViewPager. This renders correctly, but the response to a swipe/fling is not performing correctly, it is jittery and doesn't always work. It seems to be getting 'confused' with the ScrollView, so only works 100% when you fling in an exact horizontal line.

如果我删除 ScrollView,ViewPager 会完美响应.

If I remove the ScrollView, the ViewPager responds perfectly.

我已经搜索过,但没有发现这是已知缺陷.有没有其他人遇到过这种情况?

I've had a search around and have not found this as a known defect. Has anyone else experienced this?

  • 平台版本:1.6
  • 兼容性库 v4.
  • 设备:HTC Incredible S

下面是一些示例代码供您测试,注释掉 ScrollView 以查看它是否正常工作.

Below is some example code for you to test with, comment out ScrollView to see it working correctly.

活动:

package com.ss.activities;

import com.ss.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

public class PagerInsideScollViewActivity 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) {
    }
}

布局:

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

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

        </LinearLayout>

    </ScrollView>

推荐答案

进一步阅读发现滚动组件内部存在滚动组件问题.

Further reading has revealed that there are issues with scrolling components inside scrolling components.

一种解决方案是在包含的可滚动组件(在我的例子中是 ViewPager)区域禁用"ScrollView 的垂直滚动.

One solution is to 'disable' the vertical scrolling of the ScrollView on the area of the contained scrollable component, in my case a ViewPager.

此解决方案的代码在此处 详细!)

The code for this solution is detailed here (and its simply brilliant!)

这篇关于ScrollView 内的 ViewPager 无法正确滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆