80,350
社区成员
发帖
与我相关
我的任务
分享
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Fragment fragment = (Fragment) mFragmentPagerAdapter.instantiateItem(fragment_container,
buttonView.getId());
mFragmentPagerAdapter.setPrimaryItem(fragment_container, 0, fragment);
mFragmentPagerAdapter.finishUpdate(fragment_container);
}
}
private FragmentPagerAdapter mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case R.id.rbtn_see:
return new SeeFragment();
case R.id.rbtn_simg:
return new SimgFragment();
case R.id.rbtn_about:
default:
return new AboutFragment();
}
}
@Override
public int getCount() {
return 3;
}
};
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
/**
* Created by puruidong on 8/15/15.
*/
public class SeeFragment extends LazyFragment {
// 标志位,标志已经初始化完成。
private boolean isPrepared;
private ArrayAdapter<String> adapter;
private View view;
private ListView lvSee;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/*
在这里对Fragment的View进行了缓存,但是没有效果,
在每次切换的时候依然会对ListView进行重新从网络进行加载.
*/
if (view == null) {
view = inflater.inflate(R.layout.see_fragment, null);
lvSee = (ListView) view.findViewById(R.id.lv_see);
adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1);
isPrepared = true;
lazyLoad();
}
// 缓存的rootView需要判断是否已经被加过parent,如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
return view;
}
@Override
protected void lazyLoad() {
if (!isPrepared || !isVisible) {
return;
}
//填充各控件的数据
//下面是网络加载部分
NetworkUtils network = new NetworkUtils(this.getActivity());
if (lvSee == null) {
Log.i("SEE", "******这是空的......SeeFragment6666666666666666666666666666666666666666----------------------------------------");
}
network.getJsonArrayData("url", new NetworkUtils.VolleyResultData<See>() {
@Override
public void onSuccess(List<See> list) {
Log.i("SEE", list.size() + "******6666666666666666666666666666666666666666----------------------------------------");
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != null) {
adapter.add(list.get(i).getValue());
}
}
}
});
lvSee.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
//加载网络数据.
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<RadioGroup
android:id="@+id/rdogop_btm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-6dp"
android:gravity="bottom"
android:orientation="horizontal"
android:background="@android:color/background_dark"
>
<RadioButton
android:id="@+id/rbtn_see"
android:layout_width="0dp"
android:drawableTop="@mipmap/ic_launcher"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="c"
/>
<RadioButton
android:id="@+id/rbtn_simg"
android:layout_width="0dp"
android:drawableTop="@mipmap/ic_launcher"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="b"
/>
<RadioButton
android:id="@+id/rbtn_about"
android:layout_width="0dp"
android:drawableTop="@mipmap/ic_launcher"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="c"
/>
</RadioGroup>
</RelativeLayout>
/**
*
* Fragment数据的缓加载.
*
*
* #{link http://blog.csdn.net/maosidiaoxian/article/details/38300627}
*
* Created by puruidong on 8/16/15.
*/
public abstract class LazyFragment extends Fragment {
protected boolean isVisible;
@Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (this.getView() != null)
this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
}
/**
* 在这里实现Fragment数据的缓加载.
*
* @param isVisibleToUser
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getUserVisibleHint()) {
isVisible = true;
onVisible();
} else {
isVisible = false;
onInvisible();
}
}
protected void onVisible() {
lazyLoad();
}
protected abstract void lazyLoad();
protected void onInvisible() {
}
}
private SeeFragment seeFragment;
private SimgFragment simgFragment;
private AboutFragment aboutFragment;
private FragmentPagerAdapter mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case R.id.rbtn_see:
if (seeFragment == null) {
seeFragment = new SeeFragment();
}
return seeFragment;
case R.id.rbtn_simg:
if (simgFragment == null)
simgFragment = new SimgFragment();
return simgFragment;
case R.id.rbtn_about:
default:
if (aboutFragment == null)
aboutFragment = new AboutFragment();
return aboutFragment;
}
}
@Override
public int getCount() {
return 3;
}
};
SeeFragment更改:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
/**
* Created by puruidong on 8/15/15.
*/
public class SeeFragment extends LazyFragment {
// 标志位,标志已经初始化完成。
private boolean isPrepared;
private ArrayAdapter<String> adapter;
private View view;
private ListView lvSee;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.see_fragment, null);
}
// 缓存的rootView需要判断是否已经被加过parent,如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(lvSee==null) {
lvSee = (ListView) view.findViewById(R.id.lv_see);
}
if(adapter==null) {
adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1);
}
isPrepared = true;
lazyLoad();
}
@Override
protected void lazyLoad() {
if (!isPrepared || !isVisible) {
return;
}
if(adapter.getCount()<=0) {
//填充各控件的数据
NetworkUtils network = new NetworkUtils(this.getActivity());
network.getJsonArrayData("url", new NetworkUtils.VolleyResultData<See>() {
@Override
public void onSuccess(List<See> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != null) {
adapter.add(list.get(i).getValue());
}
}
}
});
lvSee.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
改了两部分:第一是在每次切换的时候,判断是否已经有对象,有对象直接返回,没有才创建;
第二是在Fragment里面将数据初始化放在了onActivityCreated方法里面(当Fragment所在的Activity创建成功时调用,只调用一次),并且在加载数据的地方对adapter进行了判断,如果有值就不在继续执行网络请求,没有值则继续执行。