SwipeRefreshLayout无法正常工作

时间:2015-12-16 19:22:01

标签: java android swiperefreshlayout

我正在尝试在我的应用中实现SwipeRefreshLayout。该应用程序的功能正常。 SwipeRefreshLayout也可正常工作,直到它运行并且用户再次拉下来然后会发生什么是SwipeRefreshLayout关闭并开始行为不端。我注意到无论用户拉下多少次都会调用onRefresh一次。什么可能出错?我上传了问题youtube_video的YouTube视频。

这是我片段的xml(我的应用程序使用片段):

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:background="@drawable/bg_gradient"
            tools:context="koemdzhiev.com.stormy.ui.HourlyForecastActivity">

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

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:id="@+id/recyclerView"/>

</android.support.v4.widget.SwipeRefreshLayout>

这是片段中的代码:

    public class Hourly_forecast_fragment extends Fragment {
    private Hour[] mHours;
    private MainActivity mActivity;
    @InjectView(R.id.hourly_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    RecyclerView.LayoutManager layoutManager;
    //inject the RecyclerView as member variable
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = ((MainActivity) getActivity());


    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.hourly_forecast_fragment,container,false);
        ButterKnife.inject(this, v);
        layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.green,R.color.blue,R.color.orange);
        if (mRecyclerView != null)
            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    //if there is internet and if the mSwipeRefreshLayout in the current and daily fragments are not already running...
                    if (mActivity.isNetworkAvailable()) {
                        if (!mActivity.mCurrent_forecast_fragment.mSwipeRefreshLayout.isRefreshing() && !mActivity.mDaily_forecast_fragment.mSwipeRefreshLayout.isRefreshing()) {
                            mActivity.getLocation();
                        }else{
                            mSwipeRefreshLayout.setRefreshing(false);
                            Toast.makeText(mActivity, "currently refreshing...", Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(mActivity, "No Internet Connection!", Toast.LENGTH_LONG).show();
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }
            });
        Log.e("Forecast_fragment", "onCreateView");
        return v;
    }

    public void setUpHourlyFragment(){
        if (mActivity.mForecast != null) {
//            Toast.makeText(mActivity, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
            Hour[] hourlyForecast = mActivity.mForecast.getHourlyForecast();
            mHours = Arrays.copyOf(hourlyForecast, hourlyForecast.length, Hour[].class);

            HourAdapter adapter = new HourAdapter(mActivity, mHours);
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.setHasFixedSize(true);
            layoutManager = new LinearLayoutManager(mActivity);
            mRecyclerView.setLayoutManager(layoutManager);
            //if dealing with fixed size data, it is recommended to do the following...
            mRecyclerView.setHasFixedSize(true);
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

我能够使用ListView而不是RecycleView来解决我的问题。出于某种原因,SwideRefreshLayout与RecycleView的效果不佳。 Current_fragment代码:     

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:divider="@null"
        android:dividerHeight="0dp"/>

</android.support.v4.widget.SwipeRefreshLayout>