I'm implementing a time picker which looks like this:
That yellow block is MyCustomView
. When moving MyCustomView
, I should calculate new date and set tvDate
.
partial layout file:
<LinearLayout
android:orientation="vertical">
<TextView android:id="@+id/tv_date">
<RelativeLayout
android:id="@+id/rl">
<MyCustomView
android:layout_centerInParent="true"/>
<OtherViews/>
</RelativeLayout>
</LinearLayout>
code:
class MyCustomView extends View{
// move listener
public interface IMoveCallback{
void update(int index);
}
private IMoveCallback listener = null;
// set listener
public void setMoveCallback(IMoveCallback callback){
this.listener = callback;
}
@Override
protected void onDraw(Canvas c){
super.onDraw(c);
// draw yellow block and four arrows here.
}
@Override
public boolean onTouch(View v, MotionEvent event) {
processDrag(v, event);
invalidate();
return false;
}
private void processDrag(View v, MotionEvent event){
// calculate new position(left, top, right, bottom)
v.layout(newLeft, newTop, newRight, newBottom);
if(listener != null){
// calculate index by new position
listener.update(index);
}
}
}
class MainActivity extends Activity implements MyCustomView.IMoveCallback{
MyCustomView view; // view.setMoveCallback(MainActivity.this)
@Override
public void update(int index){
tvDate.setText(String.valueOf(System.currentTimeMillis()))//update tvDate
}
}
If tvDate.setText()
is removed, MyCustomView
follows the finger, like this:
If I update tvDate
, MyCustomView
moves back to the center of rl
:
I don't think it an activity-lifecycle issue. Someone mentioned ((MarginLayoutParams)rl.getLayoutParams()).topMargin
but did not explain why.
Anybody can help me?