我有一个片段拉动刷新。当列表视图没有数据时,一切正常,但是当它有一行时,当我向下滑动以刷新它时,它没有反应。这是片段的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;
}
}
答案 0 :(得分:1)
你没有提供doUpdate(视图)中发生的事情的详细信息,无论如何我在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);
}