2 qq 30710615 qq_30710615 于 2016.02.02 11:04 提问

fragmentactivity中了用了办法来解决每次点击后重复加载ui,但是没效,求大神指教

package com.doone.changeverday.ui.fragmrnt;

import java.util.ArrayList;
import java.util.List;

import com.doone.changeverday.R;
import com.doone.changeverday_bean.ShopBean;
import com.doone.changevryday.adpter.ShopAdpter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

public class FragmentShoping extends Fragment {
private View view;
private View rooview;//用來判斷是否重複加載ui
private ListView mListView;
private List mdata = new ArrayList();

@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    if (rooview==null) {
        rooview = inflater.inflate(R.layout.fragment_shop, null);
        mListView = (ListView) rooview.findViewById(R.id.shop_list);
    }
    ViewGroup prent=(ViewGroup)rooview.getParent();
    if (prent!=null) {
        prent.removeView(rooview);
    }

    return rooview;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    addData();
    mListView.setAdapter(new ShopAdpter(mdata, getActivity()));
}

private void addData() {
    for (int i = 0; i < 3; i++) {
        ShopBean bean = new ShopBean();
        bean.setStation("武侯區社區服務站");
        bean.setStart("★★★★★");
        bean.setMap("青羊區東大街16號");
        mdata.add(bean);
    }

}
@Override
public void onDestroyView() {

    super.onDestroyView();
    ((ViewGroup)rooview.getParent()).removeView(rooview);
}

}


1个回答

u010539561
u010539561   2016.02.02 17:16

onActivityCreated方法,每次在Fragment切换回来的时候都会被回调,建议添加一个逻辑判断,是否第一次加载并获取到数据!boolean isFirstLoadData=true;

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!