将 ListView 拖放到 CardView 列表中

Drag and Drop ListView into CardView list

我正在尝试在 Gridlayout 的每个子项中实现拖放 ListView。 所以我的 GridLayout 由 CardView 组成(它们也可以是 dragged/dropped)。它已经用这个库实现:http://patrick-iv.github.io/2015/05/04/drag-n-drop/ 拖放 ListView 是用这个库实现的:http://nhaarman.github.io/ListViewAnimations/

问题是ListView里面的项目不能拖放,只有GridLayout的滚动移动。 如在我的Activity我有一个GridLayout的ScrollView,我觉得拖放ListView的ScrollView没有考虑到。

并且当我只有一个 CardView(因此 ScrollView 被禁用)时,拖放操作有效!

所以我尝试了一些像下面这样的 hacky,但没有任何改变。

((GripView)rootView.findViewById(R.id.grip_view)).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mParentScrollView.setEnabled(false);
                        break;
                    case MotionEvent.ACTION_UP:
                        mParentScrollView.setEnabled(true);
                        break;
                }
                return true;
            }
        });

存在XML结构:

fragment_manage_program.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.GridLayout
        android:id="@+id/grid_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="1" />

</ScrollView>

fragment_manage_program_item.xml(在GridLayout内部创建)

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/exercise_card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="4dp"
    app:contentPaddingBottom="@dimen/activity_vertical_margin"
    app:contentPaddingLeft="20dp"
    app:contentPaddingRight="@dimen/activity_horizontal_margin"
    app:contentPaddingTop="20dp">

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/exercise_name"
            style="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|left|center"
            android:text="Pushups"
            android:textColor="#f44336" />

        <com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
            android:id="@+id/dynamic_list_view"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

    </LinearLayout>

</android.support.v7.widget.CardView>

fragment_manage_program_set_item.xml(在 DynamicListView 内部创建)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:ignore="UseCompoundDrawables">

    <com.nhaarman.listviewanimations.itemmanipulation.dragdrop.GripView
        android:id="@+id/grip_view"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:color="@android:color/darker_gray"
        android:paddingBottom="4dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp" />

    <TextView
        android:id="@+id/set_summary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:gravity="center_vertical"
        android:minHeight="40dp"
        android:textColor="?android:attr/textColorSecondary"
        android:textSize="20sp"
        tools:ignore="UnusedAttribute" />

</LinearLayout>

好的,我已经用需要 API 级别 21 的解决方案解决了我的问题:

mDragNDropListView.setNestedScrollingEnabled(true);

对于 API 4 级或更高级别,您可以使用 NestedScrollView