public class Fragment_shouye extends Fragment { private List<Zixun_shouye> datas; private TopView top; @Override public void onCreate(Bundle savedInstanceState) { top = new TopView(getActivity());//---------------------一定在这儿初始化 接口回调时再去更新ui尽管与主线程不一致也可以 datas = new ArrayList<Zixun_shouye>(); NUtils.get(NUtils.TYPE_TXT, Config.FIRST_PAGE_WEBVIEW, new Callback() { @Override public void response(String url, byte[] bytes) { JSONObject obj; try { obj = new JSONObject(new String(bytes, "utf-8")); JSONArray array = obj.getJSONArray("data"); for (int j = 0; j < array.length(); j++) { JSONObject obj1 = array.getJSONObject(j); String type = obj1.getString("type"); String picurl = obj1.getString("picurl"); String title = obj1.getString("title"); String newsurl = obj1.getString("newsurl"); Zixun_shouye zixun = new Zixun_shouye(); zixun.setNewsurl(newsurl); zixun.setPicurl(picurl); zixun.setTitle(title); zixun.setType(type); System.out.println(zixun); datas.add(zixun); } top.setData(datas); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } @Override public boolean isCancelled(String url) { return false; } }); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return top;//防止返回为空不执行 }
遇到一些自定义类作为布局的可以先用一个linearlayout占位 在用ll.addView(v);可以