2015-12-16 6 views
0

Я пытаюсь реализовать SwipeRefreshLayout в своем приложении. Функциональность приложения отлично работает. SwipeRefreshLayout также отлично работает до момента его запуска, и пользователь снова сбрасывается, тогда происходит то, что SwipeRefreshLayout падает и начинает плохо себя вести. Я заметил, что onRefresh вызывается один раз независимо от того, сколько раз пользователь уходит. Любые предложения о том, что может произойти неправильно? Я загрузил видео youtube проблемы youtube_video.SwipeRefreshLayout не работает должным образом

Это мой 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); 
     } 
    } 
    } 

ответ

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> 

Смежные вопросы