File was renamed from app/src/main/java/com/duqing/missions/base/BaseFragment.java |
| | |
| | | package com.duqing.missions.base; |
| | | package com.duqing.missions.base.fragments; |
| | | |
| | | import android.os.Bundle; |
| | | import android.view.LayoutInflater; |
| | |
| | | |
| | | import androidx.annotation.Nullable; |
| | | import androidx.fragment.app.Fragment; |
| | | import androidx.lifecycle.ViewModel; |
| | | import androidx.lifecycle.ViewModelProvider; |
| | | import androidx.viewbinding.ViewBinding; |
| | | |
| | | import com.duqing.missions.base.activities.BaseActivity; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.lang.reflect.ParameterizedType; |
| | | |
| | | /** |
| | | * fragment 封装 |
| | | * Created by Administrator on 2021/10/28 0028. |
| | | */ |
| | | public abstract class BaseFragment<B extends ViewBinding> extends Fragment { |
| | | public abstract class BaseFragment<B extends ViewBinding,VM extends ViewModel> extends Fragment { |
| | | |
| | | protected BaseActivity activity; |
| | | protected BaseActivity activity; |
| | | protected B binding; |
| | | protected VM viewModel; |
| | | |
| | | @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()[0]; |
| | | try { |
| | | final ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); |
| | | Class<B> entityClass = (Class<B>) type.getActualTypeArguments()[0]; |
| | | 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; |
| | | Class<VM> vmClass = (Class<VM>) type.getActualTypeArguments()[1]; |
| | | viewModel = new ViewModelProvider(this).get(vmClass); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |