应用 PageTransformer 时,ViewPager 中的 ListView 不会滚动 [英] ListView inside ViewPager won't scroll when applying a PageTransformer

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

问题描述

我有一个 ViewPager,其中的页面包含 ListView.一切正常,我的 viewPAger 和 ListViews 都按预期工作:可以在页面之间滑动,并且列表视图可以垂直滚动.

I have a ViewPager in which the pages contain ListViews. Everything works fine and my viewPAger as well as ListViews work as expected : it is possible to swipe from page to page, and the listviews scroll vertically as they should.

现在我想添加一个 PageTransformer 来平滑分页和我使用的 ZoomOutPageTransformer.

Now I wanted to add a PageTransformer to smooth out paging anbd I used the ZoomOutPageTransformer offered in the google docs.

现在我在视图之间滑动时有一个很好的动画,但列表不再可滚动.

Now I have a nice animation when swiping between views but the Lists are not scrollable anymore.

代码如下:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    viewPager = (ViewPager) view.findViewById(R.id.bookMenuPager);
    viewPager.setPageTransformer(false, new ZoomOutPageTransformer());
    pagerAdapter = new MenuPagerAdapter();
    viewPager.setAdapter(pagerAdapter);
}


class MenuPagerAdapter extends PagerAdapter{

    @Override
    public int getCount() {

        return 3; //change this as needed
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view.equals( o );
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(position == 0){
            if(!rootMenuAdded){
                viewPager.addView(rootMenucont, 0);
                rootMenuAdded = true;
            }
            return rootMenucont; 
        }else if(position == 1){
            if(!level1MenuAdded){
                viewPager.addView(level1MenuCont, 0);
                level1MenuAdded = true;
            }
            return level1MenuCont;
        }else if(position == 2){
            if(!level2MenuAdded){
                viewPager.addView(level2MenuCont, 0);
                level2MenuAdded = true;
            }
            return level2MenuCont;
        }

        //we got a problem houston
        return null;
    }
 }

和页面布局:

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

<ListView
    android:id="@+id/level1Menu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="#f02bb6"
    >
</ListView>

</RelativeLayout> 

我该怎么做才能让我的列表按预期滚动?PageTransformer 在我的 ListView 中破坏了什么,使其不再滚动?这是一个已知的错误吗?

What can I do to have my lists scrolling as expected ? What does the PageTransformer break in my ListView so that it wont scroll anymore? Is this a known bug?

感谢您的帮助:)

推荐答案

我想我已经找到了解决这个问题的方法.

I think I have found a workaround for this issue.

经过一些调查,我认为只有当你应用一个 PageTransformer 来改变 Views 的坐标时才会发生这种情况,所以 它们都在彼此之上(两个示例转换器就是这样做的).

After some investigation, I think this only happens if you apply a PageTransformer that changes the coordinates of the Views so they are all on top of each other (the two example transformers do exactly this).

当您向诸如 NEW VIEW 的 Z-index 比 OLD VIEW 低(通常向后滑动)这样的方向滑动时,这些转换器发生的情况是 OLD VIEW 处于开启状态新视图的顶部,Alpha==0,并且是后来获得幽灵"触摸的视图.

When you swipe in the direction such as the NEW VIEW has a Z-index LOWER than the OLD VIEW (normally a swipe backwards), what happens with those transformers is that the OLD VIEW is on top of the NEW VIEW, with Alpha==0, and is the one that later on gets the "ghost" touches.

不幸的是,@ngatyrauks bringToFront() 的解决方案对我不起作用(尽管它绝对应该).

Unfortunately, the solution by @ngatyrauks bringToFront() didn't work for me (although it definitely should).

但是,我已经调整了转换器,因此不可见的 views 将其可见性更改为GONE".这就是诀窍.

However, I have tweaked the transformer so invisible views are changed its visibility to "GONE". And this does the trick.

我尚未调查此可见性更改是否有任何副作用(GONE 视图将返回 null 和布局中的零等,因此这可能会破坏内部的其他内容ViewPager),但到目前为止它运行完美.

I have yet to investigate if this Visibility change has any side effects (A GONE view will return null and zeros in layout etc, so maybe this breaks other things inside ViewPager), but so far it's working perfect.

我在这里发布了一个经过调整的 DepthPageTransformer(在文档中相同),其中包含这些更改.希望对大家有帮助!

I post here a tweaked DepthPageTransformer (the same in the docs) with these changes. Hope it helps anybody!

            package com.regaliz.gui.fx;

            import android.util.Log;
            import android.view.View;
            import android.support.v4.view.ViewPager;

            public class DepthPageTransformer implements ViewPager.PageTransformer {

                private static final String TAG="DepthTransformer";
                private static float MIN_SCALE = 0.75f;

                public void transformPage(View view, float position) {
                    int pageWidth = view.getWidth();
                    Log.d(TAG, "VIew "+view+" Position: "+position);

                    if (position <= -1) { // [-Infinity,-1) ] ***

                        // RLP> I Changed to include "-1" as well: When position is -1, the view is not visible

                        // This page is way off-screen to the left.

                        view.setAlpha(0);
                        Log.d(TAG, "VIew "+view+" Position: "+position+", way left");
                        view.setVisibility(View.GONE);

                    } else if (position <= 0) { // [ (-1,0]
                        // Use the default slide transition when moving to the left page
                        view.setAlpha(1);
                        view.setTranslationX(0);
                        view.setScaleX(1);
                        view.setScaleY(1);
                        if (position==0) {
                            Log.d(TAG, "View "+view+" focused now?");
                        }

                        if (view.getVisibility()!=View.VISIBLE)
                            view.setVisibility(View.VISIBLE);

                    } else if (position <= 1) { // (0,1]

                        // Fade the page out.
                        view.setAlpha(1 - position);

                        // Counteract the default slide transition

                        // I THINK THIS IS WHAT BREAKS EVERYTHING
                        // ViewPager normally has the views one after another, but this makes all views on top

                        view.setTranslationX(pageWidth * -position);

                        // Scale the page down (between MIN_SCALE and 1)

                        float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                        view.setScaleX(scaleFactor);
                        view.setScaleY(scaleFactor);

                        if (position==1) {

                            Log.d(TAG, "View "+view+" invisible now?");
                            view.setVisibility(View.GONE);
                            // we totally hide the view. This seems to solve focus issue

                        } else {
                            if (view.getVisibility()!=View.VISIBLE)
                                view.setVisibility(View.VISIBLE);
                        }

                    } else { // (1,+Infinity]
                        // This page is way off-screen to the right.
                        view.setAlpha(0);

                        // we totally hide the view. This seems to solve focus issue
                        // I have to check for strange side-effects, but so far I found none :)

                        view.setVisibility(View.GONE);

                        Log.d(TAG, "VIew "+view+" Position: "+position+", way right");
                    }
                }
            }

这篇关于应用 PageTransformer 时,ViewPager 中的 ListView 不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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