com.oldnum7.androidlib.base.BaseFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.oldnum7.androidlib.base.BaseFragment.java

Source

/*
 * Copyright 2016 jeasonlzy(?)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.oldnum7.androidlib.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * ?initData,Lazy?Fragment
 * ??LazyinitData,?initView??
 * -
 * -1: ViewPagersetUserVisibleHint
 * ------?mViewPager.setOffscreenPageLimit(size), viewpager?Fragment
 * -2: FragmentTransactionshowhide?onHiddenChanged.
 * -3: ?showFragment ?onHiddenChanged lazy ?hide?show
 */

/**
 * <Pre>
 * author : denglin
 * time   : 2017/05/31/13:47
 * desc   :The base activity that every activity you create must extent it.
 * version: 1.0
 * </Pre>
 */

public abstract class BaseFragment extends Fragment implements View.OnTouchListener {

    public final String TAG = this.getClass().getSimpleName();

    protected String fragmentTitle; //fragment
    private boolean isVisible; //????
    private boolean isPrepared; //?View???
    private boolean isFirstLoad = true; //?
    protected LayoutInflater inflater;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        isFirstLoad = true;
        View view = initView(inflater, container, savedInstanceState);
        view.setClickable(true);//?!
        isPrepared = true;
        lazyLoad();
        return view;
    }

    /**
     * ViewPagersetUserVisibleHint
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    /**
     * FragmentTransactionshowhide?onHiddenChang ed.
     * ?showFragment ? ?hide?show
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void onVisible() {
        lazyLoad();
    }

    protected void onInvisible() {
    }

    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirstLoad) {
            return;
        }
        isFirstLoad = false;
        initLazyData();
    }

    protected abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    /**
     * viewpager  tab? ....??!!!
     */
    protected void initLazyData() {
    }

    public String getTitle() {
        return TextUtils.isEmpty(fragmentTitle) ? "" : fragmentTitle;
    }

    public void setTitle(String title) {
        fragmentTitle = title;
    }

    public interface Callback {

        void onFragmentAttached();

        void onFragmentDetached(String tag);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}