懶加載的Fragment
package com.luwei.constraintlayoutdemo;
import android.support.v4.app.Fragment;
/**
* Created by Ly on 2017/7/26.
*/
public abstract class LazyFragment extends Fragment {
protected boolean isVisible;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getUserVisibleHint()){
isVisible=true;
onVisible();
}else {
isVisible=false;
onInvisible();
}
}
// 懶加載的方法渔呵,在這個(gè)方法里面為fragment的各個(gè)組件去添加數(shù)據(jù)
protected abstract void lazyLoad();
protected void onVisible(){
lazyLoad();
}
protected void onInvisible(){}
}
放到viewpager的Fragment
package com.luwei.constraintlayoutdemo;
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.ImageView;
/**
* Created by Shinelon on 2017/7/26.
*/
public class TestFragment extends LazyFragment {
private boolean isPrepared;// 標(biāo)志位 標(biāo)志已經(jīng)初始化完成了
private ImageView ivTest;
private String index;
public static TestFragment newInstance(String index) {
Bundle args = new Bundle();
args.putString("index", index);
TestFragment fragment = new TestFragment();
fragment.setArguments(args);
return fragment;
}
@Override
protected void lazyLoad() {
if (isPrepared && isVisible) {
ivTest.setImageResource(R.mipmap.ic_launcher_round);
isPrepared = false;
Log.e("-------------this is", index);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_test, container, false);
isPrepared = true;
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ivTest = (ImageView) view.findViewById(R.id.iv);
index = getArguments().getString("index");
lazyLoad();
}
}
加載的Activity
package com.luwei.constraintlayoutdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Shinelon on 2017/7/26.
*/
public class TestLazyActivity extends AppCompatActivity {
private ViewPager vpTestLazy;
private List<Fragment> list = new ArrayList<>();
private TestViewPagerAdapter testViewPagerAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lazy);
list.add(TestFragment.newInstance("0"));
list.add(TestFragment.newInstance("1"));
list.add(TestFragment.newInstance("2"));
list.add(TestFragment.newInstance("3"));
testViewPagerAdapter = new TestViewPagerAdapter(getSupportFragmentManager(), list);
vpTestLazy = (ViewPager) findViewById(R.id.vp_test_lazy);
vpTestLazy.setAdapter(testViewPagerAdapter);
vpTestLazy.setOffscreenPageLimit(list.size()-1);
}
}
效果: