FragmentPickFloor.java
......
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnScan:
if(spinFloor.performClick() && spinPartFloor.performClick()) {
extras = new Bundle();
spinFloorValue = spinFloor.getSelectedItem().toString();
spinPartFloorValue = spinPartFloor.getSelectedItem().toString();
extras.putString(Constants.FLOOR_KEY, spinFloorValue);
extras.putString(Constants.FLOOR_PART_KEY, spinPartFloorValue);
intent = new Intent(getContext(), MainActivity.class);
intent.putExtras(extras);
startActivity(intent);
FragmentCreator.fragmentTransactionSetup(new FragmentFloor(), getFragmentManager(), FragmentFloor.class.getSimpleName());
}
break;
}
}
FragmentCreator.java
public static void fragmentTransactionSetup(Fragment fragment,
FragmentManager fragmentManager,
String tag) {
fragmentManager.executePendingTransactions();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
FragmentFloor.java
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_floor, container, false);
}
TouchView.java
public TouchView(Context context, AttributeSet attrs) {
super(context, attrs);
extras = ((MainActivity) getContext()).getIntent().getExtras();
spinFloorValue = extras.getString(Constants.FLOOR_KEY);
spinPartFloorValue = extras.getString(Constants.FLOOR_PART_KEY);
}
TouchView在FragmentPickFloor中从微调器中选择的依赖关系值中显示不同的背景。
答案 0 :(得分:0)
间接是的。您必须使用片段setArguments()
方法将bundle参数作为参数传递给片段,然后将数据传递到onCreateView()
中的自定义视图。
创建Fragment时,使用setArguments()方法传递值。
FragmentFloor fragment = new FragmentFloor();
Bundle args = new Bundle();
args.putString(Constants.FLOOR_KEY, spinFloorValue);
args.putString(Constants.FLOOR_PART_KEY, spinPartFloorValue);
fragment.setArguments(args);
然后在你的片段......
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_floor, container, false);
TouchView touchView = (TouchView)view.findViewById(R.id.touchView);
touchView.setSpinFloorValue(getArguments().getString(Constants.FLOOR_KEY));
touchView.setSpinPartFloorValue(getArguments().getString(Constants.FLOOR_PART_KEY));
return view;
}
或者,您可以尝试使用getActivity().getIntent()
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_floor, container, false);
TouchView touchView = (TouchView)view.findViewById(R.id.touchView);
extras = ((MainActivity) getContext()).getIntent().getExtras();
touchView.setSpinFloorValue(extras.getString(Constants.FLOOR_KEY));
touchView.setSpinPartFloorValue(extras.getString(Constants.FLOOR_PART_KEY));
return view;
}