Android 如何滚动到长滚动视图布局的顶部?

Android 如何滚动到长滚动视图布局的顶部?,android,listview,scrollview,Android,Listview,Scrollview,在我的应用程序中,会向用户显示一个姓名列表,并要求用户根据自己的喜好对其进行分组 (注意,ListView代码是从Android视图教程中一字不差地复制而来的。我还没有根据自己的需要定制它,我只是想弄清楚如何实现这一点。) 基本布局是一个LinearLayout,包含一个ScrollView(在下面的代码中称为“groupsScrollView”),其中包含一个RelativeLayout。我有一些按钮和文本,然后在下面是我的列表视图,它显示名称列表。所有这些都比可见屏幕区域高,因此允许用户垂直

在我的应用程序中,会向用户显示一个姓名列表,并要求用户根据自己的喜好对其进行分组

(注意,ListView代码是从Android视图教程中一字不差地复制而来的。我还没有根据自己的需要定制它,我只是想弄清楚如何实现这一点。)

基本布局是一个LinearLayout,包含一个ScrollView(在下面的代码中称为“groupsScrollView”),其中包含一个RelativeLayout。我有一些按钮和文本,然后在下面是我的列表视图,它显示名称列表。所有这些都比可见屏幕区域高,因此允许用户垂直滚动以查看所有内容

<>这一切都很好,除了页面加载时,它总是滚动到我的ListVIEW的顶部——在页面的中间。我创建的告诉用户要做什么的文本和按钮不可见

我可以抓取屏幕并向上滚动,这很好,但我希望屏幕已经滚动到顶部,可以加载。用户不必向上滚动才能看到新加载页面的顶部。但我尝试通过编程方式滚动到屏幕顶部的所有操作都失败了

以下是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.groups);

    mainScrollView = (ScrollView)findViewById(R.id.groupsScrollView);

    //get the Bundle out of the Intent...
    Bundle extras = getIntent().getExtras();
    mNames = extras.getStringArray("mNames");

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mNames));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    //This is the line I'm having issues with
    mainScrollView.pageScroll(View.FOCUS_UP);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_SHORT).show();
        }
      });
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.groups);
mainScrollView=(ScrollView)findViewById(R.id.groupsScrollView);
//将包从意图中取出。。。
Bundle extras=getIntent().getExtras();
mNames=extras.getStringArray(“mNames”);
setListAdapter(新的ArrayAdapter(this,R.layout.list_项,mNames));
ListView lv=getListView();
lv.setTextFilterEnabled(真);
//这是我遇到的问题
mainScrollView.pageScroll(View.FOCUS\u UP);
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//单击后,显示带有文本视图文本的祝酒词
Toast.makeText(getApplicationContext(),((TextView)视图).getText(),
吐司。长度(短)。show();
}
});
}
行“mainScrollView.pageScroll(View.FOCUS\u UP)”;是问题所在。它不会导致错误,但似乎没有任何作用。我尝试了scrollTo(0,0)、scrollDirection(View.FOCUS\u UP)以及我能想到的所有其他方法

由于我没有收到错误,我不得不假设这些滚动命令实际上正在工作,但它们正在滚动到ListView的顶部,而不是滚动视图或包含它的RelativeLayout的顶部。谷歌自己对scrollTo(intx,inty)方法的描述似乎证实了这一点,其中他们说“这个版本也将滚动限制在我们孩子的范围内。”

因此,为了让一个长问题变得更长,我如何在ScrollView中包含的屏幕上加载一组视图,然后以编程方式将整个内容滚动到页面顶部,以便用户可以与之交互

谢谢!

试试看

mainScrollView.fullScroll(ScrollView.FOCUS_UP);

它应该可以工作。

也有同样的问题,可能是某种bug

甚至来自另一个答案的
fullScroll(ScrollView.FOCUS\u UP)
也不起作用。

对我来说唯一有效的方法是在对话框显示后立即调用
scroll\u view.smoothScrollTo(0,0)

当我使用Scrollview内部视图翻转器或对话框时,遇到了同样的问题,即case
scrollViewObject.fullScroll(Scrollview.FOCUS\u UP)
返回false,因此case
scrollViewObject.smoothScrollTo(0,0)
为我工作

