如何在使用customAnimation替换FragmentTransaction时避免片段覆盖

时间:2014-08-29 13:57:34

标签: android android-fragments

使用以下代码,我将一个片段替换为另一个片段:

FragmentTransaction ft = getFragmentManager().beginTransaction();
if(anomalieFragment == null)
{
    ft.setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left, R.animator.slide_out_right, R.animator.slide_in_left);
    anomalieFragment = new AnomalieListFragment();
    ft.replace(R.id.content, anomalieFragment);
    ft.addToBackStack(null);
}

slide_in_leftslide_out_right的定义如下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="500"
            android:propertyName="XFraction"
            android:valueFrom="0"
            android:valueTo="100"
            android:valueType="floatType" />

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="500"
            android:propertyName="XFraction"
            android:valueFrom="100"
            android:valueTo="0"
            android:valueType="floatType" />

</set>

一旦完成转换,一切都很好,没有叠加,但是当转换处于运行状态时,旧片段在新片段下仍然可见。

正在进行的转型 - 1 Transition in progress - 1 正在进行的过渡 - 2 Transition in progress - 2

我能做些什么来避免这种情况吗?

修改

这是XFraction的实现(我创建了一个FrameLayout派生类:

SlidingFrameLayout

package com.aquadata.tutorials.FirstApp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import com.aquadata.fms.R;
import com.google.android.gms.internal.x;

/**
 * Created by igirouard on 2014-08-27.
 * FrameLayout supportant d'être slidé in an out par le R.animator.slide_in_left et slide_out_right
 * Supporte aussi que son enfant contienne une vue id/shadow pour afficher un ombrage quand
 */
public class SlidingFrameLayout extends FrameLayout
{
    //Position initiale du layout, telle que définie dans OnLayout
    float initialX = -1;

    //Dernière fraction
    float _xFraction = -1;

    public SlidingFrameLayout(Context context)
    {
        super(context);
    }

    public SlidingFrameLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public SlidingFrameLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    //Retourne la fraction de la position initiale par rapport à la position lors du slide
    private float getXFraction()
    {
        return getWidth() == 0 ? 0 :(getX() - initialX) / getWidth() * 100;
    }

    //Déterminer un nouveau x selon la fraction de déplacement domandée
    public void setXFraction(float xFraction)
    {
        _xFraction = xFraction;
        if(initialX != -1.0f && getWidth() != 0)
        {
            final float newX = initialX + (xFraction / 100 * getWidth());
            setX(newX);
        }
    }

    /**
     * Called from layout when this view should
     * assign a size and position to each of its children.
     *
     * Derived classes with children should override
     * this method and call layout on each of
     * their children.
     * @param changed This is a new size or position for this view
     * @param left Left position, relative to parent
     * @param top Top position, relative to parent
     * @param right Right position, relative to parent
     * @param bottom Bottom position, relative to parent
     *
     * Si une ombre a été ajoutée au layout, la cacher si on est à x == 0
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        if(changed)
            initialX = left;


        super.onLayout(changed, left,top,right,bottom);

        if(_xFraction != -1)
            setXFraction(_xFraction);
    }
}

1 个答案:

答案 0 :(得分:0)

我通过设置背景颜色修复了这个类似的问题:

android:background="@color/background_material_light"
相关问题