Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 滚动视图返回顶部_Android_Android Edittext_Scrollview_Touch Event_Motionevent - Fatal编程技术网

Android 滚动视图返回顶部

Android 滚动视图返回顶部,android,android-edittext,scrollview,touch-event,motionevent,Android,Android Edittext,Scrollview,Touch Event,Motionevent,所以我有一个只有一个视图(EditText)的scrollView,我想实现 dispatchTouchEvent(MotionEvent me) View current_view = Processor.findViewGroupWithPoint( (int) me.getRawX(), (int) me.getRawY(), rmodel.layout.parent); if (current_view instanceof ScrollView) {

所以我有一个只有一个视图(EditText)的scrollView,我想实现

dispatchTouchEvent(MotionEvent me)
 View current_view = Processor.findViewGroupWithPoint(
            (int) me.getRawX(), (int) me.getRawY(), rmodel.layout.parent);
    if (current_view instanceof ScrollView) {


        switch( me.getAction() ){
        case MotionEvent.ACTION_DOWN:
            Log.d(TAG, " down");

            ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_DOWN);

            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, " up");
((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
            break;
        }
}


我的问题是,当我向下滚动时,它可以工作,但我必须将手指放在屏幕上,就好像我将它移走一样,它会回到滚动视图的顶部。

据我所知,当你放开它时,它会转到滚动视图的顶部,因为是你告诉它的。你说

case MotionEvent.ACTION_UP:
    Log.d(TAG, " up");
    ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
MotionEvent.ACTION\u UP
用于检测手指何时松开,因此当手指松开时,您会告诉它使用

((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
要解决这个问题,您真正需要做的就是删除该行

编辑:

我写了这个,它应该会工作,可能需要一些调整

boolean bottom = false;
基本上,这里只需创建一个名为bottom的布尔值,并将其设置为false^^

case MotionEvent.ACTION_DOWN:
    Log.d(TAG, " down");

    if (bottom = false){
        ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_DOWN);
    }else{
        ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
    }
    break;
}

这只是看看滚动视图是在底部还是现在。。。希望它能起作用

当我实现case MotionEvent.ACTION_DOWN:Log.d(标记“DOWN”);((ScrollView)findViewById(111111)).fullScroll(View.FOCUS_DOWN);独自一人,它会把我带到底部,永远不会后退,但我也希望能够站起来,当我向上移动时,是否有其他地方可以同时做这两件事?你可以使用if语句来检测它是在底部还是现在。我将尝试添加一个示例,但看看您是否能够自己解决它!回答得好。请注意,@a.AL,您不应该每次要引用该项目时都调用
findViewById()
。我建议创建一个类级变量,类似于
private ScrollView mMyScrollView
,在onCreate中可以调用
mMyScrollView=(ScrollView)findViewById(111111)在代码的其余部分,您可以像
mMyScrollView.fullScroll(View.FOCUS\u DOWN)
一样引用它。这不仅会使您的代码更具可读性,而且还可以通过不多次挖掘视图层次结构来提高性能。@McAdam331,谢谢,我在编写代码时只是在测试一些东西,但谢谢,我从来不知道调用findViewById()会影响性能,再次感谢