Fragment懒加载
Posted 不积跬步-无以至千里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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懒加载的主要内容,如果未能解决你的问题,请参考以下文章