【发布时间】:2021-12-28 09:49:35
【问题描述】:
我是 Android 开发的新手。我面临一个问题。我需要你们的帮助。
首先,我正在开发一个类似于 BookMyShow 的应用程序。
我有一个包含片段的活动。该活动包含回收站视图中的时间段列表。
当我点击其中一个时间段时,我会转到新活动,在其中我使用用户名、id、timeslot_selected 等特定详细信息预订节目,一旦他在活动中提交“提交”按钮,它就会出现回到他之前所在的同一个片段,并在片段上显示他的详细信息。最初在回收站视图中没有人,现在用户应该在选定的时间段在回收站视图中看到自己作为图标。
我也有下一个按钮。当用户单击它时,我将片段替换为以下日期。如果用户单击其中一个插槽,他将被重定向到相同的预订活动,并且一旦他提交,他应该返回到前一个片段,其日期与他之前所在的日期相同,并带有更新的他自己的图标。
现在,一旦他在预订活动上单击提交,我就无法在上一个片段中显示更新的图标。你能建议我该怎么做吗?
我尝试分离和附加片段,但无济于事。请帮我解决。提前致谢!
SampleActivity.java:
public class SampleActivity extends AppCompatActivity {
TextView text;
Person person;
String id, name, date;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
Intent intent = getIntent();
id = intent.getStringExtra("id");
name= intent.getStringExtra("name");
date= intent.getStringExtra("date");
person= (Person) getIntent().getSerializableExtra("person");
text= findViewById(R.id.name);
text.setText(name);
Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("name", name);
bundle.putString("date", date);
bundle.putSerializable("Person", person);
FragmentA newFragment = new FragmentA();
newFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrameLayout, newFragment, "main_fragment").commit();
}
FragmentA.java:
public class FragmentA extends Fragment implements View.OnClickListener {
RecyclerView recyclerView;
public FragmentA() {
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new, container, false);
recyclerView = view.findViewById(R.id.slots);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
requestQueue = VolleySingleton.getmInstance(getActivity()).getRequestQueue();
newList = new ArrayList<>();
next = view.findViewById(R.id.nextButton);
timeView = view.findViewById(R.id.timeOn);
dateText= view.findViewById(R.id.date);
Bundle bundle = getArguments();
assert bundle != null;
id= bundle.getString("id");
name = bundle.getString("name");
date = bundle.getString("date");
person = (Person) bundle.getSerializable("Person");
next.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
if (v.getId()== R.id.nextButton) {
replaceFragment();
}
}
private void replaceFragment() {
Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("name", name);
bundle.putString("date", date);
bundle.putSerializable("Person", person);
bundle.putSerializable("hashmap", (Serializable) hashmap);
fm = requireActivity().getSupportFragmentManager();
ft = fm.beginTransaction();
f = new FragmentFirst();
f.setArguments(bundle);
ft.replace(R.id.fragmentLayout, f, "main_fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void refreshData() {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment currentFragment = fragmentManager.findFragmentByTag("main_fragment");
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(currentFragment != null) {
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();
}
}
}
【问题讨论】:
-
使用 onAttach 方法
标签: java android android-fragments android-activity android-recyclerview