android - Viewpager 与 SwipeRefreshLayout 刷新时图标不会消失

标签 android listview android-fragments android-viewpager swiperefreshlayout

我的问题和这个一模一样。

Frozen snapshot of list rendered with SwipeRefreshLayout in ViewPager

但简而言之,我刷新了列表,列表刷新后旋转的事情仍然继续。

我尝试了他提供的以下解决方案,用 FrameLayout 包装 SwipeRefreshLayout 但没有成功。

这是我现在拥有的屏幕截图,以及我实现此功能的代码。

enter image description here enter image description here

在我的 Fragment 类和 Asynctask 的部分中,

private LocationUtil loc;
private static final String ARG_SORT_TYPE = "sortType";
String sortType = null;
private ArrayList<Facility> results;
private SwipeRefreshLayout mPtrListView;
private ListView waitTimesListView;
SummaryAdapter adapter;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_wait_times_viewpager_fragment,container,false);

    mPtrListView = (SwipeRefreshLayout) view.findViewById(R.id.wait_times_pull_refresh_list);
    waitTimesListView = (ListView) view.findViewById(R.id.wait_times_listview);
    waitTimesListView.setOnItemClickListener(this);
    mPtrListView.setOnRefreshListener(this);

    WaitTimesTask task = new WaitTimesTask(getActivity());
    task.execute(sortType, longitudeLocation, latitudeLocation);
    return view;
}

 @Override
 public void onRefresh() {
    WaitTimesTask task = new WaitTimesTask(getActivity());
    task.execute("name", longitudeLocation, latitudeLocation);
}

public void setLocation(){
    if(loc.useSmartLocation()){
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            Log.v("Check Location frag" + sortType + " " , " " + location.getLongitude() + " "  + location.getLatitude());

            latitudeLocation = Double.toString(location.getLatitude());
            longitudeLocation = Double.toString(location.getLongitude());

        } else {
            Log.v("Check waitTimesActivity" , " is null");
            }
    }
    else {
        loc.determineLocation();
        latitudeLocation = Double.toString(loc.getLatitude());
        longitudeLocation = Double.toString(loc.getLongitude());
    }
}

    @Override
    protected void onPreExecute() {
        dialog = AsyncProgressDialog.show(mContext, "", "");
        setLocation();
    }

    @Override
    protected void onPostExecute(String string) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        if(string != null){
            Log.v("result on post & type: " + Sort, string);

            NovantFacilityXmlParser parser = new NovantFacilityXmlParser();
            string = string.replace("&", "&amp;");
            try {
                results = parser.parse(new ByteArrayInputStream(string.getBytes()));

            } catch (IOException | XmlPullParserException ex) {
                Log.e("Parsing error:", ex.getMessage());
            }

            Log.v("SetUp "," of List");

            if(!results.isEmpty()) {
                adapter = new SummaryAdapter(mContext, R.layout.row_wait_time, results.toArray(new Facility[results.size()]));
                waitTimesListView.setAdapter(adapter);
            }
        }
        else{
            Toast.makeText(getActivity(), "No data available", Toast.LENGTH_LONG).show();
        }
}

我的 xml,包括 fragment 和主要 Activity 。

Activity XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/aubergine">

<android.support.design.widget.TabLayout
    android:id="@+id/wait_times_tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorHeight="3dp"
    app:tabIndicatorColor="#ffffff"
    app:tabSelectedTextColor="#ffffff"
    app:tabMode="fixed"
    android:background="#333333"/>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/wait_times_ViewPager">
    </android.support.v4.view.ViewPager>
</LinearLayout>

XML fragment

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/base_color">
<!--tools:context=".WaitTimesFragment">-->

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/wait_times_pull_refresh_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/wait_times_listview"/>

        </android.support.v4.widget.SwipeRefreshLayout>
    <include layout="@layout/novant_menu_layout" />

在我的 OnpostExecute 中,我从 doinBackground 获取字符串并解析 xml feed 并创建一个数组列表,用于我定制的适配器。

任何帮助将不胜感激。 谢谢。

最佳答案

请在onPostExecute中添加mPtrListView.setRefreshing(false)

关于android - Viewpager 与 SwipeRefreshLayout 刷新时图标不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489047/

相关文章:

Android:字符串资源未按预期工作

android - 是否可以为选项卡标题应用样式

android - 通过代码使 ListView 项发光

mysql - 如何将DeleteParameter值获取到后面的代码中?

android - 如何在 BaseAdapter 中刷新 Listview?

android - 使用 Snackbar.Callback 进行撤消删除操作

java - replace() 不适用于多 fragment

java - Android : How to clip views by parent, 像 CSS 溢出:隐藏

android - 如何使用 android 键盘更改按钮文本?

android - 如何检查用户是否在 Firebase 中通过身份验证?