Search in sources :

Example 81 with PagerAdapter

use of android.support.v4.view.PagerAdapter in project nmid-headline by miao1007.

the class SlidingTabFragment method onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.d(TAG, "onViewCreated");
    fragments.add(NewsFeedFragment.newInstance("学院风采", HeadlineService.CATE_SCHOOL));
    fragments.add(NewsFeedFragment.newInstance("青春通信", HeadlineService.CATE_TELECOMMUNICATION));
    fragments.add(NewsFeedFragment.newInstance("科技创新", HeadlineService.CATE_TECHNOLOGY));
    fragments.add(NewsFeedFragment.newInstance("通信校友", HeadlineService.CATE_ALUMNUS));
    Log.d(TAG, "setViewPager");
    mTabLayout.setBackgroundResource(ThemePref.getToolbarBackgroundResColor(getActivity()));
    mPagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager(), fragments);
    //mViewPager.setOffscreenPageLimit(fragments.size());
    mViewPager.setAdapter(mPagerAdapter);
    mTabLayout.setViewPager(mViewPager);
}
Also used : FragmentStatePagerAdapter(android.support.v4.app.FragmentStatePagerAdapter)

Example 82 with PagerAdapter

use of android.support.v4.view.PagerAdapter in project material-components-android by material-components.

the class TabLayout method setupWithViewPager.

private void setupWithViewPager(@Nullable final ViewPager viewPager, boolean autoRefresh, boolean implicitSetup) {
    if (mViewPager != null) {
        // If we've already been setup with a ViewPager, remove us from it
        if (mPageChangeListener != null) {
            mViewPager.removeOnPageChangeListener(mPageChangeListener);
        }
        if (mAdapterChangeListener != null) {
            mViewPager.removeOnAdapterChangeListener(mAdapterChangeListener);
        }
    }
    if (mCurrentVpSelectedListener != null) {
        // If we already have a tab selected listener for the ViewPager, remove it
        removeOnTabSelectedListener(mCurrentVpSelectedListener);
        mCurrentVpSelectedListener = null;
    }
    if (viewPager != null) {
        mViewPager = viewPager;
        // Add our custom OnPageChangeListener to the ViewPager
        if (mPageChangeListener == null) {
            mPageChangeListener = new TabLayoutOnPageChangeListener(this);
        }
        mPageChangeListener.reset();
        viewPager.addOnPageChangeListener(mPageChangeListener);
        // Now we'll add a tab selected listener to set ViewPager's current item
        mCurrentVpSelectedListener = new ViewPagerOnTabSelectedListener(viewPager);
        addOnTabSelectedListener(mCurrentVpSelectedListener);
        final PagerAdapter adapter = viewPager.getAdapter();
        if (adapter != null) {
            // Now we'll populate ourselves from the pager adapter, adding an observer if
            // autoRefresh is enabled
            setPagerAdapter(adapter, autoRefresh);
        }
        // Add a listener so that we're notified of any adapter changes
        if (mAdapterChangeListener == null) {
            mAdapterChangeListener = new AdapterChangeListener();
        }
        mAdapterChangeListener.setAutoRefresh(autoRefresh);
        viewPager.addOnAdapterChangeListener(mAdapterChangeListener);
        // Now update the scroll position to match the ViewPager's current item
        setScrollPosition(viewPager.getCurrentItem(), 0f, true);
    } else {
        // We've been given a null ViewPager so we need to clear out the internal state,
        // listeners and observers
        mViewPager = null;
        setPagerAdapter(null, false);
    }
    mSetupViewPagerImplicitly = implicitSetup;
}
Also used : PagerAdapter(android.support.v4.view.PagerAdapter)

Example 83 with PagerAdapter

use of android.support.v4.view.PagerAdapter in project material-components-android by material-components.

the class TabLayoutWithViewPagerTest method testAdapterChange.

@Test
@SmallTest
public void testAdapterChange() {
    setupTabLayoutWithViewPager();
    // Verify that we have the expected initial adapter
    PagerAdapter initialAdapter = mViewPager.getAdapter();
    assertEquals("Initial adapter class", ColorPagerAdapter.class, initialAdapter.getClass());
    assertEquals("Initial adapter page count", 3, initialAdapter.getCount());
    // Create a new adapter
    TextPagerAdapter newAdapter = new TextPagerAdapter();
    final int newItemCount = 6;
    for (int i = 0; i < newItemCount; i++) {
        newAdapter.add("Title " + i, "Body " + i);
    }
    // And set it on the ViewPager
    onView(withId(R.id.tabs_viewpager)).perform(setAdapter(newAdapter), ViewPagerActions.scrollToPage(0));
    // As TabLayout doesn't track adapter changes, we need to re-wire the new adapter
    onView(withId(R.id.tabs)).perform(setupWithViewPager(mViewPager));
    // We have more comprehensive test coverage for changing the ViewPager adapter in v4/tests.
    // Here we are focused on testing the integration of TabLayout with the new
    // content of ViewPager
    assertEquals("Matching item count", newItemCount, mTabLayout.getTabCount());
    for (int i = 0; i < newItemCount; i++) {
        assertEquals("Tab #" + i, mViewPager.getAdapter().getPageTitle(i), mTabLayout.getTabAt(i).getText());
    }
    verifyViewPagerSelection();
    verifyTabLayoutSelection();
}
Also used : PagerAdapter(android.support.v4.view.PagerAdapter) SmallTest(android.support.test.filters.SmallTest) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test) LargeTest(android.support.test.filters.LargeTest) SmallTest(android.support.test.filters.SmallTest)

