美文网首页
自用BaseFragment

自用BaseFragment

作者: 贝黎明 | 来源:发表于2017-08-07 09:45 被阅读13次
    public abstract class BaseFragment extends Fragment {
    
        private View parentView;
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            parentView = inflater.inflate(getLayoutId(), container, false);
            return parentView;
        }
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            initView(view, savedInstanceState);
            initData();
            onEvent();
        }
    
        //获取布局文件ID
        protected abstract int getLayoutId();
    
        protected abstract void initView(View view, Bundle savedInstanceState);
    
        protected abstract void initData();
    
        protected abstract void onEvent();
    
        public <T extends View> T findView(int resId) {
            return (T) (getView().findViewById(resId));
        }
    
        public BaseActivity getBaseActivity() {
            return (BaseActivity) getActivity();
        }
    
        public View getParentView() {
            return parentView;
        }
    }
    

    相关文章

      网友评论

          本文标题:自用BaseFragment

          本文链接:https://www.haomeiwen.com/subject/hisolxtx.html