2014-12-18 4 views
0

Я пытаюсь реализовать функцию Pull to refresh в Android. Для этого я закончил использование SwipeRefresLayout из библиотеки поддержки. Но он не работает так, как я хотел.Внедрить SwipeRefresh Layout

Мне необходимо реализовать его внутри фрагмента. вот код для fragment_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <android.support.v4.widget.SwipeRefreshLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/swipe_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <ListView 
      android:id="@+id/group_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="45dp" /> 
    </android.support.v4.widget.SwipeRefreshLayout> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="bottom" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/banner_ad_unit_id" > 
    </com.google.android.gms.ads.AdView> 
</FrameLayout> 

</LinearLayout> 

И это фрагмент помещается внутри MainActivity.java

public static class HomeFragment extends Fragment { 
    public static final String ARG_CATEGORY_NUMBER = "category_number"; 
    private UiLifecycleHelper uiHelper; 
    public int currentimageindex = 0; 
    private SwipeRefreshLayout swipeLayout; 

    public HomeFragment() { 
     // Empty constructor required for fragment subclasses 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_home, container, 
       false); 

     //SWIPE TO REFRESH 
     swipeLayout = (SwipeRefreshLayout) container.findViewById(R.id.swipe_container); 
     swipeLayout.setOnRefreshListener((OnRefreshListener) getActivity()); 
     swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
       android.R.color.holo_green_light, 
       android.R.color.holo_orange_light, 
       android.R.color.holo_red_light); 


     ListView hlvCategory = (ListView) rootView 
       .findViewById(R.id.group_content); 

     AdView mAdView = (AdView) rootView.findViewById(R.id.adView); 
     HomeJsonData hjd = new HomeJsonData(getActivity(), hlvCategory, 
       mAdView, mDrawerList); 

     hjd.execute(); 

     return rootView; 
    } 

    //SWIPE TO REFRESH 
    public void onRefresh() { 
     new Handler().postDelayed(new Runnable() { 
      @Override public void run() { 
       swipeLayout.setRefreshing(false); 
      } 
     }, 5000); 
    } 
} 

Но это выдает ошибку, как, как это не удалось найти класс для Макет SwipeRefresh. Можете ли вы предложить мне, как я могу реализовать его здесь?

ответ

0

Вы должны осуществлять OnRefreshListener в вашем Fargment

public static class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener 

и установить swipeLayout.setOnRefreshListener(this);

0

Здесь в вашем фрагменте я обнаружил, что проблема с настройки обновления слушателя

public static class HomeFragment extends Fragment implements OnRefreshListener{ 
public static final String ARG_CATEGORY_NUMBER = "category_number"; 
private UiLifecycleHelper uiHelper; 
public int currentimageindex = 0; 
private SwipeRefreshLayout swipeLayout; 

public HomeFragment() { 
    // Empty constructor required for fragment subclasses 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_home, container, 
      false); 

    //SWIPE TO REFRESH 
    swipeLayout = (SwipeRefreshLayout) container.findViewById(R.id.swipe_container); 
    swipeLayout.setOnRefreshListener(this); 
    swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
      android.R.color.holo_green_light, 
      android.R.color.holo_orange_light, 
      android.R.color.holo_red_light); 


    ListView hlvCategory = (ListView) rootView 
      .findViewById(R.id.group_content); 

    AdView mAdView = (AdView) rootView.findViewById(R.id.adView); 
    HomeJsonData hjd = new HomeJsonData(getActivity(), hlvCategory, 
      mAdView, mDrawerList); 

    hjd.execute(); 

    return rootView; 
} 

//SWIPE TO REFRESH 
public void onRefresh() { 
    new Handler().postDelayed(new Runnable() { 
     @Override public void run() { 
      swipeLayout.setRefreshing(false); 
     } 
    }, 5000); 
} 

}

и убедитесь, что вы добавили библиотеку supportV4

попытка, как указано выше, это поможет вам, счастливым кодирования :)