片段中的片段不起作用



>我在另一个片段中制作了一个片段,当我单击列表中的列表项时,它起作用是因为 toast 方法显示消息但新片段没有添加...它显示保持不变。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView T_profile_name = (TextView) view.findViewById(R.id.profile_name);
TextView T_profile_hometown = (TextView) view.findViewById(R.id.profile_hometown);
String user_name = T_profile_name.getText().toString();
String  user_hometown = T_profile_hometown.getText().toString();
String getID = user_id[position];
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("comm_data", getContext().MODE_PRIVATE);
String getid = sharedPreferences.getString("user_id", "");
Toast.makeText(getContext(), user_name+" "+user_hometown+" "+getID+""+getid, Toast.LENGTH_SHORT).show();

//mSocket.emit("message", "getuser");
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(new Chat_box(),"chat_box");
fragmentTransaction.addToBackStack("chat");
fragmentTransaction.commit();

您应该告诉片段管理器将片段添加到某处。 正如@Mike所说,使用FragmentTransaction#add(int,Fragment, String).您的代码应该fragmentTransaction.add(R.id.the_container,new Chat_box(),"chat_box");

最新更新