0
如果用户单击其中一个列表项目,我一直试图用另一片段替换片段。现在的问题是其他片段正在显示,但它并没有取代以前的帧,但它只是显示在前一个片段的顶部。这是我如何打电话给我的列表项点击新片段:此片段相关用另一个片段替换一个片段时重叠
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sender = ((TextView) view.findViewById(R.id.sender)).getText() .toString();
String subject = ((TextView) view.findViewById(R.id.subject)).getText() .toString();
String fname = ((TextView) view.findViewById(R.id.file_name)).getText() .toString();
Fragment frag =new DisplayFragment();
Bundle bundle = new Bundle();
bundle.putString("file_name", fname);
frag.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_frame, frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
和活动是:
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
现在新片段的代码是:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_displayfile, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
TextView tv=(TextView)getView().findViewById(R.id.textView1);
Bundle bundle = this.getArguments();
String myfile = (String)bundle.get("file_name");
tv.setText(myfile);
}
及其相关活动是:
<FrameLayout
android:id="@+id/display_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
所需的输出应该是新片段中的textview应该替换之前片段的全部内容。但点击我得到的项目后的输出是: 这个“嗨”应该单独显示不与列表。看来我的代码是正确的。提前致谢。
没有放置框架布局内的列表视图没有帮助 –
请参阅更新 – Sanjeev
一个选项是替换我理解的框架布局,但什么是“在片段替换中使用列表视图ID”你怎么做? –