Android Studio 之 Navigation【2.数据的传递】 - 孤峰皓月
Android Studio 之 Navigation【2.数据的传递和过渡动画】
在资源navigation资源的xml文件中,在【目标界面】 detialFragment中点击,在右边 Arguments 中添加参数 name=李江南
添加这个name参数后,在箭头 Action 上点击,会在右边的 Argument 中显示这个name变量
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);
}
});
}
动态传递参数
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中接收参数
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); } } }