使用垂直视图寻呼机的SwipeRefreshLayout仅刷新最顶层的片段

时间:2015-06-23 13:37:17

标签: android android-fragments android-viewpager vertical-scrolling swiperefreshlayout

我已尝试在我的代码中实现swiperefreshlayout,但它会不断刷新整个寻呼机,而不仅仅是在第一个Fragment即第0 {{1}时刷新}}。 我尝试将refresh设置为false,如图所示,但加载程序仍然出现,并且当我再次返回第0 Fragment时不会重置。 设置Fragment后,我无法在任何地方刷新它。 有人可以建议替代方案吗? 这是我的swiperefreshlayout.enable(false)文件:

XML

这是我的活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentLinearLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <lbbdapp.littleblackbook.com.littleblackbook.Support.VerticalViewPager
                android:id="@+id/verticalViewPager"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

这是我的垂直public class FeedActivity extends ActionBarActivity { PageAdapter mPageAdapter; ProgressBar mProgressBar; ProgressDialog prog; ViewPager pager; String article_id; private float x1, x2; SwipeRefreshLayout mSwipeRefreshLayout; ArrayList<FragmentObject> mObjectsDataList; ArrayList<ArticleObject> mArticleDataList=new ArrayList<ArticleObject>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.feed_layout); mObjectsDataList = new ArrayList<FragmentObject>(); DataFetcherTask myTask = new DataFetcherTask(); myTask.execute(); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_blue_dark, android.R.color.holo_purple, android.R.color.darker_gray); pager=(ViewPager)findViewById(R.id.verticalViewPager); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { if(pager.getCurrentItem()==0) { Log.wtf("current item is 0", "0"); mSwipeRefreshLayout.setRefreshing(true); Log.wtf("Swiping", "Refreshing"); (new Handler()).postDelayed(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(false); DataFetcherTask myTask = new DataFetcherTask(); myTask.execute(); } }, 3000); } else{ mSwipeRefreshLayout.setRefreshing(false); } } }); }

ViewPager

2 个答案:

答案 0 :(得分:0)

不是在ViewGroup上扩展SwipeRefesh,而是在ViewPager的每个片段中使用它。 现在解决了这个问题后,您需要检查ViewPager和SwipeRefresh的向下滚动手势。

public class NEWSwipeRefresh extends SwipeRefreshLayout {

    private View mTargetView;

    public NEWSwipeRefresh(Context context) {
        this(context, null);
    }

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

    public void setTarget(View view) {
        mTargetView = view;
    }

    @Override
    public boolean canChildScrollUp() {
        if (mTargetView instanceof YOURVIEW) {
            final StaggeredGridView absListView = (YOURVIEW)mTargetView;
            return absListView.getChildCount() > 0
                    && (absListView.getFirstVisiblePosition() > 0 || absListView
                            .getChildAt(0).getTop() < absListView
                            .getPaddingTop());
        } else {
            return super.canChildScrollUp();
        }
    }

}

通常刷卡刷新返回-1并且它现在向它的父级提供滑动手势我已经在这里覆盖了listview / gridview以便它可以检查可见项目,如果它在顶部那么它将提供滑动以父母或其他方式将其交给孩子。

答案 1 :(得分:0)

嗯,我试图在几天之后解决这个问题,这很简单。只需将SwipeRefreshLayout作为ViewPager的父元素,然后使用SwipeRefreshLayout禁用特定片段上的OnPageChangeListener,如下所示:

//  viewpager change listener
 OnPageChangeListener viewPagerPageChangeListener = new OnPageChangeListener() {

  @Override
  public void onPageSelected(int position) {

     //for the 0th pos disable SwipeRefreshLayout;
     if(position != 0){
        swipeRefreshLayout.setEnabled(false);
     }else swipeRefreshLayout.setEnabled(true);

  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {

  }

  @Override
  public void onPageScrollStateChanged(int arg0) {

  }};