2015-10-30 3 views
1

У меня есть ViewPager состоит из фрагментов со списками. Я хочу поместить его в SwipeRefreshLayout, чтобы обновить все треки сразу. Когда я прокручиваю список элементов из любой анимации SvipeRefresh, начинается. Как создать это только тогда, когда текущий список находится в начальной позиции? `ViewPager в SwipeRefreshLayout

public static MainList getInstanse(int position){ 
     MainList mainList = new MainList(); 
     Bundle args = new Bundle(); 
     args.putInt("pos", position); 
     mainList.setArguments(args); 
     return mainList; 
} 




     Bundle bundle= getArguments(); 
     bs = new AdapterForListView(); 
     lvMain = (ListView) rootView.findViewById(R.id.lvMain); 

     if(bundle!=null) { 

       bs = new AdapterForListView(); 
       addItems(bundle.getInt("pos")); 
       bs.InForEntries(getActivity(), itemsArray); 
       lvMain.setAdapter(bs); 
       lvMain.setDividerHeight(2); 

       lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
           Intent intent = new Intent(getActivity(), ViewMagazin.class); 

           intent.putExtra("EXTRA_SESSION_ID", itemsArray.get(position).getIdSale()); 
           startActivity(intent); 
         } 
       }); 
       //View c = lvMain.getChildAt(0); 
       //scrollyF = -c.getTop() + lvMain.getFirstVisiblePosition() * c.getHeight(); 
     } 

     return rootView; 
}` 

ответ

0

Google разрешает эту проблему с примером, опубликованным в проекте gigaub-образца gogle.

Создайте представление, которое расширяет оригинальный SwipeRefreshLayout:

/* 
* Copyright 2014 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.example.android.swiperefreshmultipleviews; 

import android.content.Context; 
import android.support.v4.view.ViewCompat; 
import android.support.v4.widget.SwipeRefreshLayout; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.AbsListView; 

/** 
* A descendant of {@link android.support.v4.widget.SwipeRefreshLayout} which supports multiple 
* child views triggering a refresh gesture. You set the views which can trigger the gesture via 
* {@link #setSwipeableChildren(int...)}, providing it the child ids. 
*/ 
public class MultiSwipeRefreshLayout extends SwipeRefreshLayout { 

    private View[] mSwipeableChildren; 

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

    public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    /** 
    * Set the children which can trigger a refresh by swiping down when they are visible. These 
    * views need to be a descendant of this view. 
    */ 
    public void setSwipeableChildren(final int... ids) { 
     assert ids != null; 

     // Iterate through the ids and find the Views 
     mSwipeableChildren = new View[ids.length]; 
     for (int i = 0; i < ids.length; i++) { 
      mSwipeableChildren[i] = findViewById(ids[i]); 
     } 
    } 

    // BEGIN_INCLUDE(can_child_scroll_up) 
    /** 
    * This method controls when the swipe-to-refresh gesture is triggered. By returning false here 
    * we are signifying that the view is in a state where a refresh gesture can start. 
    * 
    * <p>As {@link android.support.v4.widget.SwipeRefreshLayout} only supports one direct child by 
    * default, we need to manually iterate through our swipeable children to see if any are in a 
    * state to trigger the gesture. If so we return false to start the gesture. 
    */ 
    @Override 
    public boolean canChildScrollUp() { 
     if (mSwipeableChildren != null && mSwipeableChildren.length > 0) { 
      // Iterate through the scrollable children and check if any of them can not scroll up 
      for (View view : mSwipeableChildren) { 
       if (view != null && view.isShown() && !canViewScrollUp(view)) { 
        // If the view is shown, and can not scroll upwards, return false and start the 
        // gesture. 
        return false; 
       } 
      } 
     } 
     return true; 
    } 
    // END_INCLUDE(can_child_scroll_up) 

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

Используйте этот вид в макете XML:

<com.example.android.swiperefreshmultipleviews.MultiSwipeRefreshLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/swiperefresh" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <!-- your code --> 

</com.example.android.swiperefreshmultipleviews.MultiSwipeRefreshLayout> 

А в вашей деятельности или фрагмента, вы можете использовать этот компонент с:

mSwipeRefreshLayout = (MultiSwipeRefreshLayout) view.findViewById(R.id.swiperefresh); 
mSwipeRefreshLayout.setSwipeableChildren(android.R.id.list, android.R.id.empty); 

android.R.id.list and android.R.id.empty - это идентификаторы для вашего списка или просмотр ресайклеров. Представление отлично работает с двумя списками с одинаковыми идентификаторами.

Вы можете увидеть настоящий пример на google-sample github project.

0

Вы можете установить SwipeRefreshLayout обновления включен, когда ViewPager в исходное положение и отключается, если в другом положении.

+0

Да, но как узнать, какая позиция ListView первый элемент? –

+0

getFirstvisiablePosition() == 0 указывает, что listview находится в исходном положении. – starkshang

+0

thx FireSun приятно)) –

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