繁体   English   中英

当我在listView Android中有数据时,刷卡刷新不起作用

[英]Swipe to refresh not working when I have data in listView Android

我有一个片段,可以刷新。 当列表视图没有数据时,一切正常,但是,当列表视图至少有一行时,当我向下滑动以刷新它时,它没有任何反应。 这是该片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<share.advice.khalifa.adviceshare.view.ListFragmentSwipeRefreshLayout
    android:id="@+id/going_out_activity_swipe_to_refresh"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_going_out"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_event"/>


        <ListView
            android:id="@+id/going_out_activity_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/layout_padding"
            android:background="@drawable/transparentna"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbars="none"
            android:visibility="visible"/>

    </RelativeLayout>



</FrameLayout>


</share.advice.khalifa.adviceshare.view.ListFragmentSwipeRefreshLayout>

这是我的自定义ListFragmentSwipeRefreshLayout:

public class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout {

private ListView mListView;

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

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

public ListView getmListView() {
    return mListView;
}

public void setmListView(ListView mListView) {
    this.mListView = mListView;
}

/**
 * As mentioned above, we need to override this method to properly signal when a
 * 'swipe-to-refresh' is possible.
 *
 * @return true if the {@link android.widget.ListView} is visible and can scroll up.
 */
@Override
public boolean canChildScrollUp() {
    final ListView listView = getmListView();
    if (listView.getVisibility() == View.VISIBLE) {
        return canListViewScrollUp(listView);
    } else {
        return false;
    }
}


/**
 * Utility method to check whether a {@link ListView} can scroll up from it's current position.
 * Handles platform version differences, providing backwards compatible functionality where
 * needed.
 */
private static boolean canListViewScrollUp(ListView listView) {
    if (android.os.Build.VERSION.SDK_INT >= 14) {
        // For ICS and above we can call canScrollVertically() to determine this
        return ViewCompat.canScrollVertically(listView, -1);
    } else {
        // Pre-ICS we need to manually check the first visible item and the child view's top
        // value
        return listView.getChildCount() > 0 &&
                (listView.getFirstVisiblePosition() > 0
                        || listView.getChildAt(0).getTop() < listView.getPaddingTop());
    }
}
}

和片段代码:

public class SuggestionGoingOutFragment extends Fragment implements ActivityAdapter.onContactsClickListener {

    private Model mModel;

    private OnActivityItemClicked mCallback;

    public static ActivityAdapter mActivityAdapter;

    private ListFragmentSwipeRefreshLayout mSwipeToRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_going_out, container, false);

        mModel = Model.getInstance(getActivity());
        mSwipeToRefresh = (ListFragmentSwipeRefreshLayout) view.findViewById(R.id.going_out_activity_swipe_to_refresh);
        mSwipeToRefresh.setmListView(((ListView) view.findViewById(R.id.going_out_activity_list_view)));
        mSwipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeToRefresh.setRefreshing(true);
                doUpdate(view);
            }
        });
        initView(view);

        return view;
    }
    }

您没有提供doUpdate(view)中发生的事情的详细信息,无论如何,这就是我在onRefresh()所做的onRefresh()

    @Override
        public void onRefresh() {

            refreshLayout.setRefreshing(true);

// create a handler to run after some milli seconds
// get data
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    // call method for updated data for the list view
                    getlistviewdata();
// create new adapter with the new data
                    recyclerAdapter = new RecyclerAdapter(MainArrayList);
        recyclerView.setAdapter(recyclerAdapter);

                    recyclerAdapter.notifyDataSetChanged();
                    refreshLayout.setRefreshing(false);
                }
            }, 2000);

        }

暂无
暂无

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

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