旋转android屏幕导致ListFragment显示进度指示器

时间:2014-01-27 16:14:53

标签: android android-orientation

在FragmentActivity中我放了一个ListFragment。

当我旋转屏幕时,我在listview下面得到一个沙漏作为图片,它在我退出整个活动之前永远不会消失。

错误与listadapter及其关于显示进度条的setListShown()的内容有关,但我不知道如何修复它。

如果我在创建片段后添加了retainInstance(true):

    GuideListFragment guideListFragment = new GuideListFragment();
    guideListFragment.setRetainInstance(true);

然后如果我只在onCreate期间添加我的初始片段,如果savedInstanceState为null,则沙漏消失,但是我的backstack中分配的所有动画都会失败,并且setRetainInstance的手册明确指出如果使用backstack则不使用此方法。

我该如何解决这个问题?如果有帮助,我附上了代码。

感谢。

enter image description here

public class GuideListActivity extends BaseActivity implements OnItemSelectedListener {

    private GuideAdapter<? extends Identifiable> guideAdapter;

    private List<Guide> guideItems;
    private List<Topic> topicItems;
    private List<SubTopic> subTopicItems;

    private DataManager dataManager;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private static final String GUIDE_FRAGMENT = "GUIDE";
    private static final String TOPIC_FRAGMENT = "TOPIC";
    private static final String SUBTOPIC_FRAGMENT = "SUBTOPIC";

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

        fragmentManager = getSupportFragmentManager();

        dataManager = DatabaseApplication.getInstance().getDataManager();

        addFragment(GUIDE_FRAGMENT, Identifiable.INVALID_ID);
    }

    private void addFragment(@Nonnull final String fragmentTag,
            final long parentId) {

        GuideListFragment guideListFragment = new GuideListFragment();

        if (fragmentTag.equals(GUIDE_FRAGMENT)) {
            guideItems = dataManager.getAllGuides();
            guideAdapter = new GuideAdapter<Guide>(this, guideItems);

        } else if (fragmentTag.equals(TOPIC_FRAGMENT)) {
            topicItems = dataManager.getAllTopicsForGuide(parentId);
            guideAdapter = new GuideAdapter<Topic>(this, topicItems);
            Guide g = dataManager.getGuide(parentId);

        } else if (fragmentTag.equals(SUBTOPIC_FRAGMENT)) {
            subTopicItems = dataManager.getAllSubTopicsForTopic(parentId);
            guideAdapter = new GuideAdapter<SubTopic>(this, subTopicItems);
            Topic t = dataManager.getTopic(parentId);

        } else {
            Log.e(TAG, "addFragment with empty type");
            return;
        }

        guideItems = dataManager.getAllGuides();
        guideAdapter = new GuideAdapter<Guide>(this, guideItems);

        fragmentTransaction = fragmentManager.beginTransaction();

        guideListFragment.setListAdapter(guideAdapter);

        fragmentTransaction.setCustomAnimations(R.anim.fragment_in,
                R.anim.fragment_out, R.anim.fragment_in_backstack, R.anim.fragment_out_backstack);

        fragmentTransaction.add(R.id.guidelist_container, guideListFragment,
                fragmentTag);

        addTransactionToBackStack(fragmentTag);

        fragmentTransaction.commit();
    }

    private void addTransactionToBackStack(@Nonnull final String fragmentTag) {

        Fragment prev = fragmentManager.findFragmentByTag(fragmentTag);
        if (prev != null) {
            fragmentTransaction.remove(prev);
        }
        fragmentTransaction.addToBackStack(fragmentTag);
    }

    @Override
    public void onGuideSelected(long id, @Nonnull final String fragmentTag) {
        if (fragmentTag.equals(GUIDE_FRAGMENT)) {
            addFragment(TOPIC_FRAGMENT, id);
        } else if (fragmentTag.equals(TOPIC_FRAGMENT)) {
            addFragment(SUBTOPIC_FRAGMENT, id);
        }
    }

}

0 个答案:

没有答案