Example 84 with PagerAdapter

use of android.support.v4.view.PagerAdapter in project material-components-android by material-components.

the class TabLayoutWithViewPagerTest method testAdapterContentChange.

@Test
@SmallTest
public void testAdapterContentChange() {
    setupTabLayoutWithViewPager();
    // Verify that we have the expected initial adapter
    PagerAdapter initialAdapter = mViewPager.getAdapter();
    assertEquals("Initial adapter class", ColorPagerAdapter.class, initialAdapter.getClass());
    assertEquals("Initial adapter page count", 3, initialAdapter.getCount());
    // Add two more entries to our adapter
    onView(withId(R.id.tabs_viewpager)).perform(addItemsToPager(new String[] { "Yellow", "Magenta" }, new Integer[] { Color.YELLOW, Color.MAGENTA }));
    // We have more comprehensive test coverage for changing the ViewPager adapter in v4/tests.
    // Here we are focused on testing the continuous integration of TabLayout with the new
    // content of ViewPager
    final int newItemCount = mDefaultPagerAdapter.getCount();
    assertEquals("Matching item count", newItemCount, mTabLayout.getTabCount());
    for (int i = 0; i < newItemCount; i++) {
        assertEquals("Tab #" + i, mViewPager.getAdapter().getPageTitle(i), mTabLayout.getTabAt(i).getText());
    }
    verifyViewPagerSelection();
    verifyTabLayoutSelection();
}
Also used : PagerAdapter(android.support.v4.view.PagerAdapter) SmallTest(android.support.test.filters.SmallTest) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test) LargeTest(android.support.test.filters.LargeTest) SmallTest(android.support.test.filters.SmallTest)

Example 85 with PagerAdapter

use of android.support.v4.view.PagerAdapter in project ProductTour by matrixxun.

the class ProductTour2Activity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    setContentView(R.layout.activity_tutorial);
    skip = Button.class.cast(findViewById(R.id.skip));
    skip.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            endTutorial();
        }
    });
    next = ImageButton.class.cast(findViewById(R.id.next));
    next.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            pager.setCurrentItem(pager.getCurrentItem() + 1, true);
        }
    });
    done = Button.class.cast(findViewById(R.id.done));
    done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            endTutorial();
        }
    });
    pager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    pager.setAdapter(pagerAdapter);
    pager.setPageTransformer(true, new CrossfadePageTransformer());
    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (position == NUM_PAGES - 2 && positionOffset > 0) {
                if (isOpaque) {
                    pager.setBackgroundColor(Color.TRANSPARENT);
                    isOpaque = false;
                }
            } else {
                if (!isOpaque) {
                    pager.setBackgroundColor(getResources().getColor(R.color.primary_material_light));
                    isOpaque = true;
                }
            }
        }

        @Override
        public void onPageSelected(int position) {
            setIndicator(position);
            if (position == NUM_PAGES - 2) {
                skip.setVisibility(View.GONE);
                next.setVisibility(View.GONE);
                done.setVisibility(View.VISIBLE);
            } else if (position < NUM_PAGES - 2) {
                skip.setVisibility(View.VISIBLE);
                next.setVisibility(View.VISIBLE);
                done.setVisibility(View.GONE);
            } else if (position == NUM_PAGES - 1) {
                endTutorial();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    buildCircles();
}
Also used : Window(android.view.Window) ImageButton(android.widget.ImageButton) ImageButton(android.widget.ImageButton) Button(android.widget.Button) ImageView(android.widget.ImageView) View(android.view.View) ViewPager(android.support.v4.view.ViewPager)

Aggregations

PagerAdapter (android.support.v4.view.PagerAdapter)111 View (android.view.View)49 ViewPager (android.support.v4.view.ViewPager)44 TextView (android.widget.TextView)33 ViewGroup (android.view.ViewGroup)19 HorizontalScrollView (android.widget.HorizontalScrollView)19 LinearLayout (android.widget.LinearLayout)13 Toolbar (android.support.v7.widget.Toolbar)12 ImageView (android.widget.ImageView)11 TabLayout (android.support.design.widget.TabLayout)10 ArrayList (java.util.ArrayList)9 RecyclerView (android.support.v7.widget.RecyclerView)8 FragmentStatePagerAdapter (android.support.v4.app.FragmentStatePagerAdapter)7 AppBarLayout (android.support.design.widget.AppBarLayout)6 FragmentPagerAdapter (android.support.v4.app.FragmentPagerAdapter)6 MenuItem (android.view.MenuItem)5 ImageButton (android.widget.ImageButton)5 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)4 LargeTest (android.support.test.filters.LargeTest)4 MediumTest (android.support.test.filters.MediumTest)4