gpt4 book ai didi

Android 滑动 - 我应该使用手势监听器还是 MotionEvent.ACTION_MOVE?

转载 作者:行者123 更新时间:2023-11-30 02:01:57 26 4
gpt4 key购买 nike

在 Recycler View 中左右滑动列表项时,两者中哪一个更适合使用?目前我的代码位于 MotionEvent.Action_move 中,我无法弄清楚很多事情。

  1. 运动一点也不流畅。非常波涛汹涌。并且列表项总是在稍微延迟后开始移动。
  2. 弄清楚我的手势是右手还是左手?

我正在查看 AndroidSwipeListView 的代码,他们似乎没有使用手势监听器。因此,我问。

Action_move 中执行移动的代码

               float posX = v.getX();
float velocityX = Math.abs(velocityTracker.getXVelocity());

float x = posX - event.getRawX();
if (x < 0) {
posX += event.getRawX() / 100 + velocityX / 10;

}
else {
posX -= event.getRawX() / 100 + velocityX / 10;

}
v.animate().translationX(posX)
.alpha(0.8f);

使用更多代码进行第二次编辑:

public ViewHolder(final View itemView, int ViewType) {                 // Creating ViewHolder Constructor with View and viewType As a parameter
super(itemView);

//itemView.setOnClickListener(this);
// Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
//itemView.setOnClickListener(this);
itemView.setOnTouchListener(this);

Name = (TextView) itemView.findViewById(R.id.avg_price); // Creating Text View object from header.xml for name
email = (TextView) itemView.findViewById(R.id.quantity); // Creating Text View object from header.xml for email
Holderid = 0; // Setting holder id = 0 as the object being populated are of type header view



swipeDetector = new GestureDetector(itemView.getContext(),
new GestureDetector.SimpleOnGestureListener() {
float x;
float y;

@Override
public boolean onDown(MotionEvent e) {
x = 0;
y = 0;
Log.d("onDown", "here");
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
x -= distanceX;
y -= distanceY;
Log.d("scrolling", "here");
Log.d("view", itemView.toString());

//View view = itemView.findChildViewUnder(e.getX(), e.getY());
if (Math.abs(x) > Math.abs(y)) {
//Horizontal
if (x > 0) {

//posX -= distanceX;
Log.d("no", String.valueOf(distanceX));

//itemView.animate().translationX(distanceX)
// .alpha(0.8f);
//right
} else {
//left
}
}
return true;
}
});


}


@Override
public boolean onTouch(View v, MotionEvent event) {
VelocityTracker velocityTracker = null;
int index = event.getActionIndex();
int action = event.getActionMasked();
int pointerId = event.getPointerId(index);
float downX = 0;
float posX = v.getX();
return swipeDetector.onTouchEvent(event);

最佳答案

GestureDetector 可以很容易地用于确定来自 MotionEvent 的滑动手势。

//Make a GestureDetector
swipeDetector = new GestureDetector(GLView.getContext(),
new SimpleOnGestureListener() {
float x;
float y;
@Override
public boolean onDown (MotionEvent e){
x = 0;
y = 0;
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
x -= distanceX;
y -= distanceY;
if(Math.abs(x) > Math.abs(y)){
//Horizontal
if(x > 0){
//right
}else{
//left
}
}
return true;
}
});



//Feed your MotionEvent to swipeDetector from your View
public boolean onTouchEvent(MotionEvent ev){
return swipeDetector.onTouchEvent(ev);
}

下面是一个带有 GestureDetector 的示例 Adapter,您可能希望从中开始。

public class Adapter extends BaseAdapter {
private int count = 50;

@Override
public int getCount() {
return count;
}

@Override
public Integer getItem(int position) {
return Integer.valueOf(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder h;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.list_item, null);
h = new ViewHolder(convertView);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.tv.setText("" + (position + 1));
h.tv.setTranslationX(0);
return convertView;
}

private static class ViewHolder implements View.OnTouchListener {
View row;
TextView tv;
GestureDetector swipeDetector;

ViewHolder(View convertView) {
row = convertView;
tv = (TextView) convertView.findViewById(R.id.tv);
row.setOnTouchListener(this);
swipeDetector = new GestureDetector(row.getContext(), new GestureDetector
.SimpleOnGestureListener() {
float x;
float y;

@Override
public boolean onDown(MotionEvent e) {
x = 0;
y = 0;
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float
distanceY) {
x -= distanceX;
y -= distanceY;
if (Math.abs(x) > Math.abs(y)) {
//Horizontal
if (x > 0) {
tv.setTranslationX(x);
} else {
//left
}
}
return true;
}
});
}


@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_CANCEL && event.getAction() !=
MotionEvent.ACTION_UP) {
return swipeDetector.onTouchEvent(event);
}
tv.setTranslationX(0);
return true;
}
}
}

关于Android 滑动 - 我应该使用手势监听器还是 MotionEvent.ACTION_MOVE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31324643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com