在 Fragment 的布局中查看未在正确的时间膨胀?
View in Fragment's layout not being inflated at the right time?
我试图在我的片段布局中找出某个布局的高度,因为我想在动画中使用它以使其在点击事件时弹出。这是两个动画:
private void hideEditSetFragment() {
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
mEditSetContainer.getHeight()); // toYDelta
animate.setDuration(500);
// animate.setFillAfter(true);
TranslateAnimation animate1 = new TranslateAnimation(0,0,mBottomNavigationView.getHeight(),0);
animate1.setDuration(500);
// animate1.setFillAfter(true);
mBottomNavigationView.startAnimation(animate1);
mEditSetContainer.startAnimation(animate);
mBottomNavigationView.setVisibility(View.VISIBLE);
mEditSetContainer.setVisibility(View.GONE);
}
private void showEditSetFragment() {
mEditSetContainer.setVisibility(View.INVISIBLE);
Log.i(TAG, "showEditSetFragment(): mEditSetContainer height is " + mEditSetContainer.getMeasuredHeight());
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
mEditSetContainer.getHeight(), // fromYDelta
0); // toYDelta
animate.setDuration(1000);
//animate.setFillAfter(true);
TranslateAnimation animate1 = new TranslateAnimation(0,0,0,mBottomNavigationView.getHeight());
animate1.setDuration(1000);
//animate1.setFillAfter(true);
mBottomNavigationView.startAnimation(animate1);
mEditSetContainer.startAnimation(animate);
mBottomNavigationView.setVisibility(View.INVISIBLE);
mEditSetContainer.setVisibility(View.VISIBLE);
}
问题是我的 mEditSetContainer
需要至少绘制一次才能在这个动画中使用,因为我使用了它的高度。因此,在 onCreateView()
期间,我将其设置为可见:
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle onSavedInstanceState) {
Log.i(TAG, "onCreateView()");
View v = inflater.inflate(R.layout.fragment_current_workout, container, false);
v.findViewById(R.id.fragment_current_workout_recycler_view);
mPerformedExercisesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mEditSetContainer = (ConstraintLayout) v.findViewById(R.id.fragment_current_workout_edit_set_container);
mEditSetFragment = new EditSetFragment();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_current_workout_edit_set_container, mEditSetFragment, "EditSetFragment").commit();
mEditSetContainer.setVisibility(View.VISIBLE);
mEditSetContainer.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Runnable called " + String.valueOf( mEditSetContainer.getHeight()));
Log.i(TAG, "Runnable called " + String.valueOf( mBottomNavigationView.getHeight()));
mEditSetContainer.setVisibility(View.GONE);
}
});
return v;
}
我在这里使用了一个可运行的,因为它应该在根据这个绘制之后被调用 thread. However, the view is still not drawn for the mEditSetContainer
when the view for the mBottomNavigationView
is. It is to note that the mEditSetContainer
has its height defined by wrap-content and that it is a container for a nested fragment. My question is how can I get its height when the fragment is created? I need to know this to be able to run my animation. What currently happens is that on the first click, the animation is skipped but it works after. This 也是一个相关的线程,可以提供帮助,但没有。
另外,请不要建议将我的视图设置为不可见。我需要将它们设置为消失,因为它们不应该在不使用时占用空间。 mEditSetFragment
在显示时与其他内容重叠。
在 onCreateView
试试这个:
mEditSetContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
height = mEditSetContainer.getHeight();
//Other operations
}
});
我试图在我的片段布局中找出某个布局的高度,因为我想在动画中使用它以使其在点击事件时弹出。这是两个动画:
private void hideEditSetFragment() {
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
mEditSetContainer.getHeight()); // toYDelta
animate.setDuration(500);
// animate.setFillAfter(true);
TranslateAnimation animate1 = new TranslateAnimation(0,0,mBottomNavigationView.getHeight(),0);
animate1.setDuration(500);
// animate1.setFillAfter(true);
mBottomNavigationView.startAnimation(animate1);
mEditSetContainer.startAnimation(animate);
mBottomNavigationView.setVisibility(View.VISIBLE);
mEditSetContainer.setVisibility(View.GONE);
}
private void showEditSetFragment() {
mEditSetContainer.setVisibility(View.INVISIBLE);
Log.i(TAG, "showEditSetFragment(): mEditSetContainer height is " + mEditSetContainer.getMeasuredHeight());
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
mEditSetContainer.getHeight(), // fromYDelta
0); // toYDelta
animate.setDuration(1000);
//animate.setFillAfter(true);
TranslateAnimation animate1 = new TranslateAnimation(0,0,0,mBottomNavigationView.getHeight());
animate1.setDuration(1000);
//animate1.setFillAfter(true);
mBottomNavigationView.startAnimation(animate1);
mEditSetContainer.startAnimation(animate);
mBottomNavigationView.setVisibility(View.INVISIBLE);
mEditSetContainer.setVisibility(View.VISIBLE);
}
问题是我的 mEditSetContainer
需要至少绘制一次才能在这个动画中使用,因为我使用了它的高度。因此,在 onCreateView()
期间,我将其设置为可见:
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle onSavedInstanceState) {
Log.i(TAG, "onCreateView()");
View v = inflater.inflate(R.layout.fragment_current_workout, container, false);
v.findViewById(R.id.fragment_current_workout_recycler_view);
mPerformedExercisesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mEditSetContainer = (ConstraintLayout) v.findViewById(R.id.fragment_current_workout_edit_set_container);
mEditSetFragment = new EditSetFragment();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_current_workout_edit_set_container, mEditSetFragment, "EditSetFragment").commit();
mEditSetContainer.setVisibility(View.VISIBLE);
mEditSetContainer.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Runnable called " + String.valueOf( mEditSetContainer.getHeight()));
Log.i(TAG, "Runnable called " + String.valueOf( mBottomNavigationView.getHeight()));
mEditSetContainer.setVisibility(View.GONE);
}
});
return v;
}
我在这里使用了一个可运行的,因为它应该在根据这个绘制之后被调用 thread. However, the view is still not drawn for the mEditSetContainer
when the view for the mBottomNavigationView
is. It is to note that the mEditSetContainer
has its height defined by wrap-content and that it is a container for a nested fragment. My question is how can I get its height when the fragment is created? I need to know this to be able to run my animation. What currently happens is that on the first click, the animation is skipped but it works after. This 也是一个相关的线程,可以提供帮助,但没有。
另外,请不要建议将我的视图设置为不可见。我需要将它们设置为消失,因为它们不应该在不使用时占用空间。 mEditSetFragment
在显示时与其他内容重叠。
在 onCreateView
试试这个:
mEditSetContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
height = mEditSetContainer.getHeight();
//Other operations
}
});