如何动画和控制Android PreferenceFragment屏幕?

时间:2015-05-14 01:07:13

标签: android android-fragments android-animation android-preferences

我有一个PreferenceFragment,其内容以XML格式定义。这些内容包括儿童PreferenceScreen。当我点击PreferenceScreen时,新屏幕会成功渲染,但它没有动画(它只是在屏幕上显示,甚至在Lollipop设备上完成材质波纹之前)。

更糟糕的是,如果我有任何复杂的布局(例如,PreferenceFragment位于屏幕一侧的标签中),那布局就会被吹走,并被全屏片段取代。 / p>

我怀疑如果我发现点击PreferenceScreen时发生的回调或事件,我可以解决这两个问题,但我真的不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

我为PreferenceFragment的从右到左动画制作了幻灯片... 我扩展了PreferenceFragment并覆盖了onCreateView(),如下所示:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    AnimRelativeLayout animRelativeLayout = new AnimRelativeLayout(getActivity());
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    animRelativeLayout.setLayoutParams(layoutParams);
    animRelativeLayout.addView(view);
    return animRelativeLayout;
}

其中AnimRelativeLayout.java

public class AnimRelativeLayout extends RelativeLayout {

private IMovementListener mMovementListener;

public AnimRelativeLayout(Context context) {
    super(context);
    this.initMembers();
}

public AnimRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.initMembers();
}

public AnimRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.initMembers();
}

private void initMembers() {
    this.mMovementListener = new SimpleMovementListener();
}

public float getXFraction() {
    final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    return (width == 0) ? 0 : getX() / (float) width;
}

public void setXFraction(float xFraction) {
    final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    this.mMovementListener.onSetXFraction(xFraction);
    setX((width > 0) ? (xFraction * width) : 0);
}

public float getYFraction() {
    final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int height = size.y;
    return (height == 0) ? 0 : getY() / (float) height;
}

public void setYFraction(float yFraction) {
    final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int height = size.y;
    this.mMovementListener.onSetYFraction(yFraction);
    setY((height > 0) ? (yFraction * height) : 0);
}

public interface IMovementListener {
    public void onSetXFraction(float pXFraction);

    public void onSetYFraction(float pYFraction);
}

private class SimpleMovementListener implements IMovementListener {

    @Override
    public void onSetXFraction(float pXFraction) {

    }

    @Override
    public void onSetYFraction(float pYFraction) {

    }
}

然后做了这个:

pFragmentManager.beginTransaction()
            .setCustomAnimations(
                    R.animator.slide_in_right_to_left, R.animator.slide_out_right_to_left,
                    R.animator.slide_in_left_to_right, R.animator.slide_out_left_to_right
            )
            .replace(R.id.fragment_container, fragment)
            .commit();

以及R.animator.slide_in_right_to_left的示例是:

<?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="300"
        android:propertyName="xFraction"
        android:valueFrom="1.0"
        android:valueTo="0"
        android:valueType="floatType"/>
</set>