ViewPager内滚动型 - 垂直滚动不工作 [英] ViewPager inside ScrollView - vertical scroll doesn't work

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

问题描述

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

简单?没有。

我在StackOverflow上看到的极其相似的问题雨后春笋般冒出来(<一个href="http://stackoverflow.com/questions/8381697/viewpager-inside-a-scrollview-does-not-scroll-correclty">here, <一href="http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling">here和<一href="http://stackoverflow.com/questions/10865508/viewpager-in-scrollview-wont-scroll-vertically">here和<一href="http://stackoverflow.com/questions/7381360/is-it-possible-to-have-a-viewpager-inside-of-a-scrollview">here). 没有一个建议的解决方案为我工作:(

我看到的是&LT; - 我亲爱的用户界面:),但是我不能垂直滚动:(

嵌入ScrollViews内ViewPager是不是一种选择 - 用户界面的德兴禁止这种

也许这件事情与我的编程方式填充每个页面鉴于寻呼机?嗯...

任何建议将是AP preciated。

我的code:

activity_main.xml:

 &LT;滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android

                机器人:ID =@ + ID /滚动视图
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:背景=@机器人:彩色/白
                机器人:fillViewport =真正的&GT;

                &LT;的LinearLayout
                    机器人:ID =@ + ID / linearLayoutGeneral
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:方向=垂直&GT;

                    &LT;的TextView
                    机器人:ID =@ + ID / tvText
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =200dp
                    机器人:文本=测试文本/&GT;

                &LT; android.support.v4.view.ViewPager
                            机器人:ID =@ +安卓ID / viewpager
                            机器人:layout_width =FILL_PARENT
                            机器人:layout_height =FILL_PARENT&GT;
                &LT; /android.support.v4.view.ViewPager>


                &LT; / LinearLayout中&GT;
&LT; /滚动型&GT;
 

在ViewPager每一页都有这样的布局:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直&GT;

    &LT;的LinearLayout
        机器人:ID =@ + ID /布局数据
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直&GT;
    &LT; / LinearLayout中&GT;

&LT; / LinearLayout中&GT;
 

在这样的网页单元素的布局:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =40dp
    机器人:重力=中心
    机器人:方向=垂直
    机器人:背景=@机器人:彩色/白&GT;

    &LT;的TextView
        机器人:ID =@ + ID / TextView的
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=大文本
        机器人:背景=@机器人:彩色/黑白
        机器人:文字颜色=@机器人:彩色/白
        机器人:textAppearance =:/&gt;中的Andr​​oid ATTR / textAppearanceLarge?

&LT; / LinearLayout中&GT;
 

有一个单一的页面片段也很简单:

 公共类DayFragment扩展片段{

    私有静态最后字符串变量= DayFragment.class.getSimpleName();

    公共字符串标记;

    LinearLayout中的数据;

    查看MView的;

    最终诠释ROWS_NUM = 60;

    公共DayFragment(){

    }

    / **
     *(非Javadoc中)
     *
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     * android.view.ViewGroup,android.os.Bundle)
     * /
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        如果(集装箱== NULL){
            //我们有不同的布局,并在他们这一个
            //片段的含框架不存在。片段
            //仍然可以从它的保存的状态创建的,但是有
            //没有理由去尝试创建视图,因为它的层次结构
            //将不被显示。请注意,这是没有必要 - 我们可以
            //只需运行code以下,我们将创建并返回
            //视图层次;它只是永远不会被使用。
            返回null;
        }

        MVIEW =(的LinearLayout)inflater.inflate(R.layout.day,集装箱,假);

        setUpControls();

        generateData();

        。字符串文本= getArguments()的getString(文字);
        Log.d(TAG,创建视图的文字​​:+文字);

        返回MView的;
    }

    私人无效setUpControls(){
        数据=(的LinearLayout)mView.findViewById(R.id.layoutData);
    }

    私人无效generateData(){
        的for(int i = 0; I&LT; ROWS_NUM;我++){
            视图v = createRow(ⅰ);
            data.addView(五);
        }
    }

    私人查看createRow(INT NUM){
        LayoutInflater充气=(LayoutInflater)getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        视图V = inflater.inflate(R.layout.row,NULL);

        TextView的电视=(TextView中)v.findViewById(R.id.textView);
        tv.setText(数据NR:+ NUM);

        返回伏;
    }

    公共静态DayFragment的newInstance(字符串文本){
        Log.d(TAG的newInstance的文字:+文字);
        DayFragment F =新DayFragment();

        f.tag =文本;

        //供应文本输入作为参数。
        捆绑的args =新包();
        args.putString(文字,文本);
        f.setArguments(参数);

        返回F;
    }

}
 

解决方案

我有这样的ViewPager行为怪异的问题,我发现了,这是因为somethimes的滚动型重新获得焦点和ViewPager然后失去焦点的原因

。如果你在一个滚动型中ViewPager,你希望它永远留在焦点,当你触摸它和滚动型从未得到一个焦点,设置requestDisallowInterceptTouchEvent做到这一点。这是否帮助?

  mViewPager =(ViewPager)findViewById(R.id.pager);
    mViewPager.setAdapter(适配器);
    mViewPager.setOnTouchListener(新View.OnTouchListener(){

        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            。mViewPager.getParent()requestDisallowInterceptTouchEvent(真正的);
            返回false;
        }
    });
 

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.

Simple? No.

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

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

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

Any suggestions would be appreciated.

My code:

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>

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>

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

}

解决方案

I had the problem that ViewPager behaved weird and I found out the reason for that it is because somethimes 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;
        }
    });

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

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