Navigation 组件(二) Fragment 跳转带参数,动画

1.创建 HomeFragment

  1.1 HomeFragment.java

public class HomeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EditText editText = view.findViewById(R.id.editText);
        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String string = editText.getText().toString().trim();
                Bundle bundle = new Bundle();
                bundle.putString("my_name", string);
                NavController controller = Navigation.findNavController(view);
                controller.navigate(R.id.action_homeFragment_to_detailFragment, bundle);
            }
        });
    }
}

  1.2 fragment_home.xml




    

2. 创建 DetailFragment

  2.1 DetailFragment.java

public class DetailFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String string = getArguments().getString("name");
        String string2 = getArguments().getString("my_name");
        TextView textView = getView().findViewById(R.id.textView);
        textView.setText(string2);
    }
}

  2.2 fragment_detail.xml




    

3. 创建 my_navigation.xml



    
        
            
        
    
    
        
    

4. MainActivity

  4.1 MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

  4.2 activity_main.xml




    

5. 动画文件 anim

  5.1 slide_from_left.xml



    
    

  5.2 slide_to_right.xml



    
    

  5.3 scale_rotation.xml



    
    

你可能感兴趣的:(Jetpack,Java,Android,Java,Jetpack)