本帖最后由 heaimnmn 于 2014-07-31 18:24:38 编辑

解决方案 »

  1.   

    如果fragment的数据都不一样,请求N次有什么问题?
      

  2.   

    但是可以在MainActivity里面一次性请求好,何必再重复请求了,,,
      

  3.   

    可以考虑在new fragment的时候设置给他
      

  4.   

    activity和fragment是独立的,activity可以用异步线程去做些事情的,你可以看下SyncTask类。
      

  5.   

    不懂你的意思,fragment一开始在oncreate就是就要生成了,怎么再去new了,,,
      

  6.   

    可以考虑在new fragment的时候设置给他不懂你的意思,fragment一开始在oncreate就是就要生成了,怎么再去new了,,,
      

  7.   

    回复错了,fragment的oncreateview的在oncreateactivity之前,你怎么后面去异步了???
      

  8.   

    可以考虑在new fragment的时候设置给他不懂你的意思,fragment一开始在oncreate就是就要生成了,怎么再去new了,,,
    我其实不是很清楚你的界面,我用fragment不是很多,只在和viewpager一起用过。
    使用fragment都是new fragment,在activity的oncreate时请求数据,一个菊花转,获得数据后再给fragment设置数据在fragment里面都有一个setData方法
      

  9.   

    不知道理解可对:
    在Fragment可以提前初始化 onCreateView()这个方法
    onCreateView(){
       //此时还没有数据,所以显示 数据正在加载中 showLoadingView()吧
       return view;
    }public void onUpdateView(JSONDATA data){
       //现在UI线程中更新 之前Fragment还在显示loadingView,
    }class MainActivity {
          //activty中做异步请求数据,如果请求成功 调用 Fragment中自己写的onUpdateView()方法.
       
    }//但我到喜欢把网络数据请求都写在service里面,然后网络获取数据结果通过广播回调
    Intent intent = new Intent(this,MyService.class); //这个Service 就是专门请求网络数据的。
    intent.putString("reqURL",myURL);
    intent.putString("params",params);
    startService(intent);在onStartCommand() 里面通过判断 进行网络通信操作。// service 里面为了避免多个线程浪费 我开启了一个线程池,大小为3个估计是够了。然后用阻塞队列控制。LinkedBlockingQueue.
      

  10.   

    是activity 先创建 后再oncreateView的吧。。
      

  11.   

    楼主,建议你网络或耗时操作,都放到新的线程中处理。处理完后通知界面分两种方式:一,尝试postInvalidate;二,保存标志位到数据库或文件中。
    一:把view的引用传入后台线程,网络操作完毕后view.postInvalidate()来通知更新。这时候view可能已经不存在了,要注意catch exception,另外考虑加上下面的第二种方法。二:保存view需刷新的标志到数据库或文件中,每次view重绘的时候,检查一下标识看看是否有新数据,有则加载。
      

  12.   

    1.getSupportFragmentManager().findFragmentById/ByTag
    2.MainActivty里面放个list当fragment初始化后添加到list,网络请求返回数据后取出调用fragment方法刷新
      

  13.   

    这是我以前写的一个简化版的BaseFragment,至于异步获取,本菜觉得放哪无所谓吧,可能我没明白lz的意思。直接在Activity中获取数据,更新Fragment可以,或者在BaseFragment中加个abstract   Update 方法,在异步获取之后调用Update也可以。
    package Fragment;import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.example.cs.MyApplication;
    import com.google.gson.Gson;
    import java.lang.reflect.Field;public abstract class BaseFragment extends Fragment{
        protected View view;//Fragment  layout  最外层  View
        protected Context context = MyApplication.getApplication();
        protected final static Gson gson = new Gson();//用来解析json用
        protected String json_str;//传递的json字符串
        protected static final String json_key = "json";
        boolean should_new_newview = true;    public BaseFragment() {
            themeob.addObserver(this);
        } //这个方法可以在Activity中直接获取Fragment的View
        public final View findViewById(int id) {
            return view.findViewById(id);
        }    public final View getView() {
            return view;
        }    @Override
        public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (view == null || should_new_newview)
            {
                view = monCreateView(inflater, container, savedInstanceState);
            }
            return view;
        }    @Override
        public void onDestroyView() {
            super.onDestroyView();
            should_new_newview = true;
        }    @Override
        public void onDestroy() {
            super.onDestroy();
            themeob.deleteObserver(this);
        }
    //调用此方法更新保存的数据
        public final void msetArguments(Bundle bundle) {
            try {
                Field mArguments_field = getClass().getDeclaredField("mArguments");
                mArguments_field.setAccessible(true);
                mArguments_field.set(this, bundle);
            } catch (Exception e) {
            }
        }    public abstract View monCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState);
    }
      

  14.   

    不知道是否理解对了你的意思,你的网络请求的那次数据都是一样的,那样的话你在第一个fragment 请求到数据的时候直接将数据放到一个 静态变量好了。然后 每个fragment  oncreate的时候先判断 是否有这个值,没有就请求,有就不用管好了。
      

  15.   

    fragment依附在ativity中?那么activity就拥有fragment的实例,然后在activity中请求,然后用那个实例更新界面
      

  16.   

    这个主意不错,先不加载view,先显示等待画面,,,
      

  17.   

    问题json是会改变的,所以无法统一管理,这点让我很蛋疼,,,
      

  18.   

    大神,update是否可以重新更新界面view,但是好像用户体验性不好,比如你进入一个fragment,显示了一个默认的界面,过了一会数据更新好了,又显示一个新的界面,,,
      

  19.   


    直接都是用个jsonObject啊不,我意思的是在启动这个MainActivity的时候才会得到json,才能访问网络,并不能之前访问的呗,传jsonObject倒是一个好主意,只不过还是多次访问网络了,,,
      

  20.   


    直接都是用个jsonObject啊不,我意思的是在启动这个MainActivity的时候才会得到json,才能访问网络,并不能之前访问的呗,传jsonObject倒是一个好主意,只不过还是多次访问网络了,,,这个json保存到application里,然后fragment里请求网络,json没保存的时候请求,已经保存过了,直接get这个json
      

  21.   

    在MainActivity里 new一个JsonObject 。 保存在MainActivity 注意这个变量要是Public的然后把这个值给子Fragment
      

  22.   

    额,你说这个啊?加几个过渡动画就可以吧?nineoldandroids.jar,这个还是你告诉我的。
      

  23.   


    fragment可以监听activity,activity有变化的时候可以通知fragment更新,fragment是可以变化的