1

我正在使用 JakeWharton 的 ViewPageIndicator。我希望寻呼机和指示器的高度跨越 300dp 左右,这样我就可以在它下面有更多内容并且它可以滚动。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:orientation="vertical" >

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:paddingTop="2dp" />

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/indicator"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:padding="10dip" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

事情完美。但我无法滑动 Viewpagper,它真的卡住了,不会滑到下一个。有谁知道如何让它在有限的高度工作?

******编辑******

尝试使用Here的解决方案。我不太确定它是否是子类滚动视图的正确方法。

public class PlaceDetailsFragment extends SherlockFragment {
    PlaceSlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    public static final String TAG = "detailsFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_place_details,
                container, false);

        mAdapter = new PlaceSlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

        mPager = (ViewPager) view.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        // ((CirclePageIndicator) mIndicator).setSnap(true);

        mIndicator
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        Toast.makeText(PlaceDetailsFragment.this.getActivity(),
                                "Changed to page " + position,
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPageScrolled(int position,
                            float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });

        return view;
    }

    public class CustomScrollView extends ScrollView {
        private GestureDetector mGestureDetector;
        View.OnTouchListener mGestureListener;

        public CustomScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mGestureDetector = new GestureDetector(context, new YScrollDetector());
            setFadingEdgeLength(0);
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
        }

        // Return false if we're scrolling in the x direction  
        class YScrollDetector extends SimpleOnGestureListener {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                if(Math.abs(distanceY) > Math.abs(distanceX)) {
                    return true;
                }
                return false;
            }
        }
    }

}
4

1 回答 1

3

必须创建一个自定义滚动视图并使用它

public class VerticalScrollView extends ScrollView {
    View.OnTouchListener mGestureListener;
    private VelocityTracker mVelocityTracker;
    private int mMaximumVelocity;

    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFadingEdgeLength(0);
        final ViewConfiguration configuration = ViewConfiguration.get(context);
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    }

    private boolean doNotInterceptUntilUp = false;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // System.out.println("onInterceptTouchEvent");
        int action = ev.getAction();
        int index = MotionEventCompat.getActionIndex(ev);
        int mActivePointerId = MotionEventCompat.getPointerId(ev, index);

        if (action == MotionEvent.ACTION_DOWN) {
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            }
            mVelocityTracker.addMovement(ev);
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (doNotInterceptUntilUp) {
                return false;
            }
            mVelocityTracker.addMovement(ev);
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
                    mVelocityTracker, mActivePointerId);
            int initialYVelocity = (int) VelocityTrackerCompat.getYVelocity(
                    mVelocityTracker, mActivePointerId);
            if (Math.abs(initialVelocity) > 100) {
                // 140 is the minimum threshold it seems.
                if (!(Math.abs(initialYVelocity) > 140))
                    doNotInterceptUntilUp = true;
                return false;
            }
        } else if (action == MotionEvent.ACTION_UP) {
            doNotInterceptUntilUp = false;
            if (mVelocityTracker != null) {
                mVelocityTracker.clear();
            }
        }
        boolean val = super.onInterceptTouchEvent(ev);
        return val;
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<com.m7.nomad.VerticalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.m7.nomad"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:orientation="vertical" >

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/indicator"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:paddingBottom="10dip"
                android:paddingLeft="10dip"
                android:paddingTop="1dip" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Testing something cool" />
        </LinearLayout>
    </LinearLayout>

</com.m7.nomad.VerticalScrollView>
于 2012-12-21T15:16:30.727 回答