解决方案 »
- 大家用什么手机去开发Android的?
- Android 同一ImageViw里面根据用户选择需要播放不同的动画
- 新手学习,求推荐书籍
- 重写HorizontalScrollView的问题
- 想实现iphone的屏幕触摸旋转和纹理贴图
- android一对多数据库表结构的dao方法怎么封装
- 新浪微博sso登陆不成。进入oauth......自定义webview要怎么做
- android 调用WebService 运行时出错
- 怎么可以监听到系统的闹铃广播,用com.android.alarmclock.ALARM_ALERT怎么收不到
- 鄙人创建4.0.3 或者 4.0 系统的模拟器的时候,没法创建,现在只能是2.3的和 4.4w的可以创建,其他的都不可以
- eclipse 创建模拟器OK不能点
- 关于安卓蓝牙UUID的问题
我其实不是很清楚你的界面,我用fragment不是很多,只在和viewpager一起用过。
使用fragment都是new fragment,在activity的oncreate时请求数据,一个菊花转,获得数据后再给fragment设置数据在fragment里面都有一个setData方法
在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.
一:把view的引用传入后台线程,网络操作完毕后view.postInvalidate()来通知更新。这时候view可能已经不存在了,要注意catch exception,另外考虑加上下面的第二种方法。二:保存view需刷新的标志到数据库或文件中,每次view重绘的时候,检查一下标识看看是否有新数据,有则加载。
2.MainActivty里面放个list当fragment初始化后添加到list,网络请求返回数据后取出调用fragment方法刷新
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);
}
直接都是用个jsonObject啊不,我意思的是在启动这个MainActivity的时候才会得到json,才能访问网络,并不能之前访问的呗,传jsonObject倒是一个好主意,只不过还是多次访问网络了,,,
直接都是用个jsonObject啊不,我意思的是在启动这个MainActivity的时候才会得到json,才能访问网络,并不能之前访问的呗,传jsonObject倒是一个好主意,只不过还是多次访问网络了,,,这个json保存到application里,然后fragment里请求网络,json没保存的时候请求,已经保存过了,直接get这个json
fragment可以监听activity,activity有变化的时候可以通知fragment更新,fragment是可以变化的