Android onViewCreated上的处理程序杀死片段动画
我在onViewCreated方法中有一个简单的片段:Android onViewCreated上的处理程序杀死片段动画,android,multithreading,animation,fragment,handler,Android,Multithreading,Animation,Fragment,Handler,我在onViewCreated方法中有一个简单的片段: @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if (account != null) { try { Handler handler
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (account != null) {
try {
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(() ->
{
String decryptedCode;
try {
decryptedCode = (vaultService).getDecryptedPassword(account).trim();
in_password.setText(decryptedCode);
} catch (Exception e) {
Helper.showMessage(e.toString());
} finally {
in_password_layout.setHelperText("");
}
}, 1);
} catch (Exception e) {
Helper.showMessage(e.toString());
}
}
}
把我的碎片称为:
protected void openFragment(BaseFragment fragment) {
fragment.setCaller(this);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.activity_slide_in_right, R.anim.activity_slide_out_left, R.anim.activity_slide_out_right, R.anim.activity_slide_in_left);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(fragment.getClass().getName());
fragmentTransaction.commit();
}
我面临的问题是,如果我从onViewCreated方法中删除处理程序调用,那么转换将完美地进行。如果我像这样放置处理程序,它会终止动画,只显示片段而不显示任何动画。
如果我使用handler.postInFrontOfQueue,动画可以工作,但需要一段时间才能进入。这意味着它首先处理处理程序,然后才执行动画并传输到片段。
你知道我该怎么预防吗?我已经在一个独立的线程中进行了尝试,但它不起作用。
谢谢。我刚找到一个解决办法。我必须在postDelayed方法的第二个参数中输入动画发生所需的时间 就我而言:
getResources().getInteger(android.R.integer.config_mediumAnimTime)
因此,它仅在动画完成后触发处理程序
这不完全是我想要的,但很管用。我宁愿让这两个过程并行进行,这样当动画完成时,处理程序也完成了它的工作