Android TabsAdapter with ActionbarSherlock

2019-05-03 11:36发布

问题:

I am using ActionbarSherlock with a SherlockListFragment that implements LoaderManager.LoaderCallbacks.

In my ApplicationActivity onCreate method I am using

setContentView(R.layout.application);

to set the layout -- works great.

I am initializing the actionbar like so

ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(false);
bar.setDisplayShowTitleEnabled(true);

// users event list     
bar.addTab(bar.newTab()
    .setTag("event_list")
    .setText(getString(R.string.list_events_header))
    .setTabListener(new TabListener<EventListFragment>(
        this, getString(R.string.list_events_header), EventListFragment.class, null)));

Within the ApplicationActivity, I have an AsyncTask that takes a couple of seconds to load on initial open, and when manually refreshed against the API - which means that I need to make sure I update the ListView on the fragment instantiated above, which I do in the onPostExecute method, here is how I do that:

// update the events fragment
EventListFragment fragment = (EventListFragment) getSupportFragmentManager().findFragmentById(R.string.list_events_header);
if(fragment != null) {
    // restart the loader for this fragment, refreshing the listview
    getSupportLoaderManager().restartLoader(0, null, fragment);
}

THIS ALL WORKS

Now, I wanted to put in a TabsAdapter to get the fancy swiping tabs, which I've done, and it works, but the last part I mentioned about the onPostExecute, doesnt work :(

This is my TabsAdapter:

package com.lateral.app.ui;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;

public class APPTabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

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

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

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

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

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

This is my updated onCreate method from the ApplicationActivity

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);

setContentView(mViewPager);

ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(false);
bar.setDisplayShowTitleEnabled(true);

mTabsAdapter = new APPTabsAdapter(this, mViewPager);

// users event list     
mTabsAdapter.addTab(bar.newTab()
    .setTag("event_list")
        .setText(getString(R.string.list_events_header))
        .setTabListener(new TabListener<EventListFragment>(
            this, getString(R.string.list_events_header), EventListFragment.class, null)), EventListFragment.class, null);

And this is the update I made to my onPostExecute method in the ApplicationActivity

// update the events fragment
EventListFragment fragment = (EventListFragment) mTabsAdapter.getItem(0);
if(fragment != null) {
    // restart the loader for this fragment, refreshing the listview
    getSupportLoaderManager().restartLoader(0, null, fragment);
}

Basically, when it attempts to run my onPostExecute code

I get a NullPointerException from my cursorAdapter within the fragment.. but it found the fragment..

EDIT -- Requested Code

public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    Log.d(TAG, "onCreateLoader");

    return new EventCursorLoader(getActivity());
}

public static final class EventCursorLoader extends SimpleCursorLoader {

    Context mContext;

    public EventCursorLoader(Context context) {
        super(context);

        Log.d("EventCursorLoader", "Constructor");

        mContext = context;
    }

    @Override
    public Cursor loadInBackground() {
        Log.d("EventCursorLoader", "loadInBackground");

        EventsDataSource datasource = new EventsDataSource(mContext, ((ApplicationActivity)mContext).getDbHelper());

        SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        long userId = app_preferences.getLong("authenticated_user_id", 0);

        return datasource.getAllEvents(userId);
    }
}

回答1:

Attempting to retrieve the loader here is simply the wrong idea, as the purpose is to refresh the data. After doing some digging I found that I can notify more than 1 set of content load in my provider using

getContext().getContentResolver().notifyChange(uri, null);

which notifies it of change and refreshes, all this code can be deleted and replaced with a single line.



回答2:

If you're still using the Activity's LoaderManager to manage your Fragments loaders... don't do that. The Activity's LoaderManager manages loaders across your Activity's* lifecycle... not your Fragment's. Your Fragment is probably trying to access a Loader that the Activity has not initialized yet.



回答3:

I had the same exact problem a couple of days ago, during an hackaton :-)

It turns out that getItem() does not return the fragment you created, but instantiate a new one. Basically that is the method that get called to initially create the fragments. That's why you are finding its member empty.

I was a bit in hurry, but I think with that solution there is no way to access the fragments from the activity.

However, the workaround that worked for me was to set a reference to the fragments in the activity when they were created.

Something like

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((ApplicationActivity)getActivity()).setEventListFragment(this);
}

inside your fragment and

EventListFragment getEventListFragment(){
    return mEventListFragment;
}

public void setEventListFragment(EventListFragment m){
    mEventListFragment = m;
}

inside your activity.

You should then use getEventListFragment() instead of using the getItem() method of the adapter.

If you want to see the whole code I wrote (and especially the part you need), you can check it here

https://github.com/moodeque/moodeque-android/tree/master/src/main/java/com/whiterabbit/hackitaly/Activities

The container activity is InVenueActivity , whereas the two fragments are those contained in the tabs.