欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

JetPack学习总结

程序员文章站 2022-03-10 22:57:09
JetPackNavigation 之Fragment跳转NavController controller = Navigation.findNavController(v);controller.navigate(R.id.action_testHomeFragment_to_testDetailFragment);Navigation graph中直接设置转换动画,极大减少了工作量。当然如果对自带动画效果不满意,也可以创建Animation资源自己编写xml文件来......

JetPack

 

Navigation 之Fragment跳转

JetPack学习总结

 

 

NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_testHomeFragment_to_testDetailFragment);


Navigation graph中直接设置转换动画,极大减少了工作量。当然如果对自带动画效果不满意,也可以创建Animation资源自己编写xml文件来创建动画。

NavHost:一个容器,用于显示

NavController:控制跳转流程

   <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="335dp"
            app:layout_constraintGuide_percent="0.5" />


getView().findViewById(R.id.button2).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_testDetailFragment_to_testHomeFragment));


        NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.action_testHomeFragment_to_testDetailFragment);
 

 

NavHostFragment


public class NavHostFragment extends Fragment implements NavHost {

public interface NavHost {

   clazz = Class.forName(name, true, context.getClassLoader());

Class.forName

    public static final class Builder {
    
          public Builder() {
        }


 @NonNull
        public Builder setLaunchSingleTop(boolean singleTop) {
            mSingleTop = singleTop;
            return this;
        }

  @NonNull
        public NavOptions build() {
            return new NavOptions(mSingleTop, mPopUpTo, mPopUpToInclusive,
                    mEnterAnim, mExitAnim, mPopEnterAnim, mPopExitAnim);
        }
        Builder.build


NavAction  NavArgument  NavOptions


 Method valueMethod = clazz.getDeclaredMethod("values");
 
 clazz.getDeclaredMethod
 
 NavArgument
 
    A annotation = getDeclaredAnnotation(annotationClass);
    getDeclaredAnnotation
    
    
    public class NavController {
    
    
    class NavControllerViewModel extends ViewModel {
    
    
    NavControllerViewModel
    
    
    ViewModel
    

NavControllerViewModel
     @NonNull
    static NavControllerViewModel getInstance(ViewModelStore viewModelStore) {
        ViewModelProvider viewModelProvider = new ViewModelProvider(viewModelStore, FACTORY);
        return viewModelProvider.get(NavControllerViewModel.class);
    }

    
private static final ViewModelProvider.Factory FACTORY = new ViewModelProvider.Factory() {
        @NonNull
        @Override
        @SuppressWarnings("unchecked")
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            NavControllerViewModel viewModel = new NavControllerViewModel();
            return (T) viewModel;
        }
    };

     
     public class ViewModelProvider {
     
         public interface Factory {
    
           @NonNull
        <T extends ViewModel> T create(@NonNull Class<T> modelClass);
        }
        
        }
        
            abstract static class KeyedFactory implements Factory {
         @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            throw new UnsupportedOperationException("create(String, Class<?>) must be called on "
                    + "implementaions of KeyedFactory");
        }
        }
    
        public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
        mFactory = factory;
        }
        
            public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
            
                  viewModel = ((KeyedFactory) (mFactory)).create(key, modelClass);
                  }
            

 

 

 

 

本文地址:https://blog.csdn.net/u014644594/article/details/107167788