I have made a layout, which has two RelativeLayout which behave as two sliders (like Notification on status bar), which the user can drag up and down, and accordingly I have to change top and bottom text value.
<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>
During layout, the two RelativeLayout lie on top of each other, and I offset them to the shown initial position using offsetTopAndBottom(int offset)
Now, I track MotionEvents on both RelativeLayout, and call this method on ACTION_MOVE event.
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;
}
}
Here mTopHandle and mBottomHandle are the two white RelativeLayouts.
Note : I don't use LayoutParams and change top and bottom margins respectively here because that calls requestLayout repeatedly and is not smooth at all.
Now, I have to change top text value (which is a child of top RelativeLayout) when the parent RelativeLayout offsets on move event. But, when I call setText, it implicitly calls requestLayout() which resets all offsets done on both handles.
Now, this is a deal breaker since I have come so far in making these sliders. I tried overriding requestLayout() to do nothing on TextView but then text is not set properly a.k.a it shows random behaviour.
Even if I remove both text views out from the RelativeLayouts, then also a call to requestLayout() will force the parent FrameLayout to relayout all its child views.
Any suggestions ?