懒加载

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.bt1)
    RadioButton bt1;
    @BindView(R.id.bt2)
    RadioButton bt2;
    @BindView(R.id.bt3)
    RadioButton bt3;
    @BindView(R.id.bt4)
    RadioButton bt4;

    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.bt5)
    RadioButton bt5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //数据

        final ArrayList<Fragment> fragments = new ArrayList<>();
        Frag1 frag1 = new Frag1();
        Frag2 frag2 = new Frag2();
        Frag3 frag3 = new Frag3();
        Frag4 frag4 = new Frag4();
        Frag5 frag5 = new Frag5();
        fragments.add(frag1);
        fragments.add(frag2);
        fragments.add(frag3);
        fragments.add(frag4);
        fragments.add(frag5);
        //适配器
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        //默认选中
        rg.check(rg.getChildAt(0).getId());


        //滑动页面改变监听
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                rg.check(rg.getChildAt(i).getId());

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        //点击按钮页面改变监听
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.bt1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.bt2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.bt3:
                        vp.setCurrentItem(2);
                        break;
                    case R.id.bt4:
                        vp.setCurrentItem(3);
                        break;
                    case R.id.bt5:
                        vp.setCurrentItem(4);
                        break;
                }
            }
        });









    }
}




//懒加载基类

public abstract class BaseFragment extends Fragment {
    private boolean isViewCreated;//视图是否已经创建
    private boolean isUiVisible;//该fragment是否对用户可见

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(setContentView(), null, false);
        initView(view);
        return view;
    }



    protected abstract int setContentView();

    protected abstract void initView(View view);


    //创建视图
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        lazyLoad();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isUiVisible = true;
            lazyLoad();
        }else{
            isUiVisible = false;
        }
    }

    private void lazyLoad() {
        //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,
        // 并且会在onCreateView执行前回调,必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUiVisible) {
            lazyLoadData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUiVisible = false;

        }
    }

    protected abstract void lazyLoadData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isViewCreated = false;
        isUiVisible = false;
    }

}



public class Frag1 extends BaseFragment implements NewsView {


    @BindView(R.id.rlv)
    RecyclerView rlv;
    @BindView(R.id.srf)
    SwipeRefreshLayout srf;
    private Handler handler=new Handler();
     CompositeDisposable compositeDisposable;
    int page = 1;
    private NewsPresenter newsPresenter;
    private ArrayList<NewsBean.DataBean> list;
    private boolean disposed;

    @Override
    protected int setContentView() {
        return R.layout.frag1;
    }

    @Override
    protected void initView(View view) {
        rlv = view.findViewById(R.id.rlv);
        rlv.setLayoutManager(new LinearLayoutManager(getActivity()));
        //禁止滑动
       /* rlv.setLayoutManager(new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });*/
        //分割线
        rlv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

    }

    @Override
    protected void lazyLoadData() {
        Log.d("frag", "frag1");
        newsPresenter = new NewsPresenter(this);
        newsPresenter.attachView(this);
      /*  //下拉刷新的圆圈是否显示
        srf.setRefreshing(false);
*/
        //设置下拉时圆圈的颜色(可以由多种颜色拼成)
        srf.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light);

     /*   //设置下拉时圆圈的背景颜色(这里设置成白色)
        srf.setProgressBackgroundColorSchemeResource(android.R.color.white);*/



        //自带下拉刷新
        srf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        newsPresenter.related(page);
                        srf.setRefreshing(false);
                    }
                },3000);


            }
        });

        //上拉加载
        rlv.addOnScrollListener(new onLoadMoreListener() {
            @Override
            protected void onLoading(int countItem, int lastItem) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        newsPresenter.related(page);
                    }
                },3000);
            }
        });




        newsPresenter.related(page);


    }

    @Override
    public void onNewsData(List<NewsBean> newsBeans) {

        //设置适配器
        List<NewsBean.DataBean> data = newsBeans.get(0).getData();

        if (page==1){
            list = new ArrayList<NewsBean.DataBean>();
        }
        list.addAll(data);

        MyNewsAdapter myNewsAdapter = new MyNewsAdapter(getActivity(), list);
        rlv.setAdapter(myNewsAdapter);
        rlv.scrollToPosition(list.size()-data.size());
    }

   /* @Override
    public void getCompositeDisposable(CompositeDisposable compositeDisposable) {

        disposed = compositeDisposable.isDisposed();
    }*/

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        newsPresenter.dettach();
        Log.d("kkkk", "解绑了");
        //取消订阅
    //判断是否解除订阅

/*

            if (!disposed){
                //消除订阅
                compositeDisposable.clear();

                //解除订阅
                compositeDisposable.dispose();
                Log.i("lalla","解绑了");


            }
*/


    }


}

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值