ViewPager uses Fragment for unlimited sliding and viewpagerfragment

Source: Internet
Author: User

ViewPager uses Fragment for unlimited sliding and viewpagerfragment

 

Implement ViewPager and Fragment to implement infinite loop switching, which is also carried out in the adapter. Of course, sliding listening can also be used.

Import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentPagerAdapter; import android. view. viewGroup; import java. util. arrayList; public class adapter extends FragmentPagerAdapter
{Private ArrayList <Fragment> fragmentList; public adapter (FragmentManager fm) {super (fm);} public adapter (FragmentManager fm, ArrayList <Fragment> fragmentList) {super (fm ); this. fragmentList = fragmentList;} @ Override public Fragment getItem (int position) {// the reason that the position is not processed here is that the getItem method is called in the // instantiateItem method. You only need to pre-process the call // position to avoid repeated processing of return fragmentList. get (position) ;}@ Override public int getCount ()
{Return Integer. MAX_VALUE;} @ Override public int getItemPosition (Object object)
{Return super. getItemPosition (object) ;}@ Override public Object instantiateItem (ViewGroup container, int position)
{// Process position. Place the array subscript in [0, fragmentList. size) to prevent cross-border position = position % fragmentList. size (); return super. instantiateItem (container, position );}}

Above reference: blog http://www.ilrose.com/blog/2015/12/09/android-viewpager%E5% AE %9E%E7%8E%B0%E6%97%A0%E9%99%90%E5%BE%AA%E7%8E%AF%E6%BB%91%E5%8A%A8/

After actual tests, four fragment entries must be uploaded; otherwise, a problem may occur when sliding to the right side ......

This method can be used to achieve the paging effect of the answer interface. You can create a ReadFragment and then create four new Fragment objects. The common processing of different pages can be implemented directly in ReadFragment. When turning pages, the parameters that need to be changed on each page can be obtained from the sliding listener to implement these four objects.

        Fragment a=new ReadFragment();        Fragment b=new ReadFragment();        Fragment c=new ReadFragment();        Fragment d=new ReadFragment();        list=new ArrayList<Fragment>();        list.add(a);        list.add(b);        list.add(c);        list.add(d);
readViewPager.setAdapter(new adapter(getSupportFragmentManager(),list));

 

 

ReadViewPager. encode (new ViewPager. OnPageChangeListener () {@ Override public void evaluate (int position, float positionOffset, int positionOffsetPixels) {}@ Override public void onPageSelected (int position ){
// Process position. Place position in [0, fragmentList. size) to prevent array from crossing position = position % list. size (); ReadFragment fragment = (ReadFragment) list. get (position); // obtain the selected fragment. refreshState (); // This method is used to implement the parameters that need to be changed for each page during page turning. This method is in ReadFragment, And the modifier must use public
...} @ Override public void onPageScrollStateChanged (int state ){}});

You can turn the answer pages. Of course, the implementation of paging animation is indispensable. This next blog will talk about it separately.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.