排便事件于母公司滚动型列表视图时,在顶部/底部 [英] Pass motion event to parent Scrollview when Listview at top/bottom

查看:221
本文介绍了排便事件于母公司滚动型列表视图时,在顶部/底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 滚动型来显示评论,我想做到以下几点:

I have a ListView in a ScrollView to show comments and I would like to do the following:

当用户扫描下来,首先是滚动型应充分向下滚动的列表是在底部。一旦它完全下来, Listiew 应该开始滚动。

When the user swipes down, first the ScrollView should fully scroll down as the list is at the bottom. Once it's fully down, the Listiew should start scrolling.

同样,当用户滚动起来,首先的ListView (这里本末倒置!)应该向上滚动,在滚动型开始滚动。

Similarly, when the user is scrolling up, first the ListView (order reversed here!) should scroll up, before the ScrollView starts scrolling.

到目前为止,我也做了以下内容:

So far I have done the following:

listView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // If going up but the list is already at up, return false indicating we did not consume it.
            if(event.getAction() == MotionEvent.ACTION_UP) {
                if (listView.getChildCount() == 0 && listView.getChildAt(0).getTop() == 0) {
                    Log.e("Listview", "At top!");
                    return false;
                }
            }

            // Similar behaviour but when going down check if we are at the bottom.
            if( event.getAction() == MotionEvent.ACTION_DOWN) {
                if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1 &&
                        listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight()) {
                    Log.e("Listview","At bottom!");
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            }
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

日志触发在正确的时刻,然而滚动型尽管我返回false不会移动。

The logs trigger at the right moment, however the ScrollView won't move even though I return false.

我也尝试添加 v.getParent()requestDisallowInterceptTouchEvent(假); 的声明,但没有任何工作

I also tried to add v.getParent().requestDisallowInterceptTouchEvent(false); to the statements, but that did not work either.

我怎样才能使它工作?

推荐答案

您可以创建自定义滚动型的ListView 和覆盖

You can create a custom ScrollView and ListView and override

onInterceptTouchEvent(MotionEvent event)

是这样的:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    View view = (View) getChildAt(getChildCount()-1);
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    if( event.getAction() == MotionEvent.ACTION_DOWN) {
        if (diff ==0) {
            return false;
        }
    }

    return super.onInterceptTouchEvent(event);
}

这样的的ListView每个向下触摸,而你在滚动型的底部会去的ListView

This way every Down Touch on the ListView while you are at the bottom of ScrollView will go to the ListView.

这仅仅是一个草图实现,但我认为你可以从这个开始做同样的事情,当你在的ListView顶部检测

This is just a sketch implementation but I think you could get started from this by doing the same thing to detect when you are at the top of the ListView

作为一个说明我会尝试更简单的实现仅仅通过使用的ListView 与您当前的含量滚动型通过使用 listView.addHeaderView(scrollViewContent)加入的ListView的头。我不知道这是否符合您的需求。

As a note I would try an easier implementation by using just a ListView with the current content of you ScrollView added as the header of ListView by using listView.addHeaderView(scrollViewContent). I don't know if this fits your needs.

编辑:

检测时应该开始滚动的滚动型。保持的ListView 滚动型的参考。当用户滚动起来了的的ListView 是在顶部,让该事件由滚动型。<被消耗/ P>

Detecting when should start scrolling the ScrollView. Keeping a reference of ListView in the ScrollView. When the user is scrolling up and the the ListView is at the top let the event be consumed by the ScrollView.

private void init(){
    ViewConfiguration vc = ViewConfiguration.get(getContext());
    mTouchSlop = vc.getScaledTouchSlop();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    final int action = MotionEventCompat.getActionMasked(event);

    switch (action) {
        case MotionEvent.ACTION_DOWN:{
            yPrec = event.getY();
        }
        case MotionEvent.ACTION_MOVE: {
            final float dy = event.getY() - yPrec;

            if (dy > mTouchSlop) {
                // Start scrolling!
                mIsScrolling = true;
            }
            break;
        }
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mIsScrolling = false;
    }

    // Calculate the scrolldiff
    View view = (View) getChildAt(getChildCount()-1);
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

        if ((!mIsScrolling || !listViewReference.listIsAtTop()) && diff == 0) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                return false;
            }
    }

    return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mIsScrolling = false;
    }
    return super.onTouchEvent(ev);
}


public void setListViewReference(MyListView listViewReference) {
    this.listViewReference = listViewReference;
}

在ListView控件的方法listIsAtTop看起来是这样的:

The method listIsAtTop in ListView looks like this :

public boolean listIsAtTop()   {
    if(getChildCount() == 0) return true;
    return (getChildAt(0).getTop() == 0 && getFirstVisiblePosition() ==0);
}

这篇关于排便事件于母公司滚动型列表视图时,在顶部/底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