横向滚动ViewPager横向、ViewPager

2023-09-07 10:49:45 作者:烟酒缠身

我要创建水平滚动ViewPager。我ViewPager包含片段。每个片段的宽度比所述屏幕以便滚动已启用更大的

I want to create a horizontal scrollable ViewPager. My ViewPager contains Fragments. Each Fragment's width is larger than the screen so scrolling has to be enabled.

我用下面的布局我的片段项目:

I use following layout for my fragment item:

<HorizontalScrollView
        android:id="@+id/view_horizontal_scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isScrollContainer="true" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <View
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:background="#AABBCC" />

            <View
                android:id="@+id/test"
                android:layout_width="1500dp"
                android:layout_height="fill_parent" />

            <View
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:background="#ccBBaa" />
        </LinearLayout>
    </HorizontalScrollView>

我还创建一个自定义ViewPager允许水平滚动:

I also created a custom ViewPager which allows horizontal scrolling:

public class ScrollableViewPager extends ViewPager {

    private GestureDetector mGestureDetector;

    public ScrollableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(getContext(),
                new YScrollDetector());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Tell our parent to stop intercepting our events!
        getParent().requestDisallowInterceptTouchEvent(true);

        return super.onInterceptTouchEvent(ev)
                && mGestureDetector.onTouchEvent(ev);
    }
}

该YScrollDetector只覆盖onScrollMethod。到现在为止还挺好。水平滚动工作正常。

The YScrollDetector only overwrites the onScrollMethod. So far so good. Horizontal Scrolling works fine.

但是现在我有一个问题:

让我们说我有5个片段一个ViewPager,([0],[1],[2],[3] [4])中,并要开始与项[2]。当我向后siwpe我想了previous项目的滚动位置[1]是右侧最大,而不是0。

Let's say I have a ViewPager with 5 Fragments, ([0], [1], [2], [3], [4]) in it and want to start with item [2]. When I siwpe backwards I want that the scroll position of the previous item [1] is at right maximum and not 0.

我Framgents是这样的:

My Framgents look like this:

public class AbstractFragment extends Fragment {

    private static final String _ID = "ID";
    private int _id;
    private ScrollableViewPager mPager;

    public AbstractFragment() {

    }

    public AbstractFragment(int id, int colorId) {
        this._id = id;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(getClass().getSimpleName(), "[" + _id + "]: onCreate");
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            _id = savedInstanceState.getInt(_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View fragmentView = inflater.inflate(R.layout.fragment_menu_container,
                container, false);

        setUpScrollView(fragmentView);

        return fragmentView;
    }

    @Override
    public void onResume() {
        Log.d(getClass().getSimpleName(), "[" + _id + "]: onResume");
        setUpScrollView(getView());
        super.onResume();
    }

    private void setUpScrollView(View fragmentView) {
        if (fragmentView == null) {
            return;
        }
        HorizontalScrollView scrollView = (HorizontalScrollView) fragmentView
                .findViewById(R.id.view_horizontal_scroll);

        if (scrollView != null) {
            int currentItem = getCurrentPagerItem();
            if (_id < currentItem) {
                // previous Item : X Scroll: width of content
                View content = scrollView.getChildAt(0);
                int width = content.getRight();
                width = width - scrollView.getRight();
                scrollView.scrollTo(width, 0);
                Log.d(getClass().getSimpleName(), "[" + _id + "] width: "
                        + width);
                // scrollView.fullScroll(View.FOCUS_RIGHT);
            } else if (_id > currentItem) {
                // next Item - X Scroll: 0
                scrollView.fullScroll(View.FOCUS_LEFT);
            } else {
                // current Item - Nothing to do her
            }

        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (outState == null) {
            return;
        }

        outState.putInt(_ID, _id);
    }

    private ScrollableViewPager getPager() {
        PagerActivity activity = (PagerActivity) getActivity();
        return activity.getViewPager();
    }

    private int getCurrentPagerItem() {
        if (mPager == null) {
            mPager = getPager();
        }

        return mPager.getCurrentItem();
    }
}

在我的片段我想设置的previous片段的滚动位置。但是,这并不工作,因为滚动型和内容查看的宽度均为0。

In my fragments I want to set the scroll position of the previous fragment. But this doesn't work, because the width of the ScrollView and the ContentView are both 0.

是否有人知道答案?谢谢!

Does anybody know an answer? Thanks!!!

推荐答案

我找到了一个解决方案通过自己:

I found a solution by myself:

private void setUpScrollView(final ViewGroup fragmentView) {
        if (fragmentView == null) {
            return;
        }

        fragmentView.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        HorizontalScrollView scrollView = (HorizontalScrollView) fragmentView
                                .findViewById(R.id.view_horizontal_scroll);

                        if (scrollView != null) {
                            int currentItem = getCurrentPagerItem();
                            if (_id < currentItem) {
                                // previous Item
                                View content = scrollView.getChildAt(0);
                                int width = content.getWidth();
                                width = width - scrollView.getWidth();
                                scrollView.scrollTo(width, 0);
                            } else if (_id > currentItem) {
                                // next Item
                                scrollView.scrollTo(0, 0);
                            } else {
                                // current Item
                            }

                        }
                    }
                });

    }