Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:58:17+00:00 2026-06-18T08:58:17+00:00

I have a parent SherlockFragmentActivity class that contains ViewPager with 4 Fragments inside. One

  • 0

I have a parent SherlockFragmentActivity class that contains ViewPager with 4 Fragments inside.
One of them extends from SherlockListFragment and I want to scroll it to top by click on it’s tab.

MainActivity class:

public class MainActivity extends SherlockFragmentActivity  {

ViewPager viewPager;
TabAdapter tabAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(Constants.THEME);

    viewPager = new ViewPager(this);
    viewPager.setId(R.id.pager);
    viewPager.setBackgroundResource(R.color.background_color);

    tabAdapter = new TabAdapter(this,viewPager);

    super.onCreate(savedInstanceState);
    setContentView(viewPager);

    // Action bar setup
    setupTabs(savedInstanceState);
}

private void setupTabs(Bundle savedInstanceState) {
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (int i = 1; i <= 4; i++) {
        ActionBar.Tab tab = getSupportActionBar().newTab();
        switch (i) {
            case 1:
                tab.setText(R.string.feed).setTabListener(tabAdapter);
                tabAdapter.addTab(EventListFragment.class);
                break;
            case 2:
                tab.setText(R.string.downloads).setTabListener(tabAdapter);
                tabAdapter.addTab(LocalEventListFragment.class);
                break;
            case 3:
                tab.setText(R.string.tags).setTabListener(tabAdapter);
                tabAdapter.addTab(TagsFragment.class);
                break;
            case 4:
                tab.setText(R.string.settings).setTabListener(tabAdapter);
                tabAdapter.addTab(SettingsFragment.class);
                break;
        }
        getSupportActionBar().addTab(tab);
    }
}

TabAdapter class:

public static class TabAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {

    private final Context mContext;
    private final ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
    private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;

    public TabAdapter(SherlockFragmentActivity activity, ViewPager pager){
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(Class<?> clss){
        classes.add(clss);
        mFragments.add(Fragment.instantiate(mContext, clss.getName(),
                null));
    }

    @Override
    public Fragment getItem(int i) {
        return mFragments.get(i);
    }

    public int getId(int index){
        return mFragments.get(index).getId();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        // fixed double call onTabReselected
        if (mActionBar.getSelectedNavigationIndex() != position)
            mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int i) {
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        final int position = tab.getPosition();
        final Fragment fragment =  mFragments.get(position);
        if (fragment != null) {
            switch (position) {
                case 0:
                    // call tab's ListFragment scroll to top item
                    ((EventListFragment) fragment).scrollToTop();
                    break;
                case 1:
                    // do something
                    break;
        }
    }
}

onTabReselected called, then user press current tab, so I try to do EventListFragment scrolling:

EventListFragment class:

public class EventListFragment extends SherlockListFragment {

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mAdapter = new EventListAdapter(getSherlockActivity());
        setListAdapter(mAdapter);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

   @Override
   public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View eventListLayout = inflater.inflate(R.layout.eventlist_fragment,null);
        ListView lv = (ListView) eventListLayout.findViewById(android.R.id.list);
        ViewGroup parent = (ViewGroup) lv.getParent();
        //Remove ListView and add PullToRefreshListView in its place
        int lvIndex = parent.indexOfChild(lv);
        parent.removeViewAt(lvIndex);
        mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
        parent.addView(mPullToRefreshListView, lvIndex, lv.getLayoutParams());
        return eventListLayout;
   }

   public void scrollToTop() {
      if (getSherlockActivity() != null) {  // null after screen rotation
        final ListView listView =  getListView();
        listView.post(new Runnable(){
        public void run() {
            listView.smoothScrollToPosition(0);
        }});
      }
   }

It works on application start, but after screen rotation scrollToTop – getSherlockActivity() returns null.
If remove this condition check, have an exception:

java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.project.fragment.EventListFragment.scrollToTop(EventListFragment.java:337)
at com.project.MainActivity$TabAdapter.onTabReselected(MainActivity.java:411)
at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabReselected(ActionBarWrapper.java:327)

I haven’t totally understatinding how to ViewPager and its FragmentPagerAdapter works. So can’t find a problem, that occurs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:58:18+00:00Added an answer on June 18, 2026 at 8:58 am

    Applied solution according to this post:

    MainActivity class:

    //...
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        FragmentManager manager = getSupportFragmentManager();
        for (int i = 0; i < tabAdapter.getClasses().size(); i++) {
            String className = tabAdapter.getClasses().get(i).getName();
            if (manager.findFragmentByTag(className) != null) {
                manager.putFragment(outState, className, manager.findFragmentByTag(className));
            }
        }
    }
    //...
    

    TabAdapter class:

    //...
    public void addTab(Class<?> clss, FragmentManager manager, Bundle savedInstanceState) {
            classes.add(clss);
            String className = clss.getName();
            tags.add(className);
            Fragment fragment;
    
            if (savedInstanceState != null) {
                fragment = manager.getFragment(savedInstanceState, className);
                if (fragment == null) {
                    fragment = createFragment(className);
                    manager.beginTransaction().add(fragment, className);
                }
            } else {
                fragment = createFragment(className);
                manager.beginTransaction().add(fragment, className);
            }
            mFragments.add(fragment);
        }
    //...
    

    where createFragment is a simple method, that creates needed fragment by passed class name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have parent class c1 with function hi(). class c2 extends c1 and contains
I have a parent class which contains a child object. I am using set
I have a ViewPager FragmentActivity that holds 3 tabs, Each one of the tabs
So, I have bunch of elements inside parent elements: <div class=copyFrom> <div class=copyThese>copyThese1</div> </div>
I have a parent with two children that I want to communicate with each
I have a parent div .photo_container that holds an img , and stretches to
I have a parent project that is branched to many dependent child projects. I
I have parent & a child class and trying to instantiate the child class
I have parent process that opens child process . I need to perform some
A typical problem in OO programming is the diamond problem. I have parent class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.