Fragment配合ViewPager使用,实现懒加载

1、定义抽象类

public abstract class LazyFragment extends Fragment {
    public static final String TAG = "LazyFragment";
    View rootView;
    boolean isViewCreated = false;
    boolean currentVisibleState = false;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG,"onCreateView");
        if(rootView == null){
            rootView = inflater.inflate(getLayoutId(),container,false);
        }
        initView(rootView);
        isViewCreated = true;
        if(getUserVisibleHint()){
            dispatchUserVisibleHint(true);
        }
        return rootView;
    }
    /**
     * 布局id
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化控件
     * @param rootView
     */
    protected abstract void initView(View rootView);

    //判断fragment 是否可见
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG,"setUserVisibleHint");
        if(isViewCreated){
            if(!currentVisibleState && isVisibleToUser){//不可见到可见
                dispatchUserVisibleHint(true);
            }else if(currentVisibleState && !isVisibleToUser){//可见到不可见
                dispatchUserVisibleHint(false);
            }
        }
    }
    //统一的分发可见事件,处理数据
    private void dispatchUserVisibleHint(boolean isVisible){
        Log.i(TAG,"dispatchUserVisibleHint");
        if(currentVisibleState == isVisible){//避免重复加载
            return;
        }
        currentVisibleState = isVisible;
        if(isVisible){
            onDataLoad();
        }else {
            onDataLoadStop();
        }
    }
    //数据加载
    public void onDataLoad(){
        Log.i(TAG,"onDataLoad");
    }
    //停止加载数据
    public void onDataLoadStop(){
        Log.i(TAG,"onDataLoadStop");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG,"onResume");
        if(!currentVisibleState && getUserVisibleHint()){
            dispatchUserVisibleHint(true);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG,"onPause");
        if(currentVisibleState && getUserVisibleHint()){
            dispatchUserVisibleHint(false);
        }
    }
}

2、子类继承抽象类,重写onDataLoad方法实现懒加载,如果不需要实现懒加载则不用重写方法

class MyFragment:LazyFragment() {
    private lateinit var tvTest:TextView
    companion object{
        const val type_key = "type"
        @JvmStatic
        fun getInstance(type:String) :MyFragment{
            val myFragment = MyFragment()
            val bundle = Bundle()
            bundle.putString(type_key,type)
            myFragment.arguments = bundle
            return myFragment
        }
    }
    lateinit var type:String
    /**
     * 布局id
     * @return
     */
    override fun getLayoutId(): Int {
        return R.layout.fragment_layout
    }

    /**
     * 初始化控件
     * @param rootView
     */
    override fun initView(rootView: View?) {
        type = arguments?.get(type_key)?.toString().toString()
        tvTest = rootView?.findViewById(R.id.tv_test)!!
    }

    override fun onDataLoad() {
        super.onDataLoad()
        Log.i(TAG, "fragement$type,开始加载数据")
        tvTest?.text = "fragement$type,数据加载中..."
        Thread(Runnable { mHandler.sendEmptyMessageDelayed(1, 2000) }
        ).start()
    }

    override fun onDataLoadStop() {
        Log.i(TAG, "fragement$type,停止加载数据")
        tv_test.text = "fragement$type,停止加载数据"
        super.onDataLoadStop()
    }
    private val mHandler = MyHandler(WeakReference(this))

    private class MyHandler(val myFragment: WeakReference<MyFragment>) : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            myFragment.get()?.run {
                when (msg.what) {
                    1 -> tvTest.text = "数据加载成功${type}"
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值