关于SwipeRefreshLayout嵌套ViewPage又嵌套ListView的下滑冲突问题

有个android项目,用TabPageIndicator实现分页,用SwipeRefreshLayout实现下拉刷新,但是出现的问题是:下拉还未到顶部时,就触发SwipeRefreshLayout刷新了。
我的界面类似这样:
[http://static.open-open.com/lib/uploadImg/20170110/20170110202653_236.gif]()
布局:

  <com.prgguru.example.listvideotest.VpSwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        </android.support.v4.view.ViewPager>
    </com.prgguru.example.listvideotest.VpSwipeRefreshLayout>

Activity片段:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_viewpager);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        mTabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator);
        mTabAdapter = new TabAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mTabAdapter);
        mTabPageIndicator.setViewPager(viewPager, 0);
        //下拉刷新
        vpSwipeRefresh = (VpSwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        vpSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshView();
            }
        });
    }

VpSwipeRefreshLayout是我自定义的SwipeRefreshLayout,主要解决ViewPage左右滑动时的冲突,代码如下:

 public class VpSwipeRefreshLayout extends SwipeRefreshLayout {
     private float startY;
    private float startX;
    // 记录viewPager是否拖拽的标记
    private boolean mIsVpDragger;
    private final int mTouchSlop;
    private ViewPager mViewPage;
    private ListView mListView;
    private View contentView;

    public VpSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mViewPage = null;
        mListView = null;
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // 记录手指按下的位置
                startY = ev.getY();
                startX = ev.getX();
                // 初始化标记
                mIsVpDragger = false;
                break;
            case MotionEvent.ACTION_MOVE:
                // 如果viewpager正在拖拽中,那么不拦截它的事件,直接return false;
                if(mIsVpDragger) {
                    return false;
                }

                // 获取当前手指位置
                float endY = ev.getY();
                float endX = ev.getX();
                float distanceX = Math.abs(endX - startX);
                float distanceY = Math.abs(endY - startY);
                // 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理。
                if(distanceX > mTouchSlop && distanceX > distanceY) {
                    mIsVpDragger = true;
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // 初始化标记
                mIsVpDragger = false;
                break;
        }
}

我的TabAdapter:

 public class TabAdapter extends FragmentPagerAdapter {

    public static String[] TITLES = new String[]{"item1","item2","item3"};
    public TabAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        TabFragment fragment = new TabFragment(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

我的TabFragment:

 public class TabFragment extends Fragment {
    private int pos;
    public TabFragment(int pos){
        this.pos= pos;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //R.layout.layout_list 只有一个ListView
        final ListView listView = (ListView) inflater.inflate(R.layout.layout_list, container, false);


        listView.setAdapter(new VideoListAdapter(getActivity(),
                VideoConstant.videoUrls[pos],//视频地址
                VideoConstant.videoTitles[pos],//视频标题
                VideoConstant.videoThumbs[pos]));//视频缩略图
        return listView;
    }

2个回答

这个问题我之前遇到过,其实很好解决,给你思路吧,监听listview的滑动事件(onScrollListener),在其onScroll中判断listview的item是否滑到最顶端,若最顶端则将SwipeRefreshLayout启用,若不是最顶端则禁用( setEnable)

qq_23931287
Newbieforme 回复safei37: listview.setOnScrollListener
接近 3 年之前 回复
safei37
立风飞 那要在哪里监听呢?Activity还是TabFragment?我在TabFragment监听每次都闪退,还是说要在Activity里监听?如何监听?
接近 3 年之前 回复

问题就变成,我在Activity里找不到SwipeRefreshLayout,在TabFragment则找不到ListView来做这件事

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