繁体   English   中英

如何使用带有拉动刷新和滑动删除的 Listview

[英]How to use Listview with pull to refresh and swipe to delete

我正在使用库在我的 bu 中使用 swipe 删除。 并且还使用滑动来刷新,为此我使用android.support.v4.widget.SwipeRefreshLayout所以设计很好,当我向下滑动刷新时,一切看起来都不错。 我的列表得到刷新并更新内容。

以下是我的布局

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

问题 :

因为我想通过使用 swipe to delete 来删除项目,为此我必须从右向左滑动以打开删除菜单,它会打开但是当我触摸它时什么也没有发生。

我现在在结果上,我认为两次手势正在相互拦截。 我做了调试,我删除项目的菜单没有没有监听点击。

请帮助我被困在中间。 你有什么建议可以帮助我吗?

更新 1

我调试了代码,当我单击菜单中的删除按钮时,它会在日志中显示以下内容

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

我认为这是忽略我删除该项目的触摸

这个答案中最好的方法是在开始扫描菜单时禁用 sweipRefreshLayout 并在完成时再次启用它作为此代码

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

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

我希望它对我有用。

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

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

尝试一下!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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