androidandroid-fragmentspaginationandroid-viewpager

Not Getting page Count on Activity from Fragment Adapter and Fragment Class


Paging View

I have one details activity. Inside this, I'm using paging Controller with

<android.support.v4.view.ViewPager ..../> for fragment details view.

My code:

ViewPager pager = (ViewPager) findViewById(R.id.pagerHandbookDetails);

/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();

/** Instantiating FragmentPagerAdapter */
DetailsFragmentAdapter pagerAdapter = new DetailsFragmentAdapter(fm,cases);

/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);

Then, Details Fragment Adapter Class extends FragmentPagerAdapter

int PAGE_COUNT = 9;

@Override
public Fragment getItem(int arg0) {

    DetailsFragment detailsFragment = new DetailsFragment();
    Bundle data = new Bundle();
    Log.d("Current Page", "Page " + arg0);

    data.putInt("current_page", arg0);
    detailsFragment.setArguments(data);
    return detailsFragment;
}

Class Details Fragment extends Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle data = getArguments();
    int mCurrentPage = data.getInt("current_page", 0);
}

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

        layoutPaging = (LinearLayout) v.findViewById(R.id.layoutPagingView);

        for(int i = 0; i < 9; i++){

            ImageView image1 = new ImageView(layoutPaging.getContext());
            image1.setTag(i);

            if(i == mCurrentPage)
            {
                image1.setImageResource(R.drawable.black_dot);
            }
            else
            {
                image1.setImageResource(R.drawable.white_dot);
            }

            layoutPaging.addView(image1); 
        }
        .......
}

It's working fine like this.

So whenever I swipe page, It'll obviously move all content of details fragment page. So it'll also move my custom view layout for paging. And it looks odd.

I want this layout static and change image content as per swipe pages.

If I'm taking this layout in main details class, then I'm not getting current page's value for changing an image from fragment adapter. It's only changing values inside fragment details class.

I'm stuck here.


Solution

  • you can add page indicators in your layout that is provided by:

    https://github.com/Papercloud/SimpleViewPagerIndicator

    you need to just place the control and set your viewpager controller's object to SimpleViewPagerIndicator.You can find sample code and implementation steps there

    Using these classes, you can get indicators that will not scroll with pages