我在另一个片段中创建了一个片段,当我点击列表中的列表项时,它起作用,因为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();
答案 0 :(得分:3)
您应该告诉片段管理器在某处添加片段。正如@Mike所说,使用FragmentTransaction#add(int,Fragment, String)
。您的代码应为fragmentTransaction.add(R.id.the_container,new Chat_box(),"chat_box");