2 ViewPager 在 Android 2.3 中不滚动 [英] 2 ViewPager not scrolling in Android 2.3

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

问题描述

我在我的应用中使用导航抽屉,其中包含一些如下图所示的片段.

I am using Navigation Drawer in my app, that contains some Fragments just like in the below picture.

每个片段包含另一个 ViewPager,它是一个 ImageSlider,下面是一个 Listview,在顶部我使用 SwipeRefreshLayout.我的问题是图像滑块在 Android 3.0 或更高版本的设备上运行良好,但向左或向右滑动在 2.3 及更低版本的设备上不起作用,而是调用父 ViewPager 的滑动,即它导航片段.为此,我使用支持版本 4 库来支持低于 3.0 的设备.除了那一个之外,所有功能在 2.3 设备上都运行良好.我已经用谷歌搜索了它,但我没有在任何地方找到任何帮助.所以为了让它滚动我应该怎么做,任何想法/帮助将不胜感激.

Every Fragment Contains another ViewPager that is an ImageSlider, and below that is a Listview and at the top I am using the SwipeRefreshLayout. My problem is the image slider works well on devices that has Android version 3.0 or higher but the swipe left or right doesn't works on devices 2.3 and lower, instead it invokes the Parent ViewPager's swipe that is it navigates the fragment. I am using support Version 4 library for this purpose to support devices lower than 3.0. All functions works quite well on 2.3 devices except that one. I have googled it but I haven't found any help anywhere. So to make it scroll what should I do for this, any idea/help will be highly appreciated.

推荐答案

你可以使用这个 ViewPager 作为你的父 ViewPager.这允许子 ViewPager 滚动.

You can use this ViewPager as your parent ViewPager. This allows the child ViewPager to scroll.

public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context) {
        super(context);
    }


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


    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        try {
            //Handle the issue only in lower versions of android
            if (v != this && v instanceof ViewPager && CJRAppCommonUtility.getOSVersion() < android.os.Build.VERSION_CODES.HONEYCOMB) {
                ViewPager viewPager = (ViewPager) v;
                int currentPage = viewPager.getCurrentItem();
                int size = viewPager.getAdapter().getCount();
                //if ViewPager has reached its end and if user tries to swipe left allow the parent to scroll
                if (currentPage == (size - 1) && dx < 0) {
                    return false;
                }
                //if ViewPager has reached its start and if user tries to swipe right allow the parent to scroll
                else if (currentPage == 0 && dx > 0) {
                    return false;
                } 
                //Allow the child to scroll hence blocking parent scroll
                else {
                    return true;
                }
            } 
            return super.canScroll(v, checkV, dx, x, y);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Android 开发者网站对在 Viewgroup 中处理触摸事件有很好的解释.你可以在这里参考:http://developer.android.com/training/gestures/viewgroup.html

Android developers site has a nice explanation about handling touch events in a Viewgroup. You can refer it here: http://developer.android.com/training/gestures/viewgroup.html

希望能帮到你!!

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

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