【问题标题】:Fragment transition clears data visually (android)片段过渡可视化清除数据(android)
【发布时间】:2017-07-24 11:14:28
【问题描述】:

假设我有一个带有 ListView 或带有图像的 GridView 的片段。当我使用编程方式或后退按钮进行转换时,我看到这些列表视图/网格视图数据在动画之间被视觉清除。像 textviews 这样的其他控件没有这个问题。这种行为会造成不良的视觉效果。

以下是我用于片段转换的代码。

有没有办法减少这种丑陋的视觉影响。

        String ttag = fragment
                .getClass().toString();
        Fragment tempF = fragmentManager.findFragmentByTag(ttag);

        if (tempF != null && tempF.isVisible())
            return;
        Bundle args = new Bundle();
        args.putParcelable(Utils.FRAGMENT_INPUT_KEY, fragment.getInput());
        fragment.setArguments(args);

        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        fragmentTransaction.setAllowOptimization(true);
        fragmentTransaction.setCustomAnimations(R.anim.fragment_enter,
                R.anim.fragment_exit, R.anim.fragment_leftenter,
                R.anim.fragment_leftexit);
        fragmentTransaction.replace(R.id.fragment, fragment, ttag);

        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

以下片段具有顶级视图分页器,它使用带有图像的网格视图加载片段。清除子级片段上的网格视图时会出现视觉问题。顶级片段没有这样的问题。

使用 viewpager 的顶级 Fragment 生命周期方法。

ViewPager viewPager;
@Override
public void onActivityCreated(Bundle bundle) {
    super.onActivityCreated(bundle);

    activity = getActivity();
    resource = activity.getResources();
    view = getView();

    viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    // some other setup

 }

@Override
public void onStop() {
    super.onStop();
}


@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (Exception e) {

    }

    try {

        if(viewPager != null) {
            viewPager.setAdapter(null);
            viewPager.removeAllViews();
        }

    } catch (Exception e) {

        int x = 0;
    }
}

子选项卡级别的片段,带有加载的图像的网格视图,在按下后退按钮时会被清除。

GridView list;
GridViewIncompleteAdapter adapter;
ArrayList<String> puzzelList = null;
ArrayList<PuzzelFileData> imagePuzzelList = null;
Override
public void onActivityCreated(Bundle bundle) {
    super.onActivityCreated(bundle);

    activity = getActivity();
    resource = activity.getResources();
    View view = getView();

    puzzelList = new ArrayList<String>();
    imagePuzzelList = new ArrayList<PuzzelFileData>();

    path = Utils.getAppFilePath(activity);
    path = path + Utils.INC_FOLDER;

    list = (GridView) view.findViewById(R.id.listIncomplete);
    Point p = Utils.getDisplaySize(activity);
 }

 @Override
 public void onStop() {
    super.onStop();

    if(task != null)
        task.cancel(true);

    if(handler != null && runnable != null)
        handler.removeCallbacks(runnable);

}

@Override
public  void onDestroy(){
    super.onDestroy();

    if(puzzelList != null)
        puzzelList.clear();

    if(imagePuzzelList != null)
        imagePuzzelList.clear();

    if(adapter != null)
        adapter.disponse();
}

带有标签视图的顶部布局,

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frag"
    android:orientation="vertical">
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"/>
    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

带有网格视图的布局,

<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"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/txt_inc_nodata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/LOADING_DATA"
        android:textColor="@color/subtitle_text_color"
        android:textSize="@dimen/text_size_smallmedium"
        android:textStyle="bold" />

    <GridView
        android:id="@+id/listIncomplete"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="1dp"
        android:gravity="center"
        android:numColumns="3" />

</RelativeLayout>

【问题讨论】:

  • 请从有问题的片段中显示生命周期方法。
  • 我添加了片段方法。如果有不清楚的地方,请告诉我。谢谢
  • view = getView(); 内 onActivityCreated 看起来不太好。基于此,我假设您在活动的 xml 布局中声明 &lt;fragment/&gt;,对吗?
  • 我也添加了 UI。我没有按照你说的去做。谢谢

标签: android android-fragments gridview


【解决方案1】:

根据您的代码,您正在自行清除onDestroy 内的列表:

@Override 
public  void onDestroy() {
    super.onDestroy();

    if(puzzelList != null)
        puzzelList.clear();

    if(imagePuzzelList != null)
        imagePuzzelList.clear();

    if(adapter != null)
        adapter.disponse(); 
}

另外,您正在替换您的片段,因此在这种情况下应该调用onDestroy。请记住生命周期流程,在您的情况下将是:

Fragment1.onPause();
Fragment1.onStop();
Fragment1.onDestroy();
Fragment2.onCreate();
Fragment2.onResume();

为了更好地理解您可以正确显示位图,我建议您查看Handling Bitmaps。

【讨论】:

  • 感谢您的输入,我想当我们到达 onDestroy 片段时不再可见。如果这是基于我的代码的行为,我如何清除数据而不产生令人讨厌的视觉效果。
  • @pats 为什么你决定在onDestroy 期间清除列表?你想达到什么目标?
  • 我想发布片段数据。有一个包含 400 个位图的片段,这对内存很重要。所以我需要在不依赖 GC 的情况下以编程方式清除它们。如果我清除它们 ondistory,它对用户可见。这就是我面临的问题。我不知道其他任何地方/生命周期方法来做这个数据发布。
  • @pats 你不应该同时在内存中保存400 bitmaps。
  • 它们很小,但这不是我的问题。有没有无视觉效果的资源释放
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 2012-05-24
相关资源
最近更新 更多