【问题标题】:Fragment Debug GPU overdraw片段调试 GPU 过度绘制
【发布时间】:2019-04-30 10:23:50
【问题描述】:

我有MainActivity 并在R.id.container 中添加了2 个片段。

MainActivity 如下所示。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, Fragment1.newInstance())
                .commit();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, Fragment2.newInstance())
                .commit();
    }
}

Fragmnet1Fragment2 的代码相同,但布局不同。

public class Fragment1 extends Fragment {
    public static Fragment1 newInstance() {
        return new Fragment1();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}

而关联的布局fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".ui.main.Fragment1">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Fragment2 的布局是 fragmnet2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".ui.main.Fragment1">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Test2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

这是在添加Fragment1Fragment2 后的Debug GPU overdraw MainActivity

请看下面这张图片。这是GPU的另外一张图,因为Fragment1中的布局仍然在MainActivity中。

如何在不加载/显示下面的片段的情况下添加片段?

【问题讨论】:

    标签: java android fragment gpu


    【解决方案1】:

    您正在同一个容器中添加两个片段。这是根据您的代码的确切行为。如果您只需要显示稍后添加的片段,则需要replace 片段而不是add

    我不知道你的确切用例。但是,我猜您正在尝试基于一些检查来显示 fragment1 和 fragment2。在这种情况下,您可能会考虑在您的MainActivity 中使用如下两个函数。

    public boolean fragment1Loaded = false;
    
    public void switchToFrag1() {
        fragment1Loaded = true;
    
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, Fragment1.newInstance())
                .commit();
    }
    
    public void switchToFrag2() {
        fragment1Loaded = false;
    
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, Fragment2.newInstance())
                .commit();
    }
    

    现在在onCreate 函数中,您需要根据必要的条件调用函数。

    if (showFirstFragment) switchToFrag1();
    else switchToFrag2();
    

    您可以随时从MainActivity 进行切换。

    希望对您有所帮助!

    更新

    您可以处理MainActivity 中的后退按钮单击并自行处理逻辑。覆盖onBackPressed 函数,然后根据需要切换片段。例如,参见上面的修改函数。我正在保留当前在屏幕中加载哪个片段的参考。然后覆盖onBackPressed 函数,如下所示。

    @Override
    public void onBackPressed() {
        if (fragment1Loaded) super.onBackPressed();
        else switchToFrag1();
    }
    

    希望你能明白。

    【讨论】:

    • 我需要用户按下后退按钮,用户可以看到最后一个片段,替换功能删除最后一个片段。
    • 我需要使用添加功能
    • 在使用 onBackPressed 时,由于使用了 add 功能,片段 1 被删除并再次新建,在我的片段中可能有数据并且这些数据是从服务器获取的。我不喜欢 onCreate() 最后一个片段,所以我想使用替换功能
    • 我强烈建议您查看Android的ViewModel,它可用于存储您加载的数据并根据您的需求再次加载数据。您也可以考虑设置本地数据库。
    猜你喜欢
    • 2013-03-04
    • 1970-01-01
    • 2014-04-28
    • 2018-09-10
    • 2021-09-17
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多