刷新图标时,使用SwipeRefreshLayout的Viewpager不会消失

时间:2016-07-20 19:16:35

标签: 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

在我的片段类和我的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,片段和主要活动。

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

<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并创建一个arraylist,用于我自定义的适配器。

非常感谢任何帮助。 感谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我认为它有点晚了,但未来可能对某人有所帮助

你必须在被覆盖的 onRefresh()方法中调用setRefreshing(false)

@Override
 public void onRefresh() {
    WaitTimesTask task = new WaitTimesTask(getActivity());
    task.execute("name", longitudeLocation, latitudeLocation);
    mPtrListView.setRefreshing(false); //this method call will resolve your problem 
}