ViewPager在滚动型不会垂直滚动

ViewPager在滚动型不会垂直滚动

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

问题描述

我有一个的布局的,有一个 ViewPager 里面的自定义的滚动型和该ViewPager不会垂直滚动。自定义滚动型是用来固定可怕的标签与滚动型刷卡。

是的,有足够的内容为它滚动,我已经有和没有底部的按钮尝试。

我使用的有3个标签的动作条和当前code可让刷卡左/右就好了。

我的目标是有一对夫妇的行动项目,如保存和取消,并在底部的动作条和按钮之间的部分是ViewPager将垂直滚动的底部栏。

我试过太多的组合,我现在发送这封情书给你。有任何想法吗?任何帮助是pciated AP $ P $,并随时在需要时要求提供更多的细节。

的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的LinearLayout
        机器人:ID =@ + ID /页脚
        风格=@安卓风格/ Holo.ButtonBar
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:方向=横向>        <按钮
            机器人:ID =@ + ID / saveButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文本=保存/>        <按钮
            机器人:ID =@ + ID / cancelButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文字=取消/>
    < / LinearLayout中>    < com.blah.hootiehoo.ViewPagerCompatScrollView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_above =@ ID /页脚
        机器人:fillViewport =真正的>        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:方向=垂直
            机器人:scrollbarAlwaysDrawVerticalTrack =真
            机器人:滚动条=垂直>            < android.support.v4.view.ViewPager
                机器人:ID =@ + ID /寻呼机
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:scrollbarAlwaysDrawVerticalTrack =真
                机器人:滚动条=垂直>
            < /android.support.v4.view.ViewPager>
        < / LinearLayout中>
    < /com.blah.hootiehoo.ViewPagerCompatScrollView>< / RelativeLayout的>

MainActivity.java

  ViewPager mViewPager;
TabsAdapter mTab​​sAdapter;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);
    mViewPager =(ViewPager)findViewById(R.id.pager);    制表符//设置操作栏
    动作条动作条= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(真);
    actionBar.setDisplayHomeAsUpEnabled(真);    mTabsAdapter =新TabsAdapter(这一点,mViewPager);    TAB键的话= actionBar.newTab();
    //标签设置
    mTabsAdapter.addTab(/ *** ***的东西/);    //增加2个标签,等
}

ViewPagerCompatScrollView.java

 公共类ViewPagerCompatScrollView扩展滚动型{
    私人浮动xDistance,yDistance,lastX,lastY;    公共ViewPagerCompatScrollView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    @覆盖
    公共布尔onInterceptTouchEvent(MotionEvent EV){
        开关(ev.getAction()){
            案例MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0F;
                lastX = ev.getX();
                lastY = ev.getY();
                打破;
            案例MotionEvent.ACTION_MOVE:
                最终浮动curX = ev.getX();
                最终浮动CURY = ev.getY();
                xDistance + = Math.abs(curX - lastX);
                yDistance + = Math.abs(CURY - lastY);
                lastX = curX;
                lastY = CURY;
                如果(xDistance> yDistance)
                    返回false;
        }        返回super.onInterceptTouchEvent(EV);
    }
}


解决方案

根据先前的评论:

是不是换一个选项滚动型 ViewPager 身边?从以往的经验我知道一个滚动型 ViewPager 工作没有任何问题。所以基本上,而不是包裹 ViewPager 滚动型,给予每页一个滚动型为根。

欣闻为你工作。

I have a layout that has a ViewPager inside of a custom ScrollView and the ViewPager won't scroll vertically. The custom ScrollView is used to fix the dreaded tab swiping with a ScrollView.

Yes, there is enough content for it to scroll and I've tried with and without the bottom buttons.

I'm using the ActionBar that has 3 tabs and the current code allows swiping left / right just fine.

My goal is to have the bottom bar for a couple action items such as save and cancel and the part between the ActionBar and the buttons at the bottom be the ViewPager that will scroll vertically.

I've tried too many combinations and am now sending this love letter to you. Any ideas? Any help is appreciated and feel free to ask for additional details if needed.

main.xml

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

    <LinearLayout
        android:id="@+id/footer"
        style="@android:style/Holo.ButtonBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/saveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save" />

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>

    <com.blah.hootiehoo.ViewPagerCompatScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:fillViewport="true" >

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

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                android:scrollbars="vertical" >
            </android.support.v4.view.ViewPager>
        </LinearLayout>
    </com.blah.hootiehoo.ViewPagerCompatScrollView>

</RelativeLayout>

MainActivity.java

ViewPager mViewPager;
TabsAdapter mTabsAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mViewPager = (ViewPager)findViewById(R.id.pager);

    // setup action bar for tabs
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    Tab tab = actionBar.newTab();
    // tab setup
    mTabsAdapter.addTab(/*** stuff ***/);

    // add 2 more tabs, etc
}

ViewPagerCompatScrollView.java Got from here, at bottom

public class ViewPagerCompatScrollView extends ScrollView {
    private float xDistance, yDistance, lastX, lastY;

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - lastX);
                yDistance += Math.abs(curY - lastY);
                lastX = curX;
                lastY = curY;
                if(xDistance > yDistance)
                    return false;
        }

        return super.onInterceptTouchEvent(ev);
    }
}
解决方案

As per earlier comment:

Is it an option to swap the ScrollView and ViewPager around? From experience I know that a ScrollView inside a ViewPager works without any problems. So basically instead of wrapping the ViewPager by a ScrollView, give every 'page' a ScrollView as root.

Glad to hear that worked for you.

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

09-04 22:21