DragSortListView cannot drag on SwipeRefreshLayout

I am using DragSortListView ( https://github.com/bauerca/drag-sort-listview ) inside SwipeRefreshLayout so that it can use "pull-to-refresh" and dragable listview, Here is my 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>

      

And this is my XML list item:

<?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>

      

And this is my DragSortListView initialization that I am calling on onCreateView

my fragment of mine:

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);

      

However, somehow the DragSortListView cannot drag any list item, but the drag-and-drop function still works. I suspect that onTouchListener

the DragSortListView is overriding SwipeRefreshLayout in some way when dragging the list item onTouchListener

.

Is there any solution or workaround for this problem? I need both features in my application, Swipe-to-Refresh and Drag listview.

+3


source to share


2 answers


I faced the same problem, but there are 2 list states in my application: the first is when the user can drag and drop content, and the second is when the user can scroll to update the entire list.

So, I found a way how to prevent the screen from updating when my list is in drag & drop state. The point is to override the canChildScrollUp () function of SwipeRefreshLayout, which by default prevents swiping behavior for any AbsListView if no action is taken when the first row of the list is on top. I am checking if the child view is an instance of DragSortListView and returns true if list.isDragEnabled (). You can also implement any other logic.

Here is my 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;
            }
        }
    }
}

      

}

Hope this would help someone.

+1


source


Use dslvController.setDragInitMode(DragSortController.ON_LONG_PRESS);

insteaddslvController.setDragInitMode(DragSortController.ON_DOWN);




I have bad, still drag and drop issue. One dirty workaround is to move the element up a bit before dragging it down. I know this sucks, I read the docs in the SwipeRefreshLayout, update if I find anything. Sorry for the wrong decision.

0


source







All Articles