我有一個包含text
幷包裝它的佈局。我需要在屏幕上垂直移動這個layout
。移動後返回原來的位置
這就是我所做的。
這將在屏幕上垂直移動佈局。但是現在,當我退出屏幕的手指時,我需要這種佈局返回到原來的位置。
當你得到ACTION_DOWN
事件的任何代碼將appriciated
我有一個包含text
幷包裝它的佈局。我需要在屏幕上垂直移動這個layout
。移動後返回原來的位置
這就是我所做的。
這將在屏幕上垂直移動佈局。但是現在,當我退出屏幕的手指時,我需要這種佈局返回到原來的位置。
當你得到ACTION_DOWN
事件的任何代碼將appriciated
試試這個:
case MotionEvent.ACTION_UP:
params.topMargin = initialTopMargin;
v.setLayoutParams(params);
存儲初始POS。處理ACTION_UP
事件,並在那裏將其重新設置爲最高。如果你想讓它流暢,可以啓動一個新線程來更新值並等待。
使用ACTION_UP移動佈局回到它原來的位置。以下是代碼。嘗試一下。
View valuesContainer = view.findViewById(R.id.valuesContainer);
valuesContainer.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldY = (int)event.getRawY();
initialTopMargin = params.topMargin;
break;
case MotionEvent.ACTION_MOVE:
newY = (int)event.getY();
params.topMargin = (int) (initialTopMargin - (oldY - newY));
v.setLayoutParams(params);
break;
case MotionEvent.ACTION_UP:
params.topMargin = initialTopMargin;
v.setLayoutParams(params);
break;
}
return true;
}
});