如何在扩展Fragment时获取ViewPager的特定页面

时间:2014-03-19 08:12:52

标签: android android-fragments android-viewpager slidingmenu

在我的应用程序中,我有一个ViewPager和两个滑动菜单,我的ViewPager扩展Fragment,我要实现的是:在我的左侧滑动菜单中,我有一些按钮,之后点击每个按钮,我想查看ViewPager的特定页面,但我无法实现它

这是我的ViewPageFragment类:

public class ViewPageFragment extends Fragment {

private ImageView showLeft;
private ImageView showRight;
private MyAdapter mAdapter;
private ViewPager mPager;
private ArrayList<Fragment> pagerItemList = new ArrayList<Fragment>();

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


    View mView = inflater.inflate(R.layout.view_pager, null);
    showLeft = (ImageView) mView.findViewById(R.id.showLeft);
    showRight = (ImageView) mView.findViewById(R.id.showRight);
    mPager = (ViewPager) mView.findViewById(R.id.pager);
    PageFragment1 page1 = new PageFragment1();
    PageFragment2 page2 = new PageFragment2();
    PageFragment3 page3 = new PageFragment3();
    PageFragment4 page4 = new PageFragment4();
    pagerItemList.add(page1);
    pagerItemList.add(page2);
    pagerItemList.add(page3);
    pagerItemList.add(page4);
    mAdapter = new MyAdapter(getFragmentManager());
    mPager.setAdapter(mAdapter);
    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // @Override
        public void onPageSelected(int position) {

            if (myPageChangeListener != null)
                myPageChangeListener.onPageSelected(position);

        }

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

        }

        // @Override
        public void onPageScrollStateChanged(int position) {

        }
    });

    return mView;
}

public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    showLeft.setOnClickListener(new OnClickListener() {

        // @Override
        public void onClick(View v) {
            ((SlidingActivity) getActivity()).showLeft();
        }
    });

    showRight.setOnClickListener(new OnClickListener() {

        // @Override
        public void onClick(View v) {
            ((SlidingActivity) getActivity()).showRight();
        }
    });
}

public boolean isFirst() {
    if (mPager.getCurrentItem() == 0)
        return true;
    else
        return false;
}

public boolean isEnd() {
    if (mPager.getCurrentItem() == pagerItemList.size() - 1)
        return true;
    else
        return false;
}

public class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return pagerItemList.size();
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;
        if (position < pagerItemList.size())
            fragment = pagerItemList.get(position);
        else
            fragment = pagerItemList.get(0);

        return fragment;

    }
}

private MyPageChangeListener myPageChangeListener;

public void setMyPageChangeListener(MyPageChangeListener l) {

    myPageChangeListener = l;

}

public interface MyPageChangeListener {
    public void onPageSelected(int position);
}

}

这是我的SlidingMenu课程:

public class SlidingMenu extends RelativeLayout {

private View mSlidingView;
private View mMenuView;
private View mDetailView;
private RelativeLayout bgShade;
private int screenWidth;
private int screenHeight;
private Context mContext;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;
private static final int VELOCITY = 50;
private boolean mIsBeingDragged = true;
private boolean tCanSlideLeft = true;
private boolean tCanSlideRight = false;
private boolean hasClickLeft = false;
private boolean hasClickRight = false;

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

private void init(Context context) {

    mContext = context;
    bgShade = new RelativeLayout(context);
    mScroller = new Scroller(getContext());
    mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    WindowManager windowManager = ((Activity) context).getWindow()
            .getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();
    LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight);
    bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    bgShade.setLayoutParams(bgParams);

}

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

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

public void addViews(View left, View center, View right) {
    setLeftView(left);
    setRightView(right);
    setCenterView(center);
}

public void setLeftView(View view) {
    LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.FILL_PARENT);
    addView(view, behindParams);
    mMenuView = view;
}

public void setRightView(View view) {
    LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.FILL_PARENT);
    behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    addView(view, behindParams);
    mDetailView = view;
}

public void setCenterView(View view) {
    LayoutParams aboveParams = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT);

    LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight);
    bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);

    View bgShadeContent = new View(mContext);
    bgShadeContent.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.shade_bg));
    bgShade.addView(bgShadeContent, bgParams);

    addView(bgShade, bgParams);

    addView(view, aboveParams);
    mSlidingView = view;
    mSlidingView.bringToFront();
}

