首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewPager in SwipeRefreshLayout

ViewPager in SwipeRefreshLayout
EN

Stack Overflow用户
提问于 2015-10-30 12:46:07
回答 3查看 2.1K关注 0票数 1

我有ViewPager由带有列表的片段组成。我想把它放在SwipeRefreshLayout,以更新所有的轨道在一次。当我向下滚动任何SvipeRefresh动画中的元素列表时,就会启动。仅当当前列表位于初始位置时,如何创建它?

代码语言:javascript
复制
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;
}` 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-31 11:46:04

谷歌解决这个问题的例子张贴在谷歌示例github项目上。

创建一个扩展原始SwipeRefreshLayout的视图:

代码语言:javascript
复制
/*
 * 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布局中使用此视图:

代码语言:javascript
复制
<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>

在您的活动或片段中,您可以在以下情况下使用此组件:

代码语言:javascript
复制
mSwipeRefreshLayout = (MultiSwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
mSwipeRefreshLayout.setSwipeableChildren(android.R.id.list, android.R.id.empty);

android.R.id.list和android.R.id.empty是您的列表或回收器视图的is。视图很好地处理两个ids相同的列表。

您可以在google-示例github项目上看到真正的示例。

票数 0
EN

Stack Overflow用户

发布于 2015-10-30 12:55:19

您可以在初始位置设置SwipeRefreshLayout刷新启用,在其他位置设置禁用。

票数 0
EN

Stack Overflow用户

发布于 2021-10-23 09:12:00

我有一个FragmentContainer作为SwipeRefreshLayout的子代,而在我的片段中有一个RecyclerView (对于片段来说这是非常正常的)。

SwipeRefreshLayout正在给列表卷轴带来问题,我正在使用这个扩展函数来解决这个问题。方法是不言自明的,尽管我会留下我的评论。我希望它能帮助人们。

代码语言:javascript
复制
/**
 * This method binds [SwipeRefreshLayout] to child [RecyclerView].
 * Normally if RefreshLayout is the direct parent of RecyclerView scrolling is handled internally.
 * However, sometimes we have multiple recyclerViews inside RefreshLayout or recyclerViews are inside
 * a ViewPager. In this cases, scrolling up will have problems. To tackle this scrolling issues we
 * are disabling/enabling refreshLayout depending on the recyclerView.
 *
 * @param refreshLayout to be bounded with.
 * @see [InprogressInspectionFragment.initRecyclerView]
 * and [OutstandingInspectionFragment.initRecyclerView]
 */
fun RecyclerView.refreshLayoutPatched(refreshLayout: SwipeRefreshLayout){
    val marginTop = (layoutParams as ConstraintLayout.LayoutParams).topMargin

    addOnScrollListener(object : RecyclerView.OnScrollListener(){
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            recyclerView.getChildAt(0)?.let{
                refreshLayout.isEnabled =
                    (layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() == 0
                            && recyclerView.getChildAt(0).top == marginTop
            }
        }
    })

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33436317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档