Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用一个片段替换另一个片段时重叠_Java_Android_Fragment - Fatal编程技术网

Java 用一个片段替换另一个片段时重叠

Java 用一个片段替换另一个片段时重叠,java,android,fragment,Java,Android,Fragment,如果用户单击列表项之一,我一直试图用另一个片段替换该片段。现在的问题是,另一个片段正在显示,但它并没有取代上一帧,而是显示在上一个片段的顶部。以下是我如何调用列表项上的新片段单击: public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String sender = ((TextView) v

如果用户单击列表项之一,我一直试图用另一个片段替换该片段。现在的问题是,另一个片段正在显示,但它并没有取代上一帧,而是显示在上一个片段的顶部。以下是我如何调用列表项上的新片段单击:

            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/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,它应该替换以前片段的全部内容。但单击我得到的项目后的输出是:
此hi应单独显示,而不是与列表一起显示。看来我的代码是正确的。提前感谢。

这里的问题是,您在单个视图中的列表视图中有一个框架布局。您正在用片段替换框架,但列表视图仍然存在

在xml中使用片段,而不是框架布局和列表视图 创建一个列表片段 创建活动时,将列表片段添加到视图 在列表项上,单击“用片段替换列表片段”。
希望这有帮助。

不,将listview放在框架布局内没有帮助。一个选项是替换我理解的框架布局,但在片段替换中使用列表视图id有什么用呢?fragmentTransaction.replaceR.id.list,frag;否如果我这样做,我会得到异常java.lang.UnsupportedOperationException:addViewView在adapterView中不受支持
    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>