简体   繁体   English

为什么视图没有更新?

[英]Why the view isn`t updating?

I'm building an app which has 2 fragment containers and takes screenshot feature.我正在构建一个具有 2 个片段容器并具有屏幕截图功能的应用程序。 the problem is whenever one of the 2 fragment containers update with another fragment(replaced) and I'm taking a screenshot it shows the previous fragments and not the updated ones.问题是每当两个片段容器中的一个更新另一个片段(已替换)并且我正在截取屏幕截图时,它会显示以前的片段而不是更新的片段。

I'm using getView().getRootView() within another fragment in the activity to take the screenshot.我在活动的另一个片段中使用getView().getRootView()来截取屏幕截图。 any advice on why it happens?关于为什么会发生的任何建议?

Please be more elaborate about the problem or try to add the code.请详细说明问题或尝试添加代码。

If you are not using the FrameLayout, please use this.如果您没有使用 FrameLayout,请使用它。

Code for Adding FrameLayout:添加 FrameLayout 的代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/your_placeholder"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></FrameLayout>

</android.support.constraint.ConstraintLayout>

To add or replace new Fragments use the FragmentTransaction.要添加或替换新的 Fragment,请使用 FragmentTransaction。

// Code inside the onCreate method
getSupportFragmentManager().beginTransaction()
               .replace(R.id.your_placeholder, new TheOtherFragment())
               .commit();

To take screen Shot :截屏:

View view = findViewById(R.id.your_placeholder);
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

To take screen Shot :截屏:

View view = findViewById(R.id.id_of_constraintLayout); 
// Here you assign the view Variable to the id of the View hosting all your Views. 

view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM