/** * Copyright 2016 JustWayward Team * <p> * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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.justwayward.reader.base; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.annotation.LayoutRes; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.justwayward.reader.ReaderApplication; import com.justwayward.reader.component.AppComponent; import com.justwayward.reader.view.loadding.CustomDialog; import butterknife.ButterKnife; public abstract class BaseFragment extends Fragment { protected View parentView; protected FragmentActivity activity; protected LayoutInflater inflater; protected Context mContext; private CustomDialog dialog; public abstract @LayoutRes int getLayoutResId(); protected abstract void setupActivityComponent(AppComponent appComponent); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) { parentView = inflater.inflate(getLayoutResId(), container, false); activity = getSupportActivity(); mContext = activity; this.inflater = inflater; return parentView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ButterKnife.bind(this, view); setupActivityComponent(ReaderApplication.getsInstance().getAppComponent()); attachView(); initDatas(); configViews(); } public abstract void attachView(); public abstract void initDatas(); /** * 对各种控件进行设置、适配、填充数据 */ public abstract void configViews(); @Override public void onAttach(Activity activity) { super.onAttach(activity); this.activity = (FragmentActivity) activity; } @Override public void onDetach() { super.onDetach(); this.activity = null; } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.unbind(this); } public FragmentActivity getSupportActivity() { return super.getActivity(); } public Context getApplicationContext() { return this.activity == null ? (getActivity() == null ? null : getActivity() .getApplicationContext()) : this.activity.getApplicationContext(); } protected LayoutInflater getLayoutInflater() { return inflater; } protected View getParentView() { return parentView; } public CustomDialog getDialog() { if (dialog == null) { dialog = CustomDialog.instance(getActivity()); dialog.setCancelable(false); } return dialog; } public void hideDialog() { if (dialog != null) dialog.hide(); } public void showDialog() { getDialog().show(); } public void dismissDialog() { if (dialog != null) { dialog.dismiss(); dialog = null; } } protected void gone(final View... views) { if (views != null && views.length > 0) { for (View view : views) { if (view != null) { view.setVisibility(View.GONE); } } } } protected void visible(final View... views) { if (views != null && views.length > 0) { for (View view : views) { if (view != null) { view.setVisibility(View.VISIBLE); } } } } protected boolean isVisible(View view) { return view.getVisibility() == View.VISIBLE; } }