2014-02-11 137 views
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应该替换之前片段的全部内容。但点击我得到的项目后的输出是: enter image description here 这个“嗨”应该单独显示不与列表。看来我的代码是正确的。提前致谢。

回答

-1

问题在于,您在单个视图中有一个框架布局列表视图。您正在用片段替换帧,但列表视图仍然存在。在你的XML,而不是框架布局和列表视图

  1. 使用片段
  2. 创建列表片段
  3. 在创建活动中添加列表片段视图
  4. 列表项单击替换为表片段您分段。

希望这有助于。

+0

没有放置框架布局内的列表视图没有帮助 –

+0

请参阅更新 – Sanjeev

+0

一个选项是替换我理解的框架布局,但什么是“在片段替换中使用列表视图ID”你怎么做? –