Android Studio 之 Navigation【2.數據的傳遞】

Android Studio 之 Navigation【2.數據的傳遞和過渡動畫】java

 

 

 

在資源navigation資源的xml文件中,在【目標界面】 detialFragment中點擊,在右邊 Arguments 中添加參數 name=李江南android

 

 

 

添加這個name參數後,在箭頭 Action 上點擊,會在右邊的 Argument 中顯示這個name變量app

 

 

 

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller = Navigation.findNavController(v);

                //這樣也能夠直接導航到 Detial 界面,直接調用這個界面的話,navigation中設置的參數將傳遞不過來 //這樣直接調用的是 my_nav.xml中 detialFragment 中右邊設置的參數【李江南】 controller.navigate(R.id.detialFragment);

                //能夠將 navigation 中的參數傳遞過來 //這樣調用,會將 箭頭 Action 中設置的參數傳遞過去【張無忌】 //controller.navigate(R.id.action_homeFragment_to_detialFragment);
            }
        });
    }

  

 

動態傳遞參數ide

HomeFragment.java 文件動畫

package com.example.navigationdemo2;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller = Navigation.findNavController(v);

                //動態傳遞參數
                EditText editText = getView().findViewById(R.id.editText);
                String s = editText.getText().toString();
                if(TextUtils.isEmpty(s)){
                    Toast.makeText(getActivity(), "請輸入名字!", Toast.LENGTH_SHORT).show();
                    return;
                }

                //將參數放在 Bundle 中
                Bundle bundle = new Bundle();
                bundle.putString("my_name",s);
                NavController navController = Navigation.findNavController(v);
                controller.navigate(R.id.action_homeFragment_to_detialFragment,bundle);//第2個帶參數
            }
        });
    }
}

  

在 DetialFragment.java中接收參數ui

package com.example.navigationdemo2;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


/**
 * A simple {@link Fragment} subclass.
 */
public class DetialFragment extends Fragment {


    public DetialFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detial, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView textView = getView().findViewById(R.id.textView);
       //獲取參數【這個參數是在 資源navigation中的detial的fragment中右邊參數界面添加的】
        String strName = getArguments().getString("name");

       //獲取動態參數 String my_name = getArguments().getString("my_name");   
        if(!TextUtils.isEmpty(my_name)) {
            textView.setText(my_name);
        }
        else
        {
            textView.setText(strName);
        }

    }
}
相關文章
相關標籤/搜索