ScrollView 中的 ViewPager - 垂直滚动不起作用

ViewPager inside ScrollView - vertical scroll doesn#39;t work(ScrollView 中的 ViewPager - 垂直滚动不起作用)
本文介绍了ScrollView 中的 ViewPager - 垂直滚动不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这个 ScrollView 和一个孩子 - 一个 LinearLayout 有两个孩子:一个 TextView 和一个 ViewPager.ViewPager 包含许多元素的布局,这就是为什么我需要垂直滚动的能力.只有 ViewPager 中的页面可以水平滚动(即:我只想在 ViewPager 中水平滑动).那一个 TextView 不能水平滚动,但应该与我的 ViewPager 一起滚动.

So, I have this ScrollView with one child - a LinearLayout that has two children: a TextView and a ViewPager. ViewPager contains layout with many elements, that's why i need the ability to scroll vertically. Only pages in ViewPager may be scrolled horizontally (that is: I'd like to swipe horizontally only within the ViewPager). That one TextView must not scroll horizontally but should scroll together with my ViewPager.

简单吗?没有.

我在 StackOverflow 上看到了极其相似的问题(这里、这里和这里和这里).建议的解决方案都不适合我:(

I've seen extremely similar issues at StackOverflow popping up (here, here and here and here). None of the suggested solutions work for me :(

我看到的是 this <- 我可爱的 ​​UI :),但是我不能垂直滚动:(

What I see is this <- my sweet UI :), However I cannot scroll vertically :(

在 ViewPager 中嵌入 ScrollViews 不是一种选择 - UI 设计禁止这样做.

Embedding ScrollViews inside ViewPager is not an option - desing of the UI forbids this.

也许这与我以编程方式填充视图寻呼机中的每个页面有关?嗯……

Maybe it's something with my programmatically filling each page in view pager? Hmmm...

任何建议将不胜感激.

我的代码:

activity_main.xml:

activity_main.xml:

<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:background="@android:color/white"
                android:fillViewport="true" >

                <LinearLayout
                    android:id="@+id/linearLayoutGeneral"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                    android:id="@+id/tvText"
                    android:layout_width="fill_parent"
                    android:layout_height="200dp"
                    android:text="Test text" />

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


                </LinearLayout>
</ScrollView>

ViewPager 中的每个页面都有这样的布局:

each page in ViewPager has this layout:

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

    <LinearLayout
        android:id="@+id/layoutData"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

此类页面中单个元素的布局:

single element's layout in such a page:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

单页Fragment也很简单:

A single page Fragment is also very simple:

public class DayFragment extends Fragment {

    private static final String TAG = DayFragment.class.getSimpleName();

    public String tag;

    LinearLayout data;

    View mView;

    final int ROWS_NUM = 60;

    public DayFragment() {

    }

    /**
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     *      android.view.ViewGroup, android.os.Bundle)
     */
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist. The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed. Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        mView = (LinearLayout) inflater.inflate(R.layout.day, container, false);

        setUpControls();

        generateData();

        String text = getArguments().getString("text");
        Log.d(TAG, "creating view with text: " + text);

        return mView;
    }

    private void setUpControls() {
        data = (LinearLayout) mView.findViewById(R.id.layoutData);
    }

    private void generateData() {
        for (int i = 0; i < ROWS_NUM; i++) {
            View v = createRow(i);
            data.addView(v);
        }
    }

    private View createRow(int num) {
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.row, null);

        TextView tv = (TextView) v.findViewById(R.id.textView);
        tv.setText("Data nr: " + num);

        return v;
    }

    public static DayFragment newInstance(String text) {
        Log.d(TAG, "newInstance with text: " + text);
        DayFragment f = new DayFragment();

        f.tag = text;

        // Supply text input as an argument.
        Bundle args = new Bundle();
        args.putString("text", text);
        f.setArguments(args);

        return f;
    }

}

推荐答案

我遇到了 ViewPager 行为怪异的问题,我发现这是因为有时 ScrollView 重新获得焦点,ViewPager 然后失去焦点.如果您在 ScrollView 中有一个 ViewPager 并且您希望它在您触摸它时始终保持焦点,并且 ScrollView 永远不会获得焦点,设置 requestDisallowInterceptTouchEvent 可以做到这一点.这有帮助吗?

I had the problem that ViewPager behaved weird and I found out the reason for that it is because sometimes the ScrollView regains focus and the ViewPager then loses focus. If you have a ViewPager in a ScrollView and you want it always to stay in focus when you touch it and the ScrollView never getting a focus, setting the requestDisallowInterceptTouchEvent does that. Did that help?

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

这篇关于ScrollView 中的 ViewPager - 垂直滚动不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)