DragSortListView无法在SwipeRefreshLayout上向下拖动

时间:2014-11-22 16:17:46

标签: android listview drag-and-drop pull-to-refresh swiperefreshlayout

我在SwipeRefreshLayout中使用DragSortListView(https://github.com/bauerca/drag-sort-listview)使其能够进行“pull-to-refresh”和可拖动的listview。这是我的XML:

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_below="@id/div_subtitle"
    android:id="@+id/srl_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.mobeta.android.dslv.DragSortListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        dslv:drag_enabled="true"
        dslv:track_drag_sort="false"
        android:divider="@null"
        dslv:use_default_controller="false"
        tools:listitem="@layout/list_kkategori" />
</android.support.v4.widget.SwipeRefreshLayout>

这是我的listview元素的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:paddingRight="@dimen/right_margin"
    android:paddingLeft="@dimen/left_margin"
    android:background="@color/background"
    android:layout_height="50dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/container_side_dropshadow">

        <ImageView
            android:id="@+id/iv_handler"
            android:layout_width="50dp"
            android:scaleType="fitXY"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/draghandler" />

        <TextView
            android:id="@+id/tv_nama"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/textBlack"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/iv_handler"
            android:text="Nama Kategori"
            android:textSize="17sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_alignParentBottom="true"
            android:background="@color/dividerColor"
            android:layout_marginLeft="@dimen/left_divider_margin"
            android:layout_marginRight="@dimen/right_divider_margin" />

    </RelativeLayout>

</RelativeLayout>

这是我在我的片段onCreateView上调用的DragSortListView初始化:

dslv = (DragSortListView) v.findViewById(android.R.id.list);
dslv.addHeaderView(View.inflate(getActivity(), R.layout.list_header, null), null, false);
dslv.addFooterView(View.inflate(getActivity(), R.layout.list_footer, null), null, false);

dslvController = new DragSortController(dslv);
dslvController.setDragHandleId(R.id.iv_handler);
dslvController.setRemoveEnabled(false);
dslvController.setSortEnabled(true);
dslvController.setDragInitMode(DragSortController.ON_DOWN);
dslvController.setRemoveMode(DragSortController.FLING_REMOVE);
dslv.setFloatViewManager(dslvController);
dslv.setOnTouchListener(dslvController);
dslv.setDragEnabled(true);

然而,不知何故DragSortListView无法向下拖动任何列表项,但拖动功能仍然有效。我怀疑当我向下拖动列表元素时DragSortListView中的onTouchListener被SwipeRefreshLayout的onTouchListener以某种方式覆盖。

此问题是否有任何解决方案或解决方法?我需要在我的应用程序中使用这两项功能,刷卡到刷新和拖动列表视图。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但在我的应用中,列表中有两种状态:第一种 - 当用户可以拖放内容时,第二种 - 当用户可以滑动以刷新整个列表时。

所以,当我的列表处于拖放状态时,我找到了防止刷卡动作的方法。重点是覆盖SwipeRefreshLayout的函数canChildScrollUp(),默认情况下会阻止任何AbsListView的滑动行为,除非在列表的第一行位于顶部时执行操作。我正在检查子视图是否是DragSortListView的实例,如果list.isDragEnabled()则返回true。您也可以在此处实现任何其他逻辑。

这是我的CustomSwipeRefreshLayout:

public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {

private View mTarget;

public CustomSwipeRefreshLayout(Context context) {
    super(context);
}

public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean canChildScrollUp() {
    ensureTarget();
    if (mTarget != null && mTarget instanceof DragSortListView)
        return super.canChildScrollUp()||((DragSortListView)mTarget).isDragEnabled();
    return super.canChildScrollUp();
}

/** 
* Had to copy the method from SwipeRefreshLayout
**/
private void ensureTarget() {
    // Don't bother getting the parent height if the parent hasn't been laid
    // out yet.
    if (mTarget == null) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (! (child instanceof ImageView)) {
            // In fact, refresh icon is instanseof CircleImageView, but the
            // class is private, so I decided to compare with ImageView. 
                mTarget = child;
                break;
            }
        }
    }
}

}

希望,这对某人有帮助。

答案 1 :(得分:0)

使用dslvController.setDragInitMode(DragSortController.ON_LONG_PRESS);代替dslvController.setDragInitMode(DragSortController.ON_DOWN);


我的不好,仍然有拖累的问题。一个肮脏的解决方法是在向下拖动项目之前略微向上移动项目。我知道这有点糟糕,我正在阅读SwipeRefreshLayout上的文档,如果我发现任何内容,将会更新。对不起,错误的解决方案。