SwipeRefreshLayout - 当将android版本切换到v21时,Vertical Scroll不起作用

时间:2014-10-29 09:38:20

标签: android android-viewpager swiperefreshlayout

当用户在寻呼机内滚动时,MyViewPager必须垂直滚动。 这是在Android版本19中工作,现在我已经将Android版本更新为v21,我得到的是翻页器上的刷新球。我可以让球看不见但是传感器仍然没有滚动。

这是我的main-content.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
    android:layout_below="@id/pull_text_layout"
    android:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="205dp">
<com.pager.MyViewPager
           android:id="@+id/pager"
           android:layout_marginTop="5dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="@android:color/white">
       </com.pager.MyViewPager>
</android.support.v4.widget.SwipeRefreshLayout>

这是MyActivity.java

// Pager
    mPager = (MyViewPager) findViewById(R.id.pager);
    mPagerAdapter = new CardsViewPagerAdapter(this, cardResourceIds, cardBackResourceIds);
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(final int position) {
            updateToCard(position);
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(Delays.DELAY_BALANCE);
                        mPagerAdapter.updateBalance(position, cardBalances[mRandom.nextInt(2)]);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
    mPager.setListener(new MyViewPager.ScrollListener() {

        @Override
        public void onScrollStarted() {
            mRefresh.setEnabled(false);
        }

        @Override
        public void onScrollFinished() {
            mRefresh.setEnabled(true);
        }
    });

    mRefresh = (SwipeRefreshLayout) findViewById(R.id.refresh);
    mRefresh.setColorScheme(android.R.color.transparent, android.R.color.transparent,
                                android.R.color.transparent, android.R.color.transparent);
    mRefresh.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mRefresh.setRefreshing(false);
            addPaymentFragment(mPager.getCurrentItem() != 3);
        }
    });

这是MyViewPager

public class MyViewPager extends ViewPager {

    private boolean paginationEnabled = true,
                        scrolling = false;
    private int scrolled;
    private ScrollListener listener;

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

    public MyViewPager(Context context, AttributeSet attr) {
        super(context, attr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (!paginationEnabled) {
            return false;
        } else {
            int action = e.getAction();
            if (scrolling && (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL)) {
                scrolling = false;
                if (listener != null) {
                    listener.onScrollFinished();
                }
                if(scrolling)
                {}

            }
            return super.onInterceptTouchEvent(e);
        }
    }

    public void setPaginationEnabled(boolean paginationEnabled) {
        this.paginationEnabled = paginationEnabled;
    }

    public void setListener(ScrollListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        scrolling = true;
        if (listener != null) {
            listener.onScrollStarted();
        }
    }

    public interface ScrollListener {
        public void onScrollStarted();
        public void onScrollFinished();
    }

}

0 个答案:

没有答案