android 的滚动视图中的 Recyclerview
Recycle view in scroll view for android
我这里有这段我写的代码,应该允许您在屏幕上移动对象。
我似乎遇到了问题,因为物体会移动但不平稳,当你按住手指时它会跳来跳去。
请帮忙。
VVVVVVVVVVVVV
代码
VVVVVVVVVVVVV
package com.example.alex.alexstouch;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView touchMove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
touchMove = (TextView)findViewById(R.id.pike);
touchMove.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
touchMove.setTranslationX(event.getX());
touchMove.setTranslationY(event.getY());
break;
case MotionEvent.ACTION_DOWN:
Log.d("pike pike", "broken");
break;
}
return true;
}
});
}
}
如果你想在屏幕上移动一些对象,例如拖放按钮,你应该制作你的根布局 - 相对布局并在代码中更改对象边距(调用触摸回调时)。
您可以像这样更改使用 layoutParams 的边距
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = layoutParams.rightMargin + x
layoutParams.bottomMargin = layoutParams.bottomMargin + y;
view.setLayoutParams(layoutParams);
我的浮动按钮代码(可以通过长按移动)
private class PlusButtonTouchListener implements View.OnTouchListener {
private final int MIN_X = 0;
private final int MIN_Y = 0;
private float startX;
private float startY;
private int MAX_X;
private int MAX_Y;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
MAX_X = ((View) mPlusButton.getParent()).getWidth() - mPlusButton.getWidth();
MAX_Y = ((View) mPlusButton.getParent()).getHeight() - mPlusButton.getHeight();
mPlusButton.setAlpha(0.7f);
break;
case MotionEvent.ACTION_UP:
mPlusButton.setAlpha(1f);
savePlusButtonCoordinates();
isMoved = false;
break;
case MotionEvent.ACTION_MOVE:
if (isMoved) {
float deltaX = startX - event.getX();
float deltaY = startY - event.getY();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = layoutParams.rightMargin + (int) deltaX;
layoutParams.bottomMargin = layoutParams.bottomMargin + (int) deltaY;
if (layoutParams.rightMargin > MAX_X) {
layoutParams.rightMargin = MAX_X;
} else if (layoutParams.rightMargin < MIN_X) {
layoutParams.rightMargin = MIN_X;
}
if (layoutParams.bottomMargin > MAX_Y) {
layoutParams.bottomMargin = MAX_Y;
} else if (layoutParams.bottomMargin < MIN_Y) {
layoutParams.bottomMargin = MIN_Y;
}
view.setLayoutParams(layoutParams);
}
break;
}
mPlusButton.invalidate();
return false;
}
}
我这里有这段我写的代码,应该允许您在屏幕上移动对象。
我似乎遇到了问题,因为物体会移动但不平稳,当你按住手指时它会跳来跳去。
请帮忙。
VVVVVVVVVVVVV 代码 VVVVVVVVVVVVV
package com.example.alex.alexstouch;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView touchMove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
touchMove = (TextView)findViewById(R.id.pike);
touchMove.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
touchMove.setTranslationX(event.getX());
touchMove.setTranslationY(event.getY());
break;
case MotionEvent.ACTION_DOWN:
Log.d("pike pike", "broken");
break;
}
return true;
}
});
}
}
如果你想在屏幕上移动一些对象,例如拖放按钮,你应该制作你的根布局 - 相对布局并在代码中更改对象边距(调用触摸回调时)。
您可以像这样更改使用 layoutParams 的边距
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = layoutParams.rightMargin + x
layoutParams.bottomMargin = layoutParams.bottomMargin + y;
view.setLayoutParams(layoutParams);
我的浮动按钮代码(可以通过长按移动)
private class PlusButtonTouchListener implements View.OnTouchListener {
private final int MIN_X = 0;
private final int MIN_Y = 0;
private float startX;
private float startY;
private int MAX_X;
private int MAX_Y;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
MAX_X = ((View) mPlusButton.getParent()).getWidth() - mPlusButton.getWidth();
MAX_Y = ((View) mPlusButton.getParent()).getHeight() - mPlusButton.getHeight();
mPlusButton.setAlpha(0.7f);
break;
case MotionEvent.ACTION_UP:
mPlusButton.setAlpha(1f);
savePlusButtonCoordinates();
isMoved = false;
break;
case MotionEvent.ACTION_MOVE:
if (isMoved) {
float deltaX = startX - event.getX();
float deltaY = startY - event.getY();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = layoutParams.rightMargin + (int) deltaX;
layoutParams.bottomMargin = layoutParams.bottomMargin + (int) deltaY;
if (layoutParams.rightMargin > MAX_X) {
layoutParams.rightMargin = MAX_X;
} else if (layoutParams.rightMargin < MIN_X) {
layoutParams.rightMargin = MIN_X;
}
if (layoutParams.bottomMargin > MAX_Y) {
layoutParams.bottomMargin = MAX_Y;
} else if (layoutParams.bottomMargin < MIN_Y) {
layoutParams.bottomMargin = MIN_Y;
}
view.setLayoutParams(layoutParams);
}
break;
}
mPlusButton.invalidate();
return false;
}
}