Android刷入列表
是否有人有一个简单的ListActivity示例,在列中显示文本视图,当您从左向右滑动时,您会在新视图中看到该行?这意味着编辑该行的数据或显示该行的更详细信息。请不要参考幕府代码或其他网站,因为我在谷歌上搜索过,没有看到答案。这里有一个我用来检测刷击的代码片段。然后,您可以使用更改视图Android刷入列表,android,list,textview,listactivity,swipe,Android,List,Textview,Listactivity,Swipe,是否有人有一个简单的ListActivity示例,在列中显示文本视图,当您从左向右滑动时,您会在新视图中看到该行?这意味着编辑该行的数据或显示该行的更详细信息。请不要参考幕府代码或其他网站,因为我在谷歌上搜索过,没有看到答案。这里有一个我用来检测刷击的代码片段。然后,您可以使用更改视图 @Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTou
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
} else {
return false;
}
}
private static final int SWIPE_MIN_DISTANCE = 30;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
leftFling();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
rightFling();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
我也有同样的问题,我在这里找不到答案 我想在ListView项目中检测一个滑动动作并将其标记为滑动,同时继续支持OnItemClick和OnItemLongClick 以下是我的解决方案: 第一类SwipeDetector:
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class SwipeDetector implements View.OnTouchListener {
public static enum Action {
LR, // Left to Right
RL, // Right to Left
TB, // Top to bottom
BT, // Bottom to Top
None // when no action was detected
}
private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;
public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}
public Action getAction() {
return mSwipeDetected;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.None;
return false; // allow other events like Click to be processed
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
Log.i(logTag, "Swipe Left to Right");
mSwipeDetected = Action.LR;
return false;
}
if (deltaX > 0) {
Log.i(logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return false;
}
} else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe
// detection
// top or down
if (deltaY < 0) {
Log.i(logTag, "Swipe Top to Bottom");
mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0) {
Log.i(logTag, "Swipe Bottom to Top");
mSwipeDetected = Action.BT;
return false;
}
}
return false;
}
return false;
}
}
导入android.util.Log;
导入android.view.MotionEvent;
导入android.view.view;
公共类SwipeDetector实现View.OnTouchListener{
公共静态枚举操作{
LR,//从左到右
RL,//从右到左
TB,//从上到下
BT,//从下到上
无//未检测到任何操作时
}
私有静态最终字符串logTag=“SwipeDetector”;
专用静态最终整数最小距离=100;
私人浮动downX,downY,upX,upY;
私有操作mSwipeDetected=操作。无;
公共布尔swipedtected(){
返回mSwipeDetected!=操作。无;
}
公共行动{
检测到返回消息;
}
@凌驾
公共布尔onTouch(视图v,运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
downX=event.getX();
downY=event.getY();
mSwipeDetected=操作。无;
return false;//允许处理单击等其他事件
case MotionEvent.ACTION\u UP:
upX=event.getX();
upY=event.getY();
浮动deltaX=downX-upX;
浮动三角洲=downY-upY;
//水平扫描检测
如果(数学绝对值(deltaX)>最小距离){
//左还是右
如果(deltaX<0){
Log.i(logTag,“从左向右滑动”);
mSwipeDetected=Action.LR;
返回false;
}
如果(deltaX>0){
Log.i(logTag,“从右向左滑动”);
mSwipeDetected=Action.RL;
返回false;
}
}else if(Math.abs(deltaY)>最小距离){//垂直滑动
//检测
//自上而下
如果(三角洲<0){
Log.i(logTag,“从上到下滑动”);
mSwipeDetected=Action.TB;
返回false;
}
如果(三角洲>0){
Log.i(logTag,“从下到上滑动”);
mSwipeDetected=Action.BT;
返回false;
}
}
返回false;
}
返回false;
}
}
第二,我在列表视图中使用滑动检测器类:
final ListView lv = getListView();
final SwipeDetector swipeDetector = new SwipeDetector();
lv.setOnTouchListener(swipeDetector);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (swipeDetector.swipeDetected()){
// do the onSwipe action
} else {
// do the onItemClick action
}
}
});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
if (swipeDetector.swipeDetected()){
// do the onSwipe action
} else {
// do the onItemLongClick action
}
}
});
final ListView lv=getListView();
最终SwipeDetector SwipeDetector=新SwipeDetector();
lv.setOnTouchListener(swipeDetector);
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
if(swipedtector.swipedtected()){
//执行onswip操作
}否则{
//执行mclick操作
}
}
});
lv.setOnItemLongClickListener(新的OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
if(swipedtector.swipedtected()){
//执行onswip操作
}否则{
//执行长单击操作
}
}
});
这样,我可以支持3个操作-滑动、单击、长时间单击,并且我可以使用ListView项目信息
以后添加:
由于ListView捕捉到滚动动作,所以有时很难滑动。为了修复它,我对SwipedTector.onTouch进行了以下更改:
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.None;
return false; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
Log.i(logTag, "Swipe Left to Right");
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
Log.i(logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return true;
}
} else
// vertical swipe detection
if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
Log.i(logTag, "Swipe Top to Bottom");
mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0) {
Log.i(logTag, "Swipe Bottom to Top");
mSwipeDetected = Action.BT;
return false;
}
}
return true;
}
}
return false;
}
public boolean onTouch(视图v,运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:{
downX=event.getX();
downY=event.getY();
mSwipeDetected=操作。无;
return false;//允许处理单击等其他事件
}
case MotionEvent.ACTION\u移动:{
upX=event.getX();
upY=event.getY();
浮动deltaX=downX-upX;
浮动三角洲=downY-upY;
//水平扫描检测
if(数学绝对值(deltaX)>水平最小距离){
//左还是右
如果(deltaX<0){
Log.i(logTag,“从左向右滑动”);
mSwipeDetected=Action.LR;
返回true;
}
如果(deltaX>0){
Log.i(logTag,“从右向左滑动”);
mSwipeDetected=Action.RL;
返回true;
}
}否则
//垂直扫描检测
if(数学绝对值(deltaY)>垂直最小距离){
//自上而下
如果(三角洲<0){
Log.i(logTag,“从上到下滑动”);
mSwipeDetected=Action.TB;
返回false;
}
如果(三角洲>0){
Log.i(logTag,“从下到上滑动”);
mSwipeDetected=Action.BT;
返回false;
}
}
返回true;
}
}
返回false;
}
这里是
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
if(!isSwipe)
{
adapter.increase(arg2);
adapter.notifyDataSetChanged();
}
}
});
listView.setOnTouchListener(new OnTouchListener() {
private int action_down_x = 0;
private int action_up_x = 0;
private int difference = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
action_down_x = (int) event.getX();
isSwipe=false; //until now
break;
case MotionEvent.ACTION_MOVE:
if(!isSwipe)
{
action_up_x = (int) event.getX();
difference = action_down_x - action_up_x;
if(Math.abs(difference)>50)
{
Log.d("action","action down x: "+action_down_x);
Log.d("action","action up x: "+action_up_x);
Log.d("action","difference: "+difference);
//swipe left or right
if(difference>0){
//swipe left
Log.d("action","swipe left");
adapter.decrease(selectedItem);
adapter.notifyDataSetChanged();
}
else{
//swipe right
Log.d("action","swipe right");
}
isSwipe=true;
}
}
break;
case MotionEvent.ACTION_UP:
Log.d("action", "ACTION_UP - ");
action_down_x = 0;
action_up_x = 0;
difference = 0;
break;
}
return false; //to allow the clicklistener to work after
}
})