如何滚动到长 ScrollView 布局的顶部? [英] How to scroll to top of long ScrollView layout?

查看:19
本文介绍了如何滚动到长 ScrollView 布局的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序的一部分,用户会看到一个名字列表,并被要求按照他们认为合适的方式对它们进行分组.

For part of my app, the user is presented with a list of names and is asked to group them as they see fit.

(注意,ListView 代码是从 Android 视图教程中逐字复制而来的.我还没有根据我的需要对其进行自定义,我只是想弄清楚如何使其工作.)

(Note, the ListView code was copied verbatim from the Android Views tutorial. I haven't yet customized it to my needs, I'm just trying to figure out how to make this work.)

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

The basic layout is a LinearLayout, containing a ScrollView (called "groupsScrollView" in the code below), containing a RelativeLayout. I have some buttons and text, and then my ListView beneath that, which displays the list of names. All this is taller than the visible screen area, so the user is allowed to scroll vertically to see it all.

这一切都很好,除非页面加载时它总是预先滚动到我的 ListView 的顶部 - 在页面的中间.我创建的用于告诉用户该做什么的文本和按钮是不可见的.

This all works beautifully, except when the page loads it is always pre-scrolled to the top of my ListView - in the middle of the page. The text and buttons I've created that tell the user what to do are not visible.

我可以抓取屏幕并向上滚动,这很好用,但我希望屏幕在已经滚动到顶部后加载.用户不必向上滚动才能看到新加载页面的顶部.但是我尝试以编程方式滚动到屏幕顶部的一切都失败了.

I can grab the screen and scroll up, that works just fine, but I'd like the screen to load having already been scrolled to the top. The user shouldn't have to scroll UP to see the top of a freshly loaded page. But everything I've tried to programmatically scroll to the top of the screen has failed.

这是我的 onCreate 方法:

Here's my onCreate method:

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();
        }
      });
}

mainScrollView.pageScroll(View.FOCUS_UP); 是问题所在.它不会导致错误,但它似乎没有做任何事情.我试过 scrollTo(0,0), scrollDirection(View.FOCUS_UP),以及我能想到的所有其他内容.

The line "mainScrollView.pageScroll(View.FOCUS_UP); is the problem. It doesn't cause an error, but it doesn't seem to do anything. I've tried scrollTo(0,0), scrollDirection(View.FOCUS_UP), and everything else I can think of.

因为我没有收到错误,所以我必须假设这些滚动命令确实有效,但是它们滚动到 ListView 的顶部,而不是包含它的 ScrollView 或 RelativeLayout 的顶部.谷歌自己对 scrollTo(int x, int y) 方法的描述似乎证实了这一点,他们说这个版本也将滚动限制在我们孩子的范围内.".

Since I don't get an error I have to assume that these scroll commands are actually working, but that they're scrolling to the top of the ListView, rather than the top of the ScrollView or RelativeLayout that contains it. This seems to be confirmed by Google's own description of the scrollTo(int x, int y) method where they say "This version also clamps the scrolling to the bounds of our child.".

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

So to make a long question even longer, how do I load a bunch of views on the screen contained within a ScrollView, and then programmatically scroll the whole thing to the top of the page so the user can interact with it?

谢谢!

推荐答案

尝试

mainScrollView.fullScroll(ScrollView.FOCUS_UP);

它应该可以工作.

这篇关于如何滚动到长 ScrollView 布局的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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