自定义fragment抽象父类

BaseFragment.java

public abstract class BaseFragment extends Fragment {

    protected Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = initParent(inflater, container);
        findViews(rootView);
        setListensers();
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initVariable();
    }

    private View initParent(LayoutInflater inflater, ViewGroup container) {
        View rootView = inflater.inflate(R.layout.fragment_base_layout, container, false);
        LinearLayout subCententView = (LinearLayout) rootView.findViewById(R.id.base_sub_fragment_layout);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        View centerView = View.inflate(mContext, setContentView(), null);
        subCententView.addView(centerView, layoutParams);
        return rootView;
    }

    /** * 跳转到某个Activity */
    protected void gotoActivity(Context mContext, Class toActivityClass, Bundle bundle) {
        Intent intent = new Intent(mContext, toActivityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        mContext.startActivity(intent);
        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }

    public void gotoActivityForResult(Context mContext,Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        ((Activity)mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }

    /** * 初始化子类fragment的变量 */
    protected abstract void initVariable();

    /** * 加载子类布局 */
    protected abstract int setContentView();

    /** * 加载控件 */
    protected abstract void findViews(View rootView);

    /** * 设置监听 */
    protected abstract void setListensers();
}

fragment_base_layout.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

    <LinearLayout  android:id="@+id/base_sub_fragment_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" >
    LinearLayout>

RelativeLayout>

动画文件见博客–android activity自定义父类

转载于:https://my.oschina.net/nandecanghai123/blog/1529337

你可能感兴趣的:(移动开发,java)