scroll_view.smoothScrollTo(0,0); // scroll to top of screen

scrollViewObject.fullScroll(ScrollView.FOCUS\u UP)
这行可以正常工作,但这行的唯一问题是,在scrollViewObject中填充数据时,已立即调用。您必须等待几毫秒,直到填充数据。请尝试以下代码:

scrollViewObject.postDelayed(new Runnable() {
    @Override
    public void run() {
        scroll.fullScroll(ScrollView.FOCUS_UP);
    }
}, 600);


所有这些有效解决方案的主要问题是,当屏幕上尚未绘制滚动条时,您正试图向上移动滚动条

您需要等待滚动视图出现在屏幕上,然后使用这些解决方案中的任何一种将其上移。这是一种比延时更好的解决方案,因为您不介意延时

    // Wait until my scrollView is ready
    scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Ready, move up
            scrollView.fullScroll(View.FOCUS_UP);
        }
    });

我也有同样的问题,这个解决了。希望它能帮助你

listView.setFocusable(false);
很容易
@SuppressWarnings({“弃用”、“未选中”})
公共无效swipeTopToBottom(AppiumDriver驱动程序)
抛出中断异常{
维度=driver.manage().window().getSize();
双屏幕高度开始=尺寸。getHeight()*0.30;
int scrollStart=screenHeightStart.intValue();
System.out.println(“s=“+scrollStart”);
双屏幕加高=尺寸。getHeight()*0.90;
int scrollEnd=screenEnhanced.intValue();
驱动程序。滑动(0,滚动开始,0,滚动结束,2000);
线程等待(驱动程序,3000);
}

这是用于滚动视图的强制滚动:

            scrollView.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.scrollTo(0, 0);
                    scrollView.pageScroll(View.FOCUS_UP);
                    scrollView.smoothScrollTo(0,0);
                }
            });
这是为我工作

scroll_view.smoothScrollTo(0,0); // scroll to top of screen

我在Kotlin解决了我的问题,如下所示:

scrollview.isFocusableInTouchMode = true
scrollview.fullScroll(View.FOCUS_UP)
scrollview.smoothScrollTo(0,0)
您还可以创建类似以下内容的扩展:

ScrollView.moveToTop()
{
    this.isFocusableInTouchMode = true
    this.fullScroll(View.FOCUS_UP)
    this.smoothScrollTo(0,0)
}
然后像这样使用它:

scrollView.moveToTop()

此解决方案也适用于NestedScrollView

NestedScrollView nestedScrollView = view.findViewById(R.id.YourNestedScrollViewID);
nestedScrollView.fullScroll(ScrollView.FOCUS_UP);

最初这是可行的,但后来当我一次又一次尝试这样做时,这种方法并没有给出令人满意的结果。我从scroll_view中解决。smoothScrollTo(0,0),当我们在scrollview中有listview时,fullScroll(scrollview.FOCUS_UP)将不起作用。scroll_view.smoothScrollTo(0,0)对我来说比mainScrollView更好。fullScroll(ScrollView.FOCUS\u UP);谢谢!在大多数设备上fullScroll(ScrollView.FOCUS\u UP)都能工作,但在Nexus 5和我将文本大小调大的设备上,fullScroll移动到操作栏下,smoothScrollTo(0,0)在所有设备上都能更好地工作。与
fullScroll()
相比,这还有一个优点,即在移动后,所选项目仍处于选中状态。使用
fullScroll()scroll_view.smoothScrollTo(0,0); // scroll to top of screen
scrollview.isFocusableInTouchMode = true
scrollview.fullScroll(View.FOCUS_UP)
scrollview.smoothScrollTo(0,0)
ScrollView.moveToTop()
{
    this.isFocusableInTouchMode = true
    this.fullScroll(View.FOCUS_UP)
    this.smoothScrollTo(0,0)
}
scrollView.moveToTop()
NestedScrollView nestedScrollView = view.findViewById(R.id.YourNestedScrollViewID);
nestedScrollView.fullScroll(ScrollView.FOCUS_UP);