在滑动

时间:2016-02-07 10:08:37

标签: android android-layout android-fragments

我正在使用滑动功能处理android TabLayout。我使用片段在tablayout中导航。

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(
            getSupportFragmentManager());

      adapter.addFragment(new OneFragment(), "One");
      adapter.addFragment(new TwoFragment(), "Two");
      adapter.addFragment(new ThreeFragment(), "Three");
      adapter.addFragment(new FourFragment(), "Four");
      adapter.addFragment(new FivetFragment(), "Five");

adapter.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (isGoingToRightPage && isNavigated
                && dragging == ViewPager.SCROLL_STATE_DRAGGING) {
            if (mLastPage == position)
                navigateToLoginScreen();
        } else {
           //do nothing
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (position == APP_INTRO_TOTAL - 1) {
            isGoingToRightPage = true;
        } else {
            isGoingToRightPage = false;
        }
        mLastPage = position;
    }

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

private void navigateToLoginScreen() {
   isNavigated = false;
   Intent intent = new Intent(AppIntroActivity.this, LoginActivity.class);
   startActivity(intent);
   finish();
}

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

当用户在TabLayout内滑动片段时,片段会向左和向右导航。这里FivetFragment()是Tablayout中的最后一个片段。如果我从FivetFragment()片段向左滑动,目前没有任何反应,因为FivetFragment()是该TabLayout中的最后一个片段。当我从Profile.Java片段向左滑动时,我想导航到名为FivetFragment()的下一个活动。

是否可以在片段上滑动片段导航到Profile活动。

请帮我在android中执行此操作。

1 个答案:

答案 0 :(得分:1)

只需覆盖您的viewpager.addOnPageChangeListener。并添加以下代码

adapter.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (isGoingToRightPage && isNavigated
                    && dragging == ViewPager.SCROLL_STATE_DRAGGING) {
                if (mLastPage == position)
                    navigateToLoginScreen();
            } else {
               //do nothing
            }
        }

        @Override
        public void onPageSelected(int position) {
            if (position == APP_INTRO_TOTAL - 1) {
                isGoingToRightPage = true;
            } else {
                isGoingToRightPage = false;
            }
            mLastPage = position;
        }

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

    private void navigateToLoginScreen() {
       isNavigated = false;
       Intent intent = new Intent(AppIntroActivity.this, LoginActivity.class);
       startActivity(intent);
       finish();
    }

全局定义

int mLastPage;
boolean isGoingToRightPage;
boolean isNavigated = true;
int dragging;