Android中的主要活动重叠片段

时间:2020-07-02 14:49:15

标签: android

每当我单击接收消息时,它都会转到ReceivedFragment,并且可以工作,但是问题是Mainactivity与ReceivedFragment重叠,如下图所示,我已经尝试过删除framelayout中的所有视图,但结果是总是一样,有人可以帮我吗?我是android中的新手,以下是我当前的代码,谢谢

enter image description here

MainActivty.java

           @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch (i) {
                case 0:
                    break;

                case 1:
                    ReceivedFragment dashfragment = new ReceivedFragment();
                    FrameLayout fl = (FrameLayout) findViewById(R.id.frame_layout);
                    fl.removeAllViews();
                    FragmentTransaction transaction1 =getSupportFragmentManager().beginTransaction();
                    transaction1.add(R.id.frame_layout, dashfragment);
                    transaction1.commit();
                    break;
                    //case - so on and so on

我的activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame_layout" />
</RelativeLayout >

2 个答案:

答案 0 :(得分:0)

在您的listView方法中引用您的onCreate()

listView = findViewById(R.id.listView);

在您的onItemClick方法中:

listView.setVisibility(View.INVISIBLE);

当您返回时,使其再次可见:

listView.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

为片段添加白色或任何背景。那应该解决问题。发生这种情况是因为您要维护一堆打开的片段。