@Override
public void scrollTo(int x, int y) {
    super.scrollTo(x, y);
    postInvalidate();
}

@Override
public void computeScroll() {
    if (!mScroller.isFinished()) {
        if (mScroller.computeScrollOffset()) {
            int oldX = mSlidingView.getScrollX();
            int oldY = mSlidingView.getScrollY();
            int x = mScroller.getCurrX();
            int y = mScroller.getCurrY();
            if (oldX != x || oldY != y) {
                if (mSlidingView != null) {
                    mSlidingView.scrollTo(x, y);
                    if (x < 0)
                        bgShade.scrollTo(x + 20, y);// 背景阴影��
                    else
                        bgShade.scrollTo(x - 20, y);// 背景阴影左�
                }
            }
            invalidate();
        }
    } 
}

private boolean canSlideLeft = true;
private boolean canSlideRight = false;

public void setCanSliding(boolean left, boolean right) {
    canSlideLeft = left;
    canSlideRight = right;
}


/*拦截touch事件*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();
    final float x = ev.getX();
    final float y = ev.getY();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionX = x;
        mLastMotionY = y;
        mIsBeingDragged = false;
        if (canSlideLeft) {
            mMenuView.setVisibility(View.VISIBLE);
            mDetailView.setVisibility(View.INVISIBLE);
        }
        if (canSlideRight) {
            mMenuView.setVisibility(View.INVISIBLE);
            mDetailView.setVisibility(View.VISIBLE);
        }
        break;

    case MotionEvent.ACTION_MOVE:
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float yDiff = Math.abs(y - mLastMotionY);
        if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (canSlideLeft) {
                float oldScrollX = mSlidingView.getScrollX();
                if (oldScrollX < 0) {
                    mIsBeingDragged = true;
                    mLastMotionX = x;
                } else {
                    if (dx > 0) {
                        mIsBeingDragged = true;
                        mLastMotionX = x;
                    }
                }

            } else if (canSlideRight) {
                float oldScrollX = mSlidingView.getScrollX();
                if (oldScrollX > 0) {
                    mIsBeingDragged = true;
                    mLastMotionX = x;
                } else {
                    if (dx < 0) {
                        mIsBeingDragged = true;
                        mLastMotionX = x;
                    }
                }
            }

        }
        break;

    }
    return mIsBeingDragged;
}

/*处�拦截�的touch事件*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();
    final float y = ev.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        mLastMotionX = x;
        mLastMotionY = y;
        if (mSlidingView.getScrollX() == -getMenuViewWidth()
                && mLastMotionX < getMenuViewWidth()) {
            return false;
        }

        if (mSlidingView.getScrollX() == getDetailViewWidth()
                && mLastMotionX > getMenuViewWidth()) {
            return false;
        }

        break;
    case MotionEvent.ACTION_MOVE:
        if (mIsBeingDragged) {
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = mSlidingView.getScrollX();
            float scrollX = oldScrollX + deltaX;
            if (canSlideLeft) {
                if (scrollX > 0)
                    scrollX = 0;
            }
            if (canSlideRight) {
                if (scrollX < 0)
                    scrollX = 0;
            }
            if (deltaX < 0 && oldScrollX < 0) { // left view
                final float leftBound = 0;
                final float rightBound = -getMenuViewWidth();
                if (scrollX > leftBound) {
                    scrollX = leftBound;
                } else if (scrollX < rightBound) {
                    scrollX = rightBound;
                }
            } else if (deltaX > 0 && oldScrollX > 0) { // right view
                final float rightBound = getDetailViewWidth();
                final float leftBound = 0;
                if (scrollX < leftBound) {
                    scrollX = leftBound;
                } else if (scrollX > rightBound) {
                    scrollX = rightBound;
                }
            }
            if (mSlidingView != null) {
                mSlidingView.scrollTo((int) scrollX,
                        mSlidingView.getScrollY());
                if (scrollX < 0)
                    bgShade.scrollTo((int) scrollX + 20,
                            mSlidingView.getScrollY());
                else
                    bgShade.scrollTo((int) scrollX - 20,
                            mSlidingView.getScrollY());
            }

        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(100);
            float xVelocity = velocityTracker.getXVelocity();
            int oldScrollX = mSlidingView.getScrollX();
            int dx = 0;
            if (oldScrollX <= 0 && canSlideLeft) {// left view
                if (xVelocity > VELOCITY) {
                    dx = -getMenuViewWidth() - oldScrollX;
                } else if (xVelocity < -VELOCITY) {
                    dx = -oldScrollX;
                    if (hasClickLeft) {
                        hasClickLeft = false;
                        setCanSliding(tCanSlideLeft, tCanSlideRight);
                    }
                } else if (oldScrollX < -getMenuViewWidth() / 2) {
                    dx = -getMenuViewWidth() - oldScrollX;
                } else if (oldScrollX >= -getMenuViewWidth() / 2) {
                    dx = -oldScrollX;
                    if (hasClickLeft) {
                        hasClickLeft = false;
                        setCanSliding(tCanSlideLeft, tCanSlideRight);
                    }
                }

            }
            if (oldScrollX >= 0 && canSlideRight) {
                if (xVelocity < -VELOCITY) {
                    dx = getDetailViewWidth() - oldScrollX;
                } else if (xVelocity > VELOCITY) {
                    dx = -oldScrollX;
                    if (hasClickRight) {
                        hasClickRight = false;
                        setCanSliding(tCanSlideLeft, tCanSlideRight);
                    }
                } else if (oldScrollX > getDetailViewWidth() / 2) {
                    dx = getDetailViewWidth() - oldScrollX;
                } else if (oldScrollX <= getDetailViewWidth() / 2) {
                    dx = -oldScrollX;
                    if (hasClickRight) {
                        hasClickRight = false;
                        setCanSliding(tCanSlideLeft, tCanSlideRight);
                    }
                }
            }

            smoothScrollTo(dx);

        }

        break;
    }

    return true;
}

private int getMenuViewWidth() {
    if (mMenuView == null) {
        return 0;
    }
    return mMenuView.getWidth();
}

private int getDetailViewWidth() {
    if (mDetailView == null) {
        return 0;
    }
    return mDetailView.getWidth();
}

void smoothScrollTo(int dx) {
    int duration = 500;
    int oldScrollX = mSlidingView.getScrollX();
    mScroller.startScroll(oldScrollX, mSlidingView.getScrollY(), dx,
            mSlidingView.getScrollY(), duration);
    invalidate();
}

public void showLeftView() {
    int menuWidth = mMenuView.getWidth();
    int oldScrollX = mSlidingView.getScrollX();
    if (oldScrollX == 0) {
        mMenuView.setVisibility(View.VISIBLE);
        mDetailView.setVisibility(View.INVISIBLE);
        smoothScrollTo(-menuWidth);
        tCanSlideLeft = canSlideLeft;
        tCanSlideRight = canSlideRight;
        hasClickLeft = true;
        setCanSliding(true, false);
    } else if (oldScrollX == -menuWidth) {
        smoothScrollTo(menuWidth);
        if (hasClickLeft) {
            hasClickLeft = false;
            setCanSliding(tCanSlideLeft, tCanSlideRight);
        }
    }
}
public void showRightView() {
    int menuWidth = mDetailView.getWidth();
    int oldScrollX = mSlidingView.getScrollX();
    if (oldScrollX == 0) {
        mMenuView.setVisibility(View.INVISIBLE);
        mDetailView.setVisibility(View.VISIBLE);
        smoothScrollTo(menuWidth);
        tCanSlideLeft = canSlideLeft;
        tCanSlideRight = canSlideRight;
        hasClickRight = true;
        setCanSliding(false, true);
    } else if (oldScrollX == menuWidth) {
        smoothScrollTo(-menuWidth);
        if (hasClickRight) {
            hasClickRight = false;
            setCanSliding(tCanSlideLeft, tCanSlideRight);
        }
    }
}

}

这是我的LeftFragment类,它与我的左侧滑动菜单相关:

public class LeftFragment extends Fragment {
private ArrayList<Fragment> pagerItemList = new ArrayList<Fragment>();

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

{

    if (container == null) {

        return null;
    }

    LinearLayout left_menu_list = (LinearLayout) inflater.inflate(
            R.layout.left, container, false);

    LinearLayout leftMenuItem3 = (LinearLayout) left_menu_list
            .findViewById(R.id.left_menu_item3);
    leftMenuItem3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Log.i("SAHAR click", "I SHOULD DO SOMETHING HERE ");

        }
    });

    return left_menu_list;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

}

这是我的一个页面片段的例子:

public class PageFragment3 extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.page3, null);
    return view;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

}

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法选择特定页面。

viewPager.setCurrentItem(position);

此处position是页面索引