Android仿今日头条N个fragment懒加载
实现思路:
LazyloadFragment懒加载fragment实现:
public abstract class LazyloadFragment extends Fragment {
protected View rootView;
private boolean isInitView = false;
private boolean isVisible = false;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(setContentView(), container, false);
init();
isInitView = true;
isCanLoadData();
return rootView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
//isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见,获取该标志记录下来
if(isVisibleToUser){
isVisible = true;
isCanLoadData();
}else{
isVisible = false;
}
}
private void isCanLoadData(){
//所以条件是view初始化完成并且对用户可见
if(isInitView && isVisible ){
lazyLoad();
//防止重复加载数据
isInitView = false;
isVisible = false;
}
}
/**
* 加载页面布局文件
* @return
*/
protected abstract int setContentView();
/**
* 让布局中的view与fragment中的变量建立起映射
*/
protected abstract void init();
/**
* 加载要显示的数据
*/
protected abstract void lazyLoad();
}
子fragment加载数据:
public class PageFragment extends LazyloadFragment implements XRecyclerView.LoadingListener {
private CommonAdapter<String> adapter;
private ArrayList<String> datas = new ArrayList<>();
private XRecyclerView recyclerView;
private Handler handler = new Handler();
@Override
public int setContentView() {
return R.layout.fragment_page;
}
@Override
public void init() {
recyclerView = rootView.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new CommonAdapter<String>(getActivity(),R.layout.item,datas) {
@Override
protected void convert(ViewHolder holder, String s, int position) {
}
};
recyclerView.setAdapter(adapter);
recyclerView.setPullRefreshEnabled(true);
recyclerView.setLoadingListener(this);
}
@Override
public void lazyLoad() {
recyclerView.refresh();
}
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.refreshComplete();
for(int i=0;i<10;i++){
datas.add("");
}
adapter.notifyDataSetChanged();
}
},500);
}
@Override
public void onLoadMore() {
}
}
最后Mainactivity代码:
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private String[] topics = new String[]{"推荐","热点","北京","视频","社会","图片"};
private ViewPager viewPager;
private ArrayList<Fragment> fragments = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
init();
}
private void init() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
viewPager.setOffscreenPageLimit(3);
for(int i=0;i<topics.length;i++){
tabLayout.addTab(tabLayout.newTab());
fragments.add(new PageFragment());
}
viewPager.setAdapter(new FmPagerAdapter(fragments,getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
for (int j = 0; j < topics.length; j++) {
tabLayout.getTabAt(j).setText(topics[j]);
}
}
}
坑来了
评论