gpt4 book ai didi

java - 当我在 Android 的 listView 中有数据时,滑动刷新不起作用

转载 作者:行者123 更新时间:2023-12-01 10:02:11 24 4
gpt4 key购买 nike

我有一个可以拉动刷新的 fragment 。当 ListView 没有数据时,一切正常,但是,当它至少有一行时,当我向下滑动以刷新它时,它不会使用react。这是 fragment 的 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());
}
}
}

和 fragment 代码:

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() 中所做的事情。

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

}

关于java - 当我在 Android 的 listView 中有数据时,滑动刷新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com