I've been having this problem for a long time and after hours and hours of trying to find a solution I have decided to try and ask for some advice here.

I have a fragment which is basically a calendar. It consists of a GridView which an Adapter populates (it also highlights days that have data in a SQLite database). The main problem is that when setting this adapter it "freezes" the screen for a few houndred miliseconds and makes the animations not smooth.

An example of an animation not running smoothly is when I click on a Button in the SlidingMenu (jfeinstein10's). When I click a button that replaces the current fragment with the Calendar one, the slide out animation of the SlidingMenu doesn't run smoothly. After a long time of looking for the culprit I've found out that it is when I set the adapter to the GridView (gridview.setAdapter(adapter);)

I have already tried with an AsyncTask with no results.

So my question is how can I set the adapter in a way that doesnt affect the slide in animation that occurs while it's being set?

Some code of the Calendar Fragment for reference:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    context = getActivity();

    month = (GregorianCalendar) GregorianCalendar.getInstance();
    month.setFirstDayOfWeek(GregorianCalendar.MONDAY);
    itemmonth = (GregorianCalendar) month.clone();

    items = new ArrayList<String>();
    adapter = new CalendarAdapter(context, month);

    gridview = (GridView) view.findViewById(R.id.calendarGridView);
    gridview.setAdapter(adapter);

    handler = new Handler();
    handler.post(calendarUpdater);

    title = (TextView) view.findViewById(R.id.currentMonthTextView);
    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

    previous = (Button) view.findViewById(R.id.prevMonthButton);

    previous.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setPreviousMonth();
            refreshCalendar();
        }
    });

    [the code continues but I don't think it's relevant]
}

When I supress the gridview.setAdapter(adapter); line it works smoothly so that must be the problem.

In the MenuFragment:

@Override
public void onClick(View v) {
    Fragment newContent = null;
    switch (v.getId()) {
    case R.id.homeButton:
        newContent = new TabFragment1();
        break;
    case R.id.diaryButton:
        newContent = new TabFragment1();
        break;
    case R.id.calendarButton:
        newContent = new TabFragment2();
        break;
    case R.id.statsButton:
        newContent = new StatsFragment();
        break;
    }

    if (newContent != null)
        switchFragment(newContent);
        updateSelector(v);

}

private void switchFragment(Fragment fragment) {
    if (getActivity() == null)
        return;

    if (getActivity() instanceof Main) {
        Main mainAct = (Main) getActivity();
        mainAct.switchContent(fragment);
    }
}

In the Main activity:

public void switchContent(Fragment fragment) {
    mContent = fragment;
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, fragment)
    .commit();
    getSlidingMenu().showContent();
}

This problem makes the user expirience really bad so please if you have any ideas let me know.

有帮助吗?

解决方案

I have come to a solution. I have overriden the onCreateAnimation method of the fragment and set the adapter inside the onAnimationEnd method of the animation. Like this:

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {

    Animation anim;
    if (enter) {
        anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
    } else {
        anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out);
    }

    anim.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(Animation animation) { 
            gridview.setAdapter(adapter);
        }

        public void onAnimationRepeat(Animation animation) { }

        public void onAnimationStart(Animation animation) { }
    });

    return anim;
}

This way the fragment has a fade in and fade out animation that lasts the same or more than the slide out of the menu, so when the Adapter is set there is not animation running and thus, the animation runs smoothly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top