Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

android - ViewPager with FragmentPageAdater not working properly?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...