[英]How to pass data between two fragments without any buttons?
I have two fragments between which I move with bottom navigation. 我在底部导航之间移动了两个片段。 I want to pass between them data without any buttons.
我想在它们之间传递没有任何按钮的数据。 It looks like this: In first one fragment I have string in edit text, then I'm going to another fragment and I want pass this string to the text view in that fragment.
看起来是这样的:在第一个片段中,我在编辑文本中包含字符串,然后转到另一个片段,我想将此字符串传递给该片段中的文本视图。
The First fragment: 第一个片段:
public class TodayFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FragmentTodayBinding fragmentTodayBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_today, null, false);
View view = fragmentTodayBinding.getRoot();
TodayViewModel todayViewModel = ViewModelProviders.of(this).get(TodayViewModel.class);
fragmentTodayBinding.setTodayViewModel(todayViewModel);
fragmentTodayBinding.setTodayFragmentInterface(new TodayFragmentInterface() {
@Override
public void onSearchClick(View v) {
todayViewModel.getCity();
}
});
todayViewModel.getWeather().observe(getViewLifecycleOwner(), new Observer<Weather>() {
@Override
public void onChanged(Weather weather) {
fragmentTodayBinding.windTextView.setText("Wind: " + String.valueOf(weather.getWind().getSpeed()) + " km/h");
fragmentTodayBinding.humidityTextView.setText("Humidity: " + weather.getMain().getHumidity() + " %");
fragmentTodayBinding.pressureTextView.setText("Pressure: " + weather.getMain().getPressure() + " mBar");
Double temp = weather.getMain().getTemp();
DecimalFormat df = new DecimalFormat("#0.0");
fragmentTodayBinding.tempTextView.setText(df.format(temp) + " ℃");
String desc = weather.getDescription();
String desc2 = Character.toString(desc.charAt(0)).toUpperCase()+desc.substring(1);
fragmentTodayBinding.conditionTextView.setText(desc2);
Glide.with(TodayFragment.this).load(weather.getIconUrl()).into(fragmentTodayBinding.iconImageView);
}
});
return view;
}
The second fragment: 第二个片段:
public class HourlyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FragmentHourlyBinding fragmentHourlyBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_hourly, null, false);
View view = fragmentHourlyBinding.getRoot();
HourlyViewModel hourlyViewModel = ViewModelProviders.of(this).get(HourlyViewModel.class);
List<String> data = new ArrayList<>();
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
final RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getActivity(), data);
recyclerView.setAdapter(recyclerViewAdapter);
return view;
}
} }
Here is the official docs for how to communicate with fragments. 这是有关如何与片段进行通信的官方文档 。 There are a variety of ways to do this.
有多种方法可以做到这一点。 You can use the Interface approach, you could use an EventBus like GreenRobot or you could use LiveData .
您可以使用接口方法,可以使用像GreenRobot这样的EventBus,也可以使用LiveData 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.