2

我有一个带有列表的片段(YearsFragment)。如果我单击一个项目,则会执行一个新片段(EventsFragment)。

新片段的加载器加载一个新列表。这行得通。

现在我的问题:当我回到第一个 YearsFragment 并单击同一项目时,EventsFragment 再次加载。但我会用旧数据获取旧加载器。

活动活动:

public class EventsActivity extends FragmentActivity implements YearsFragment.OnYearSelectedListener, EventsFragment.OnEventSelectedListener{

private boolean screenSizeLarge;
private EventsFragment eventFrag = null;

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.years_events);

    // Check whether the activity is using the layout version with the fragment_container FrameLayout (small Display). 
    if (findViewById(R.id.fragment_container) != null) { 
        screenSizeLarge = false; //one-pane

        YearsFragment firstFragment = new YearsFragment();

        firstFragment.setArguments(getIntent().getExtras());

        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
    }else{
        screenSizeLarge = true; //two-pane      
    }
}

@Override
public void onYearSelected(Years year) {
    // The user selected the year from the YearsFragment

    if (screenSizeLarge) {
        // two-pane layout

        // Capture the events fragment from the activity layout
        eventFrag = (EventsFragment) getSupportFragmentManager().findFragmentById(R.id.event_fragment);

        // Call a method in the EventFragment to update its content
        eventFrag.updateEventsView(year);

    } else {
        //one-pane layout

        if(eventFrag == null){
            eventFrag = new EventsFragment();
        }

        Bundle args = new Bundle();
        args.putInt("year", Integer.parseInt(year.getYear()));
        eventFrag.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, eventFrag);
        transaction.addToBackStack(null);
        // Commit the transaction
        transaction.commit();
    }
}}

岁月片段:

public class YearsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Years>>{
YearsAdapter adapter;

// The container Activity must implement this interface so the frag can deliver messages
OnYearSelectedListener mCallback;
public interface OnYearSelectedListener {
    /** Called by YearsFragment when a list item is selected */
    public void onYearSelected(Years year);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}


@Override
public void onResume() {
    super.onResume();
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    adapter = new YearsAdapter(getActivity());
    setListAdapter(adapter);

    setEmptyText("No Data Here");
    setListShown(false);

    getLoaderManager().initLoader(0, null, this);
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);

    try{
        mCallback = (OnYearSelectedListener) activity;
    } catch (ClassCastException e){
        throw new ClassCastException(activity.toString() + 
                "must implement OnYearSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    mCallback.onYearSelected(adapter.getItem(position));
}

//implements Loader

@Override
public Loader<List<Years>> onCreateLoader(int arg0, Bundle arg1) {
    Log.i("YearsFragment","YearsFragment.onCreateLoader");

    return new YearsLoader(getActivity());
}

@Override
public void onLoadFinished(Loader<List<Years>> arg0, List<Years> data) {
    Log.i("YearsFragment","YearsFragment.onLoadFinished");

    adapter.setData(data);

    if(isResumed()){
        setListShown(true);
    }else{
        setListShownNoAnimation(false);
    }
}

@Override
public void onLoaderReset(Loader<List<Years>> arg0) {
    Log.i("YearsFragment","YearsFragment.onLoaderReset");

    adapter.setData(null);
}}

事件片段:

public class EventsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Events>>{
EventsAdapter adapter;
private int year;

public EventsFragment(){
    year = Calendar.getInstance().get(Calendar.YEAR);
}

// The container Activity must implement this interface so the frag can deliver messages
OnEventSelectedListener mCallback;
public interface OnEventSelectedListener {
    /** Called by YearsFragment when a list item is selected */
    public void onEventSelected(Events event);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    Log.i("EventsFragment","EventsFragment.onActivityCreated");

    adapter = new EventsAdapter(getActivity());
    setListAdapter(adapter);

    setEmptyText("No Data Here");
    setListShown(false);
}

@Override
public void onResume() {
    super.onResume();
    Bundle args = this.getArguments();
    if(args != null){
        year = args.getInt("year");
    }
    updateEventsView(year);
    // The activity has become visible (it is now "resumed").
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);

    try{
        mCallback = (OnEventSelectedListener) activity;
    } catch (ClassCastException e){
        throw new ClassCastException(activity.toString() + 
                "must implement OnEventsSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    mCallback.onEventSelected(adapter.getItem(position));
}

public void updateEventsView(Years year) {
    this.year = Integer.parseInt(year.getYear());
    getLoaderManager().initLoader(this.year, null, this);
}

public void updateEventsView(int year) {
    this.year = year;
    getLoaderManager().initLoader(this.year, null, this);
}

//Loader
@Override
public Loader<List<Events>> onCreateLoader(int arg0, Bundle arg1) {
    Log.i("EventsFragment","EventsFragment.onCreateLoader");

    return new EventsLoader(getActivity(),this.year);
}

@Override
public void onLoadFinished(Loader<List<Events>> arg0, List<Events> data) {
    Log.i("EventsFragment","EventsFragment.onLoadFinished");

    adapter.setData(data);

    if(isResumed()){
        setListShown(true);
    }else{
        setListShownNoAnimation(false);
    }
}

@Override
public void onLoaderReset(Loader<List<Events>> arg0) {
    Log.i("EventsFragment","EventsFragment.onLoaderReset");

    adapter.setData(null);
} }

我应该怎么做简历?

问候Mr.P。

4

0 回答 0