Android ScrollView+ViewPager+PullToRefreshListView

想達到此界面的風格
而後ViewPage裏面第一個Fragment是一個瀑布流 這個瀑布流要有加載跟多php

在ScrollView中,嵌套ViewPager,在ViewPager的每頁使用Fragment而且Fragment中使用PullToRefreshListView,總體放在Fragment中banner至關於header
須要達到要求:
一、當下拉banner時,要求能夠從新加載當前ViewPager顯示頁籤中PullToRefreshListView的內容
二、當在PullToRefreshListView下拉時候,觸發ScrollView中的事件,此事件顯示「正在刷新的進度條」,此進度條放在banner上面
三、當用戶在ViewPager的區域向上滑動的時候,scrollview的中的banner要求能自動頂上去,當banner不可見的時候,上下滑動控制在viewpager中四、當用戶在ViewPager的區域向下滑動的時候,若是scrollview的中的banner不可見,則滑動區域在viewpager中下滑;若是viewpager中滑動到必定程度的時候就相應scrollview的滑動,這樣banner能夠再現出來html

我如今的問題是:
一、在ViewPager中作上滑的時候,有時header頂不上去,即滑動區域在viewpager中,
二、在ViewPager中作下滑的時候,有時heade下不來,即滑動區域在viewpager中,java

要怎麼攔截事件,不明白了請各位同窗多指教
http://www.eoeandroid.com/thread-549583-1-1.html 此貼說能夠這樣作,可是我想問的是監聽ScrollView滑動位置怎麼寫,在何時攔截tab事件,ViewPager的事件何時攔截,何時釋放,,,一大堆問題搞不定。。。。android

步奏是這樣的(ScrollView)先監聽ScrollView滑動的位置(要作下拉刷新)當過了banner下面的Tab時就把事件攔截釋放掉   讓ViewPager來獲取和分發事件   ViewPager的高度要計算到和顯示區域同樣大就是要包括上面banner的高度,由於後面banner要頂上去  回拉的時候記得監聽是否到了item第一項  要把事件所有還給ScrollView  ScrollView是不會攔截點解事件的 因此沒有問題  這個東西的難點就是把事件搞清楚   View的區域和高度要弄清楚 

我如今的代碼:
一、自定義ScrollViewExtend控件ide

package com.nd.cosplay.ui.social.home;

import javax.xml.datatype.Duration;

import com.nd.cosplay.R;
import com.nd.cosplay.common.utils.ToastUtil;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

/** 
 * 可以兼容ViewPager的ScrollView 
 * @Description: 解決了ViewPager在ScrollView中的滑動反彈問題 
 
 * @File: ScrollViewExtend.java 
 
 * @Package com.image.indicator.control 
 
 * @Author Hanyonglu 
 
 * @Date 2012-6-18 下午01:34:50 
 
 * [url=home.php?mod=space&uid=85817]@version[/url] V1.0 
 */
public class ScrollViewExtend extends ScrollView {
    public static final int MSG_LOAD = 0x123;// 進行加載信息
    public static final int MSG_FINISH = 0x124;// 完成加載信息
    LayoutInflater mLayoutInflate;// 用來得到xml的
    View mLayoutView;// 個人自定義view
    float touchLastY, touchLastX; // 最近那次觸屏的Y座標
    int touchDeltaY; // 兩次Y座標的間隔
    private LinearLayout childView;

    LinearLayout mLinearLayout;// 自定義佈局中的根佈局
    ProgressBar mProgressBar;// 自定義佈局中的ProgressBar
    TextView mTextView;// 自定義佈局中的TextView
    // 滑動距離及座標
    private float xDistance, yDistance, xLast, yLast;

    public ScrollViewExtend(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);

    }

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

    public ScrollViewExtend(Context context) {
        super(context);
        init(context);
    }

    public void init(Context context) {// 初始化變量
        // 得到佈局管理對象
        mLayoutInflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 對自定義佈局中的各個組件進行初始化
        mLayoutView = mLayoutInflate.inflate(R.layout.load_view, null);
        mLinearLayout = (LinearLayout) mLayoutView.findViewById(R.id.load_layout);
        mProgressBar = (ProgressBar) mLayoutView.findViewById(R.id.load_progress);
        mTextView = (TextView) mLayoutView.findViewById(R.id.load_text);
        touchLastY = 0.0f;
        touchDeltaY = 0;

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                // 左右滑動不攔截TouchEvent
                if (xDistance > yDistance) {
                    return false;
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        float curY = ev.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                touchLastY = curY;
                break;
            case MotionEvent.ACTION_MOVE:
                touchDeltaY = (int) (curY - touchLastY);
                // 當上拉或者下來的y超過200時候,顯示正在加載
                if (touchDeltaY > 200 || touchDeltaY < -200) {
                    // getParent().requestDisallowInterceptTouchEvent(true);
                    // 發送消息顯示正在加載
                    h.sendEmptyMessage(MSG_LOAD);
                }
                break;
            case MotionEvent.ACTION_UP:
                h.sendEmptyMessage(MSG_FINISH);
                // getParent().requestDisallowInterceptTouchEvent(false);
                break;
            case MotionEvent.ACTION_CANCEL:
                // getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.onTouchEvent(ev);
    }

    Handler h = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == MSG_LOAD) {// 更新
                ToastUtil.makeToast(getContext(), "開始加載");

                mTextView.setText("加載數據");
                mProgressBar.setVisibility(View.VISIBLE);
                mTextView.setVisibility(View.VISIBLE);
            } else if (msg.what == MSG_FINISH) {// 完成
                mProgressBar.setVisibility(View.GONE);
                mTextView.setText("更新完成");
                ToastUtil.makeToast(getContext(), "更新完成");

            }
        };
    };

}

二、自定義ScrollViewPager控件佈局

package com.nd.cosplay.ui.social.home;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ScrollViewPager extends ViewPager {
    ViewPager child_viewpager;
    float startX;

    /** 
    * @param context 
    * @param attrs 
    */
    public ScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        int action = arg0.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:// 按下
                startX = arg0.getX();
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            // 滑動,在此對裏層viewpager的第一頁和最後一頁滑動作處理
            case MotionEvent.ACTION_MOVE:
                if (startX == arg0.getX()) {
                    if (0 == getCurrentItem() || getCurrentItem() == getAdapter().getCount() - 1) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                // 裏層viewpager已是最後一頁,此時繼續向右滑(手指從右往左滑)
                else if (startX > arg0.getX()) {
                    if (getCurrentItem() == getAdapter().getCount() - 1) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                // 裏層viewpager已是第一頁,此時繼續向左滑(手指從左往右滑)
                else if (startX < arg0.getX()) {
                    if (getCurrentItem() == 0) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:// 擡起
            case MotionEvent.ACTION_CANCEL:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }

        return super.onInterceptTouchEvent(arg0);
    }

}

三、總的界面放在一個Fragment中,此xml配置文件爲ui

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

    <com.nd.cosplay.ui.social.home.ScrollViewExtend
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/mylayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
                        <!-- header爲固定的頭 -->
            <include layout="@layout/social_user_homepage_header" />

            <com.nd.cosplay.ui.social.home.ScrollViewPager
                android:id="@+id/child_viewpager"
                android:layout_width="fill_parent"
                android:layout_height="800dp" >
            </com.nd.cosplay.ui.social.home.ScrollViewPager>
        </LinearLayout>
    </com.nd.cosplay.ui.social.home.ScrollViewExtend>

</LinearLayout>
相關文章
相關標籤/搜索