首页 > 代码库 > Fragment懒加载
Fragment懒加载
package com.bpj.lazyfragment;
import android.support.v4.app.Fragment;
/*
*baseFragment
*/
public class LazyFragment extends Fragment {
/**
* 当前Fragment是否显示:true,显示
*/
public boolean isVisible ;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isVisible = false;
if(getUserVisibleHint()){
isVisible = true;
onVisible();
}
}
protected void onVisible(){
}
}
/*
*子Fragent
*/
package com.bpj.lazyfragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends LazyFragment {
public static Fragment2 newInstance(){
Fragment2 fragment1 = new Fragment2();
return fragment1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2,null);
return view;
}
@Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (this.getView() != null)
this.getView()
.setVisibility(menuVisible ? View.VISIBLE : View.GONE);
}
@Override
protected void onVisible() {
//刷新的方法
System.out.println("Fragment2开始加载。。。");
}
}
Fragment懒加载