[Developing a Live APP from 0] 2.1 Fragment for High Level Packaging

Keywords: Fragment

This article is a serial of Jiang Zhibi, the author of the rookie nest. "Develop a Live APP Series from 0" to talk about the most popular live APP, how to complete the realization of a commercial project like "Tencent Live Broadcasting"

[Developing a Live APP from 0] 2.1 Fragment for High Level Packaging
[Developing a Live APP from 0] 2.2 Fragment-Sliding Navigation for High Level Packaging

Fragment package

Similar to Active encapsulation, the main methods are: initialization interface, return operation, data initialization, event monitoring, ActionBar, layout acquisition, control acquisition, data transfer, etc.

Create BaseFragment class, change it to abstract class, inherit Fragment, implement onCreate() method and onCreateView() method

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, savedInstanceState);
}

Initialization interface

protected abstract void initView(View rootView);

Return operation

public void onBackPressed(){}

Data initialization

 protected abstract void initData();

event listeners

protected abstract void setListener();

Rewrite ActionBar

public void initActionBar(Activity activity){}

Get the layout

protected abstract int getLayoutId();

Get control

public<T extends View> T obtainView(int resId){
    return (T) mRootView.findViewById(resId);
}

Data transfer

protected Intent mBundleIntent;
public void setBundleIntent(Intent bundleIntent) {
    mBundleIntent = bundleIntent;
}

Other Fragment callback methods

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onHiddenChanged(boolean hidden) {
    if (hidden){
        mBundleIntent = null;
    }
    super.onHiddenChanged(hidden);
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
}

@Override
public void onPause() {
    super.onPause();
}

Method invocation (note the order of method invocation)

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    if (getLayoutId() != 0){
        mRootView = inflater.inflate(getLayoutId(),container,false);
    }
    initView(mRootView);
    setUserVisibleHint(true);
    setListener();
    initData();

    return mRootView;
}

Merge the code to see the overall effect

public abstract class BaseFragment extends Fragment {

    protected View mRootView;
    protected Intent mBundleIntent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (getLayoutId() != 0){
            mRootView = inflater.inflate(getLayoutId(),container,false);
        }
        initView(mRootView);
        setUserVisibleHint(true);
        setListener();
        initData();

        return mRootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden){
            mBundleIntent = null;
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    /**
     * Rewrite ActionBar
     * @param activity
     */
    public void initActionBar(Activity activity){}

      /**
     * Data transfer
     * @param bundleIntent
     */
    public void setBundleIntent(Intent bundleIntent) {
        mBundleIntent = bundleIntent;
    }

      /**
     * Get control
     * @param resId
     */
    public<T extends View> T obtainView(int resId){
        return (T) mRootView.findViewById(resId);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * Return operation
     */
    public void onBackPressed(){}

    /**
     * Initialization data
     */
    protected abstract void initData();

    /**
     * Setting up monitoring
     */
    protected abstract void setListener();

    /**
     * Initialization interface
     * @param rootView
     */
    protected abstract void initView(View rootView);

    /**
     * Get the layout
     * @return
     */
    protected abstract int getLayoutId();

}

After Fragment is encapsulated, please continue to be curious and pay attention to the blog I've pushed for you.

The next chapter explains the use of Fragment s after encapsulation, but let's write a small Demo for the time being.

For more information, please pay attention to the Bird's Nest (Wechat Public ID: cniao5), an online learning platform for programmed apes. Welcome to join the high-end technology exchange group with lov730. Reprinted please indicate the source, this article is from the rookie nest, link to the original text http://www.cniao5.com/forum/thread/334c57740efe11e797e200163e0230fa

Posted by harlequeen on Sun, 14 Jul 2019 15:27:58 -0700