简体   繁体   English

片段内容在我切换并再次选择之前不会显示

[英]Fragment content does not show up until I switch it and select it again

I've got a problem with my Android app: my fragment content doesn't show up until I switch it and I select it again. 我的Android应用程序出现问题:我的片段内容直到我切换并再次选择后才显示。 What should load just at the fragment start is a list of Cards. 仅在片段开始处加载的是卡列表。

Here you got the onCreate method of the Fragment that loads everything: 在这里,您获得了Fragment的onCreate方法,该方法可以加载所有内容:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bundle = this.getArguments();
    debts = bundle.getParcelableArrayList("list");
    adapter =  new CardAdapter(getActivity(), android.R.color.holo_blue_dark);

    for (int i=0; i<debts.size();++i) {
        Debt d = debts.get(i);
        Card card = new Card(d.getSecondUser(),d.getSubject());
        adapter.add(card);
    }


}

And here the onActivityCreated method where i set the adapter for the list: 在这里的onActivityCreated方法中,我为列表设置了适配器:

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

    CardListView list = (CardListView) getView().findViewById(R.id.newCard);
    list.setCardTheme(CardTheme.Light);
    list.setAdapter(adapter);
}

It works if I use an ArrayList of Cards already loaded, but not this way. 如果我使用已经加载的卡的ArrayList,它会起作用,但不是这样。 I tried nearly everything... thank you very much. 我几乎尝试了所有东西...非常感谢。


UPDATE: Even I tried with the solutions you gave me, it keeps happening, my code now: 更新:即使我尝试了您提供给我的解决方案,它也在不断发生,现在我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bundle = this.getArguments();
    debts = bundle.getParcelableArrayList("list");
    adapter =  new CardAdapter(getActivity(), android.R.color.holo_blue_dark);

    for (int i=0; i<debts.size();++i) {
        Debt d = debts.get(i);
        Card card = new Card(d.getSecondUser(),d.getSubject());
        adapter.add(card);
    }


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_debts, container,
            false);

    CardListView list = (CardListView) rootView.findViewById(R.id.newCard);
    list.setCardTheme(CardTheme.Light);
    list.setAdapter(adapter);

    return rootView;
}

You should initialize your cardListView inside the onCreateView() method. 您应该在onCreateView()方法中初始化cardListView。 onCreateView() gets called before the onActivityCreated() method. 在onActivityCreated()方法之前调用onCreateView()。 onCreateView() should be the point when the view of your fragment is initialized. onCreateView()应该是片段视图初始化时的关键点。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

    //use your layout file instead of R.layout.fragment_main
    View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

    CardListView list = rootView.findViewById(R.id.newCard);
    list.setCardTheme(CardTheme.Light);
    list.setAdapter(adapter);
}

Please take a look at the lifecycle of Fragments . 请查看Fragments的生命周期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM