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

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

问题描述

有关我的应用程序的一部分,用户psented与名称列表$ P $,并要求组他们,因为他们认为合适的。

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 code的逐字从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中,包含滚动型(在code以下称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.

这一切精美的作品,除了页面加载时它总是pre-滚动到我的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的顶部,而不是滚动型的顶部或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.".

因此​​,为了使一个长期的问题,甚至更长的时间,我怎么加载了一堆包含一个滚动型中在屏幕上的意见,然后以编程方式滚动整个事情到页面的顶部,这样用户可以与它进行交互?

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

它应该工作。

it should work.

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

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