gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:10:11 27 4
gpt4 key购买 nike

我有一个 PreferenceFragment,其内容是用 XML 定义的。这些内容包括一个子 PreferenceScreen。当我单击 PreferenceScreen 时,新屏幕已成功呈现,但它没有动画(它只是显示在屏幕上,甚至在 Lollipop 设备上的 Material ripple 完成之前)。

更糟糕的是,如果我正在进行任何复杂的布局(例如,PreferenceFragment 位于屏幕一侧的选项卡中),该布局会被吹走并替换为全屏 fragment 。

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

最佳答案

我为 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>

关于android - 如何动画和控制 Android PreferenceFragment 屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com