【发布时间】:2013-12-26 10:08:00
【问题描述】:
我已经制作了一个布局,它有两个 RelativeLayout,它们表现为两个滑块(如状态栏上的通知),用户可以上下拖动,因此我必须更改顶部和底部的文本值。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" >
<RelativeLayout
android:id="@+id/handle1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:layout_marginLeft="95dp" >
<TextView
android:id="@+id/top_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Top text"
android:textColor="@color/black"
android:textSize="18sp" />
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/handle2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="95dp"
android:layout_marginTop="40dp" >
<TextView
android:id="@+id/bottom_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Bottom text"
android:textColor="@color/black"
android:textSize="18sp" />
</FrameLayout>
</RelativeLayout>
在布局期间,两个 RelativeLayout 相互重叠,我使用 offsetTopAndBottom(int offset) 将它们偏移到显示的初始位置
现在,我在两个 RelativeLayout 上跟踪 MotionEvents,并在 ACTION_MOVE 事件上调用此方法。
private void offsetHandle(View view, int distanceY) {
switch(view.getId()) {
case R.id.handle1:
mTopHandle.offsetTopAndBottom(distanceY);
mTopSliderPosition = mTopHandle.getBottom();
break;
case R.id.handle2:
mBottomHandle.offsetTopAndBottom(distanceY);
mBottomSliderPosition = mBottomHandle.getTop();
break;
}
}
这里的mTopHandle和mBottomHandle是两个白色的RelativeLayout。
注意:我不使用 LayoutParams 并在此处分别更改顶部和底部边距,因为它会反复调用 requestLayout 并且根本不流畅。
现在,当父 RelativeLayout 在移动事件上发生偏移时,我必须更改顶部文本值(它是顶部 RelativeLayout 的子级)。但是,当我调用 setText 时,它会隐式调用 requestLayout() 来重置在两个句柄上完成的所有偏移量。
现在,这是一个交易破坏者,因为我到目前为止制作这些滑块。我尝试覆盖 requestLayout() 在 TextView 上什么都不做,但是文本设置不正确,也就是它显示随机行为。
即使我从 RelativeLayouts 中删除了两个文本视图,对 requestLayout() 的调用也会强制父 FrameLayout 重新布局其所有子视图。
有什么建议吗?
【问题讨论】: