Android列表视图,滑动到动作

时间:2013-08-22 16:45:07

标签: android listview swipe

我有一些文字列表视图。我想在滑动动作上显示图像(几乎像在gmail应用程序中)。例如,如果我从左向右滑动 - 我的列表项向右移动,图像从左侧滑动。我的列表项可以向右移动不超过图像宽度。滑动停止后,列表项目将移动到开始位置。我怎么能做这样的事情? example

3 个答案:

答案 0 :(得分:2)

结帐this

在那里你可以看到列表视图向右滑动以显示视图。

<强>对齐:

enter image description here

<强> main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:swipe="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

   <com.fortysevendeg.swipelistview.SwipeListView
            android:id="@+id/example_swipe_lv_list"
            android:listSelector="#00000000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            swipe:swipeFrontView="@+id/front"
            swipe:swipeBackView="@+id/back"
            swipe:swipeDrawableChecked="@drawable/choice_selected"
            swipe:swipeDrawableUnchecked="@drawable/choice_unselected"
            swipe:swipeCloseAllItemsWhenMoveList="true"
            swipe:swipeMode="both"
            />

</LinearLayout>

答案 1 :(得分:1)

你可以使用gesturedetector的onFling()事件。它将为您提供动作事件点和触摸速度点,您可以使用它来识别手势,并可以使用从左到右和从右到左的动画加载视图。

答案 2 :(得分:1)

我找不到任何能够做到这一点的图书馆,但这并不难做到。正如尼克指出的那样,看看android-swipetodismiss,看看SwipeDismissListViewTouchListener.java

首先,您必须为View的孩子创建自定义ListView。您需要一个ImageView,它与包含该文本的另一个View重叠。

然后,您向onTouch()添加ListView听众。在MotionEvent.ACTION_DOWN上(当您开始手势时),您可以使用触摸坐标并计算您正在触摸的ListView中的哪个孩子,例如:

        int[] listViewCoords = new int[2];
        mListView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

MotionEvent.ACTION_MOVE上,您可以衡量在MotionEvent.ACTION_DOWN中触及的点与当前坐标之间X坐标的差异:

float deltaX = motionEvent.getRawX() - mDownX;

然后,您翻译了包含文字的View,以便显示ImageView

mDownView.setTranslationX(deltaX);

当您抬起手指时,只需设置mDownView .setTranslationX(0)即可再次覆盖图像。这只是一个指南,有些细节没有涉及,但你应该可以从这开始。