简单抽取BaseFragment

package com.mjd.imitate_jd.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;

public abstract class BaseFragment extends Fragment {
    private ImmersionBar mImmersionBar;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(privideLayoutId(), container, false);
        initViews(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();   //所有子类都将继承这些相同的属性
        initData();
        initListener();
    }

    protected abstract void initListener();

    protected abstract void initData();

    protected abstract void initViews(View view);

    protected abstract int privideLayoutId();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }
}

 

使用

 

package com.mjd.imitate_jd.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.mjd.imitate_jd.R;
import com.mjd.imitate_jd.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private SimpleDraweeView home__sdv_sao;
    private ImageView home__iv_sou;
    private EditText home__et_message;
    private ImageView home__iv_ren;
    private SimpleDraweeView home_sdv_message;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews(View view) {
        recyclerView=view.findViewById(R.id.home_recyler_zs);
        home__sdv_sao=view.findViewById(R.id.home__sdv_sao);
        home__iv_sou=view.findViewById(R.id.home__iv_sou);
        home__et_message=view.findViewById(R.id.home__et_message);
        home__iv_ren=view.findViewById(R.id.home__iv_ren);
        home_sdv_message=view.findViewById(R.id.home_sdv_message);
    }

    @Override
    protected int privideLayoutId() {
        return R.layout.fragment_home;
    }

}

 

你可能感兴趣的:(简单抽取BaseFragment)