简体   繁体   中英

How to use Listview with pull to refresh and swipe to delete

I am using swipe to delete in my bu using this library. and Also using the swipe to refresh and for it I am using android.support.v4.widget.SwipeRefreshLayout so the design is good and all looks good when I swipe down to refresh. My list gets refresh and contents are updated.

Following is my layout

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"

>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true">
<com.baoyz.swipemenulistview.SwipeMenuListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="5.0sp"
    android:divider="@android:color/transparent"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center">
    <TextView
        android:id="@+id/swipeRefreshLayout_emptyView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:visibility="gone"
        android:gravity="center"
        /></ScrollView>
</FrameLayout>

Problem :

As I want to delete the item by using the swipe to delete , for this I have to swipe from right to left to open the menu of delete and it gets open but When I touch on it nothing happens.

I am on the result now and I think that two times of gesture are intercepting each other. I did debug and nothing my menu for deleting the item is not listening for click.

Please help I am stuck in the middle. Any suggestion you can help me with ?

Update 1

I debug the code and it is showing me following in the log when Ever I click to delete button in the menu

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

I think it is something that is ignoring my touch to delete the item

The best way as here in this answer is to disable sweipRefreshLayout when start sweeping menu and enable it again when finish as this code

list.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
        @Override
        public void onSwipeStart(int position) {
            mySwipeRefresh.setEnabled(false);
        }

        @Override
        public void onSwipeEnd(int position) {
            mySwipeRefresh.setEnabled(true);
        }
    });

I hope it helps as it works for me.

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center">


    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

java side

private static final String LOG_TAG = "MySwipeListener";
private SwipeRefreshLayout swipeRefresh;

swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");
                yourAdapter.swipeRefresh();
                swipeRefresh.setRefreshing(false);
            }
        });

Try it!

listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
    switch (index) {
    case 0:

        break;
    case 1:
        // delete
  youListData.remove(position);
  adapter.notifydatasetchanged()
        break;
    }
    // false : close the menu; true : not close the menu
    return false;
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM