ViewPager内的滚动型不滚动correclty [英] ViewPager inside a ScrollView does not scroll correclty

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

问题描述

我有一个'页面',有一个号码在其上的组件,以及谁的含量超过了装置的高度。很好,只是把所有的布局(整页)在滚动型,没问题。

其中一个组件是 ViewPager 。这使得正确,但响应轻扫/一扔没有执行正确的,这是紧张不安,并好好尝试一下总是工作。它似乎越来越糊涂与滚动型,所以只能100%,当你在一个确切的水平线一扔。

如果我删除滚动型,该ViewPager响应完美。

我有一个四处搜寻,并没有发现这是一个已知缺陷。 任何人都经历呢?

平台版本:1.6 兼容库V4。
设备:HTC Incredible S的

任何帮助将是AP preciated。

下面是一些例子$ C $下你来测试,注释掉滚动型来看看它的工作correclty。

活动:

 包com.ss.activities;

进口com.ss.R;

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

公共类PagerInsideScollViewActivity延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        ViewPager VP =(ViewPager)findViewById(R.id.viewpager);
        vp.setAdapter(新MyPagerAdapter(本));
    }
}

类MyPagerAdapter扩展PagerAdapter {

    私人上下文CTX;

    公共MyPagerAdapter(上下文的背景下){
        CTX =背景;
    }

    @覆盖
    公众诠释getCount将(){
        返回2;
    }

    @覆盖
    公共对象instantiateItem(查看收集,INT位置){

        TextView的电视=新的TextView(CTX);
        tv.setTextSize(50);
        tv.setTextColor(Color.WHITE);
        tv.setText(SMILE DUDE,微笑DUDE,微笑DUDE,微笑DUDE,微笑DUDE,+
                SMILE DUDE,微笑DUDE,微笑DUDE,微笑DUDE,微笑DUDE,+
                SMILE DUDE,微笑DUDE,微笑DUDE,微笑DUDE,微笑DUDE,+
                SMILE DUDE,微笑DUDE,微笑DUDE);

        ((ViewPager)集合).addView(TV);

        返回电视;

    }

    @覆盖
    公共无效destroyItem(查看收集,INT位置,对象视图){
         ((ViewPager)集合).removeView((视图));
    }

    @覆盖
    公共布尔isViewFromObject(查看视图,Object对象){
        返回查看==对象;
    }

    @覆盖
    公共Parcelable saveState和(){
        返回null;
    }

    @覆盖
    公共无效restoreState(Parcelable为arg0,ClassLoader的ARG1){
    }

    @覆盖
    公共无效startUpdate(查看为arg0){
    }

    @覆盖
    公共无效finishUpdate(查看为arg0){
    }
}
 

布局:

 < XML版本=1.0编码=UTF-8&GT?;
    <滚动型
         的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>

        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:方向=垂直>

            < android.support.v4.view.ViewPager
                机器人:ID =@ + ID / viewpager
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =300dp/>

        < / LinearLayout中>

    < /滚动型>
 

解决方案

进一步阅读透露,有问题的滚动组件中滚动的组件。

解决方案之一是禁用的垂直滚动型对含有滚动元件的区域滚动,在我的情况下,ViewPager。

在code这个解决方案详见<一个href="http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling">here (和它只是辉煌!)

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.

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 doens't always work. It seems to be getting 'confused' with the ScrollView, so only works 100% when you fling in an exact horizontal line.

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?

Platform Version: 1.6 Compatibility Library v4.
Device: HTC Incredible S

Any help would be appreciated.

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

Activity:

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) {
    }
}

Layout:

<?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.

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!)

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

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