【发布时间】:2015-01-14 07:11:42
【问题描述】:
我使用 ViewPager 和 4 个 Fragments 作为我的应用程序的某种介绍/手册。我的问题是当我到达最后一个片段时,它看起来像这样:
当用户单击“准备就绪”Button 时,我有一个动画来“折叠”标题(将其高度更改为 0 像素)并执行一些其他操作。我的意图是在用户导航到 ViewPager 的另一个片段时将视图重置为其原始状态(“展开”标题)。
要知道用户何时移动到另一个页面,我正在使用setUserVisibleHint,如下所示:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
if( !isVisibleToUser) {
// Reset/expand the title's TextView
if(mTitle != null) {
int titlesHeight = mTitle.getMeasuredHeight();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTitle.getLayoutParams();
params.height = titlesHeight;
mTitle.setLayoutParams(params);
mTitle.invalidate();
mTitle.requestLayout();
}
// Reset other views...
}
}
但这不起作用。
我尝试过的
我已经尝试了setLayoutParams、invalidate 和requestLayout 的所有可能组合,包括留下一个或多个并更改其顺序,但是当用户导航回第四页时,标题仍处于折叠状态。
我检查了这个方法是否被正确触发,titlesHeight != 0 每次和getText 仍然返回“标题”。
我重置了其他视图(为简单起见,我没有显示),例如我的屏幕截图中显示的Chronometer,一切都很好。
onPageSelected 和界面
界面
public interface FragmentAdapterNotificator {
public void fragmentIsVisible();
public void fragmentIsNotVisible();
}
mViewPager.setOnPageChangeListener()
Override
public void onPageSelected(int i) {
FragmentAdapterNotificator fragment = (FragmentAdapterNotificator) mAdapter.instantiateItem(mViewPager, i);
if (fragment != null) {
fragment.fragmentIsVisible();
}
}
片段实现接口
public class FragmentFour extends Fragment implements FragmentAdapterNotificator {
@Override
public void fragmentIsVisible() {
// Reset/expand the title's TextView
if(mTitle != null) {
int titlesHeight = mTitle.getMeasuredHeight();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTitle.getLayoutParams();
params.height = titlesHeight;
mTitle.setLayoutParams(params);
mTitle.invalidate();
mTitle.requestLayout();
}
}
}
有人知道这里发生了什么吗?
注意
我不想使用另一个Animation 来扩展标题,因为如果用户在另一个页面中,那么动画高度的恢复是没有意义的,因为用户不会看到它,所以我认为就性能最好使用“快捷方式”来避免动画。
我尝试使用“扩展”动画进行测试,效果很好,但我不想使用它。
附录
这是我的片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/transparent"
android:clipChildren="false"
android:padding="30dp" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextTitle"
android:text="@string/tour_four_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingTop="16dp" >
<Chronometer
android:id="@+id/chronometer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="@+id/ready"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="READY" />
</LinearLayout>
<!-- Some Stuff -->
</LinearLayout>
“折叠”动画用于将 Title 的高度设置为 0:
public void collapseView(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(800);
v.startAnimation(a);
}
“展开”动画:
public void expandView(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.WRAP_CONTENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(1000);
v.startAnimation(a);
}
和我的“reset”方法差不多,但是不知道为什么不行。
【问题讨论】:
标签: android resize textview android-viewpager collapse