ViewPager with FragmentPageAdater not working properly?

允我心安 提交于 2019-12-12 06:39:00

问题


I have an Activity which extends ActionBarActivity and activity has following code in xml.

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

It has just ViewPager. I want to display Image with list of options so I opted for creating a fragment with layout I want to be shown as a part of ViewPager.

I created an adapter extending FragmentPagerAdapter as follows:

private class CategoryViewPagerAdapter extends FragmentStatePagerAdapter {

public CategoryViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int currentPosition) {

            return FullScreenFragment.newInstance(fragments
                    .get(currentPosition));
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return fragments.size();
        }

    }

Where fragments is list of Strings.

It loads Images and Swipes also work however Image shown has title which is of previous image. THis title is basically set in ActionBar for that fragment.

In my fragment class I did this:

parentActivity = (ActionBarActivity) getActivity();
        parentActivity.getSupportActionBar().setDisplayOptions(
                ActionBar.DISPLAY_SHOW_CUSTOM);
        parentActivity.getSupportActionBar().setCustomView(
                R.layout.full_screen_action_bar);

And also sometimes few components with in fragment layout dont rendered on click a button within fragment. Do i need to findId from Activity or Fragment rootView


回答1:


use this method to update action bar

    vp.setOnPageChangeListener(new OnPageChangeListener()
    {

        @Override
        public void onPageSelected(int arg0)
        {
          //update action bar 

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0)
        {
            // TODO Auto-generated method stub

        }
    });

And for the second problem , please give more info .



来源:https://stackoverflow.com/questions/24887112/viewpager-with-fragmentpageadater-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!