| | |
| | | /** |
| | | * Created by Administrator on 2021/10/28 0028. |
| | | */ |
| | | public abstract class BaseFragment<A extends BaseActivity,B extends ViewBinding> extends Fragment { |
| | | public abstract class BaseFragment<B extends ViewBinding> extends Fragment { |
| | | |
| | | protected A activity; |
| | | protected BaseActivity activity; |
| | | protected B binding; |
| | | |
| | | @Nullable |
| | | @Override |
| | | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { |
| | | // get genericity "B" |
| | | Class<B> entityClass = (Class<B>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; |
| | | Class<B> entityClass = (Class<B>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
| | | try { |
| | | Method method = entityClass.getMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);//get method from name "inflate"; |
| | | binding = (B) method.invoke(entityClass,inflater,container,false);//execute method to create a objct of viewbind; |
| | |
| | | @Override |
| | | public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| | | super.onActivityCreated(savedInstanceState); |
| | | activity = (A) getActivity(); |
| | | activity = (BaseActivity) getActivity(); |
| | | initViews(); |
| | | } |
| | | |