2016-03-24 4 views
0

Итак, у меня есть активность с ViewPager и 2 фрагментами в нем. В каждом фрагменте я помещаю SwipeToRefreshLayout и ListView. Xml выглядит так:Проблема с SwipeToRefresh layout в фрагменте ViewPager

<?xml version="1.0" encoding="utf-8"?> 

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

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_going_out" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_tab_new"/> 


</RelativeLayout> 


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

Теперь возникает проблема. Когда список превышает мой экран, и мне нужно прокрутить вниз, чтобы увидеть элементы, когда я пытаюсь выполнить прокрутку, оно не прокручивается, оно обновляется (вызывается SwipeToRefreshLayout). Как я могу это исправить?

ответ

1

Взятые из android docs:

Они используют ListFragment, но вы должны быть в состоянии сделать необходимые изменения: Вы должны добавить и использовать пользовательские SwipeRefreshLayout, где вы определяете условия для скроллинга:

private class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout { 

    public ListFragmentSwipeRefreshLayout(Context context) { 
     super(context); 
    } 

    /** 
    * As mentioned above, we need to override this method to properly signal when a 
    * 'swipe-to-refresh' is possible. 
    * 
    * @return true if the {@link android.widget.ListView} is visible and can scroll up. 
    */ 
    @Override 
    public boolean canChildScrollUp() { 
     final ListView listView = getListView(); 
     if (listView.getVisibility() == View.VISIBLE) { 
      return canListViewScrollUp(listView); 
     } else { 
      return false; 
     } 
    } 

} 

/** 
* Utility method to check whether a {@link ListView} can scroll up from it's current position. 
* Handles platform version differences, providing backwards compatible functionality where 
* needed. 
*/ 
private static boolean canListViewScrollUp(ListView listView) { 
    if (android.os.Build.VERSION.SDK_INT >= 14) { 
     // For ICS and above we can call canScrollVertically() to determine this 
     return ViewCompat.canScrollVertically(listView, -1); 
    } else { 
     // Pre-ICS we need to manually check the first visible item and the child view's top 
     // value 
     return listView.getChildCount() > 0 && 
       (listView.getFirstVisiblePosition() > 0 
         || listView.getChildAt(0).getTop() < listView.getPaddingTop()); 
    } 
} 
+0

это один работает как шарм для ListView. Спасибо. –

0

Попробуйте добавить Swipe для обновления макета только в ListView.

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
android:id="@+id/going_out_activity_swipe_to_refresh" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical">* 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_going_out" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_tab_new"/> 


</RelativeLayout> 

1

Это ошибка при использовании ListView с SwipeToRefreshLayout. вы можете легко исправить это, используя RecyclerView вместо ListView.

Но если вы хотите, чтобы использовать ListView, попробуйте с ниже код:

mListView.setOnScrollListener(new AbsListView.OnScrollListener() { 
      @Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
       boolean enable = false; 
       if (ptrListView != null && mListView.getChildCount() > 0) { 
        boolean firstItemVisible = (mListView.getFirstVisiblePosition() == 0); 
        boolean topOfFirstItemVisible = (mListView.getChildAt(0).getTop() == 0); 
        enable = firstItemVisible && topOfFirstItemVisible; 
       } 
       mSwipeRefreshLayout.setEnabled(enable); 
      } 
     }); 
0

Это, как я это сделал, она прокручивается прекрасно и только освежает, когда вы проведите пальцем вниз, когда вы все пути к вершина. Надеюсь, это поможет.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical" 
tools:context="com.newthinktank.listviewexample2.app.MainActivity" 
android:background="@drawable/background2" 
android:layout_marginTop="65dp" 
android:weightSum="1"> 

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

    <ListView 
     android:id="@+id/lv1" 
     android:layout_marginTop="15dp" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:background="#f0f0f0" 
     android:alpha="0.90" 
     android:layout_weight="1.01" /> 

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

Это, как я использую swipetorefresh:

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout); 

    mSwipeRefreshLayout.setOnRefreshListener(
      new SwipeRefreshLayout.OnRefreshListener() { 
       @Override 
       public void onRefresh() { 
        finish(); 
        startActivity(getIntent()); 
       } 
      } 
    ); 
Смежные вопросы