gpt4 book ai didi

Android ListView 向右/向左滑动,如通话记录

转载 作者:IT老高 更新时间:2023-10-28 21:53:00 26 4
gpt4 key购买 nike

我有一个 Android 应用程序,其中有像这样的 ListView

enter image description here

现在我想以相同的方式在列表项的右/左滑动上执行两个不同的 Activity 本地调用日志的工作原理

右滑列表

enter image description here

我想要这种类型的滑动。任何人都可以知道如何实现它。基本上我想实现 SimpleOnGestureListener.

我已阅读 sir gav Fling gesture detection on grid layout 回复的帖子之后,我成功地实现了向左滑动和向右滑动检测,但唯一没有发现发生滑动的列表项。

更新 2013 年 5 月 24 日

现在我可以使用此代码检测滑动操作 --

SwipeDetector.java

/**
* Class swipe detection to 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 // Action not found
}

private static final int HORIZONTAL_MIN_DISTANCE = 30; // The minimum
// distance for
// horizontal swipe
private static final int VERTICAL_MIN_DISTANCE = 80; // The minimum distance
// for vertical
// swipe
private float downX, downY, upX, upY; // Coordinates
private Action mSwipeDetected = Action.None; // Last action

public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}

public Action getAction() {
return mSwipeDetected;
}

/**
* Swipe detection
*/@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_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) {

mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {

mSwipeDetected = Action.RL;
return true;
}
} else

// vertical swipe detection
if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
// top or down
if (deltaY < 0) {

mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0) {

mSwipeDetected = Action.BT;
return false;
}
}
return true;
}
}
return false;
}

}

现在以这种方式将它与您的 ListView 一起使用

    // Set the touch listener
final SwipeDetector swipeDetector = new SwipeDetector();
lv.setOnTouchListener(swipeDetector);

在您的 setOnItemClickListener 中,您可以检测到像这样的滑动事件

lv.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView <? > parent, View view,
int position, long id) {

if (swipeDetector.swipeDetected()) {
if (swipeDetector.getAction() == SwipeDetector.Action.LR) {

Toast.makeText(getApplicationContext(),
"Left to right", Toast.LENGTH_SHORT).show();

}
if (swipeDetector.getAction() == SwipeDetector.Action.RL) {

Toast.makeText(getApplicationContext(),
"Right to left", Toast.LENGTH_SHORT).show();

}
}
}
});

但我仍然无法像这样为滑动设置动画:

enter image description here

最佳答案

您可以使用 Fragments 来满足此要求。使用 FragmentPagerAdapter 并覆盖 getItem(int position) 它将返回 fragment 。将适配器设置为 viewPager,在 setOnPageChangeListener 中可以有调用不同 fragment 的逻辑。

雪莉

关于Android ListView 向右/向左滑动,如通话记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017988/

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