Android的:如何判断一个观点是滚动 [英] Android: how to tell if a view is scrolling

查看:85
本文介绍了Android的:如何判断一个观点是滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone,这将是很简单 - 每个视图都有scrollViewDidScroll方法。我试图找到等值的Andr​​oid系统。

我的code工作,但它不给我我想要的。我需要执行code,一个观点是滚动的整个持续时间。所以,尽管我用OnGestureListener的onScroll方法,它只火灾时,手指在屏幕上(这不是真的正确命名的---它应该被称为onSlide或onSwipe,专注于手势,而不是UI动画)。它不会继续,如果用户笔触火的观点,它仍然是滚动片刻用户抬起手指后。

有一个名为在滚动的每一步?

方法

 公共类涡旋盘活动实现OnGestureListener {

公众的WebView的WebView;

公共GestureDetector gestureScanner;

公众诠释currentYPosition;
公众诠释lastYPosition;

公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);

    的WebView =新的WebView(本);
    的setContentView(web视图);
    webview.loadUrl(文件:///android_asset/Scroll.html);

    gestureScanner =新GestureDetector(本);

    currentYPosition = 0;
    lastYPosition = 0;

}


公共布尔的onTouchEvent(最终MotionEvent我){
    返回gestureScanner.onTouchEvent(箱);
}


公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,浮distanceY){
    //我做的东西在这里。
    返回true;
}
 

解决方案

我打得周围用的WebView一点点,并没有找到一个方法来跟踪滚动,但你绝对需要它是一个web视图?如果你正在使用一个ListView或GridView控件,您可以实现ListView.OnScrollListener方法,尤其是 onScrollStateChanged onScroll 。前者是特别有用,因为它给你更新什么列表是干什么的,即使您的特定用户'扔'名单的例子,它继续事后滚动一段时间。你将如何跟踪一个例子:

 公共类List13扩展ListActivity实现ListView.OnScrollListener {

(...)

    公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){
        开关(scrollState){
        案例OnScrollListener.SCROLL_STATE_IDLE:
            //名单闲置
            打破;
        案例OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            //名单是滚动状态下直接接触用户的
            打破;
        案例OnScrollListener.SCROLL_STATE_FLING:
            //用户做了列表中的一扔',它仍然滚动
            打破;
        }
    }
}
 

我举起了直出code样品谷歌提供的,例如List13在阿比演示项目。如果你想有进一步了解的是,将其导入到Eclipse中通过转到文件 - >新建 - > Android项目,从现有源创建项目,然后导航到(您的SDK文件夹)/平台/(版本你正与)/样本/ ApiDemos。如果你不能找到它,你可能没有,当你设置你的SDK下载示例应用程序。你可以这样做,在Eclipse中通过转到窗口 - > Android SDK和AVD管理器,然后在左侧的可用的软件包

in iPhone, this would be simple---each view has a scrollViewDidScroll method. I am trying to find the equivalent in Android.

My code works, but it isn't giving me what I want. I need to execute code the entire duration that a view is scrolling. So, even though I use OnGestureListener's onScroll method, it only fires when the finger is on the screen (it's not really named correctly---it should be called "onSlide" or "onSwipe", focusing on the gesture rather than the UI animation). It does not continue to fire if the user flicks the view and it is still scrolling for a few moments after the user lifts his finger.

is there a method that is called at every step of the scroll?

public class Scroll extends Activity implements OnGestureListener {

public  WebView         webview;

public  GestureDetector gestureScanner;

public  int         currentYPosition;
public  int         lastYPosition;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("file:///android_asset/Scroll.html");

    gestureScanner = new GestureDetector(this);

    currentYPosition = 0;
    lastYPosition = 0;

}


public boolean onTouchEvent(final MotionEvent me) {
    return gestureScanner.onTouchEvent(me);
}


public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    // I do stuff here.
    return true;
}

解决方案

I've played around a little bit with a WebView and didn't find a way to track the scrolling, but do you definitely need it to be a WebView? If you're using a ListView or a GridView, you can implement the ListView.OnScrollListener methods, notably onScrollStateChanged and onScroll. The former is particularly useful as it gives you updates on what the list is doing, even for your specific example of the user 'flinging' the list and it continues to scroll for some time afterwards. An example of how you would track that:

public class List13 extends ListActivity implements ListView.OnScrollListener {

(...)

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
            //List is idle
            break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            //List is scrolling under the direct touch of the user
            break;
        case OnScrollListener.SCROLL_STATE_FLING:
            //The user did a 'fling' on the list and it's still scrolling
            break;
        }
    }
}

I lifted that straight out of the code samples Google provides, example List13 in the "Api Demos" project. If you want to have a closer look at that, import it into Eclipse by going to File -> New -> Android Project, Create Project from Existing Source, and navigate to (your SDK folder)/platforms/(the version you're working with)/samples/ApiDemos. If you can't find it, you might not have downloaded the sample apps when you set up your SDK. You can do that in Eclipse by going to Window -> Android SDK and AVD Manager, then under "Available Packages" on the left.

这篇关于Android的:如何判断一个观点是滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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