android - ScrollView设置layoutParams后无法滚动
本文介绍了android - ScrollView设置layoutParams后无法滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
ScrollView中包含一个高度远超屏幕的LinearLayout,可以正常滚动。当使用ObjectAnimator把ScrollView向上平移100个px,在updateLisnter中重设ScrollView的高度后,Scrollview没有完全显示,也无法滚动,这是什么原因呢?
代码:
ll_wrap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(scrollview, "translationY", -100f);
objectAnimator.setDuration(500);
objectAnimator.start();
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//重设ScrollView高度
ViewGroup.LayoutParams params = scrollview.getLayoutParams();
params.height = (int) (scrollview.getHeight() - (float) valueAnimator.getAnimatedValue());
scrollview.setLayoutParams(params);
}
});
}
});
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_wrap"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="400dp">
</FrameLayout>
<FrameLayout
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="400dp">
</FrameLayout>
</LinearLayout>
</ScrollView>
解决方案
首先,很遗憾的告诉你,你的属性动画方法弄错了,translationY
这个属性View
有,也就是所有的View及其子类都会有,你设置了属性名称,那么它会自动去调用对应的Set方法
,比如,你现在这样写,那么它会自动调用View.setTranslationY(float translationY)
方法,所以你无需再执行下面的代码:
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
ViewGroup.LayoutParams params = scrollview.getLayoutParams();
params.height = (int) (params.height + (float)valueAnimator.getAnimatedValue());
scrollview.setLayoutParams(params);
}
});
其次还有个问题,LayoutParams.height 对应的有
public static final int FILL_PARENT = -1;
/**
* Special value for the height or width requested by a View.
* MATCH_PARENT means that the view wants to be as big as its parent,
* minus the parent's padding, if any. Introduced in API Level 8.
*/
public static final int MATCH_PARENT = -1;
/**
* Special value for the height or width requested by a View.
* WRAP_CONTENT means that the view wants to be just large enough to fit
* its own internal content, taking its own padding into account.
*/
public static final int WRAP_CONTENT = -2;
你的代码执行起来后,设置的LayoutParams.height
<-2,这很明显是不对的,一般都是大于0的.
删掉这段代码块后,如果你希望每次点击都网上平移100px,那么你可以这样写:
ll_wrap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float curTranslationY = scrollview.getTranslationY();
Log.e("test","curTranslationY: "+curTranslationY);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(scrollview, "translationY", curTranslationY,curTranslationY-100f);
objectAnimator.setDuration(500);
objectAnimator.start();
}
});
这篇关于android - ScrollView设置layoutParams后无法滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文