Horizo​​ntalScrollView:自动滚动结束时都增加了新的看法? [英] HorizontalScrollView: auto-scroll to end when new Views are added?

查看:169
本文介绍了Horizo​​ntalScrollView:自动滚动结束时都增加了新的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含的LinearLayout一个Horizo​​ntalScrollView。在屏幕上我有一个按钮,将增加新的浏览到的LinearLayout在运行时,我想要滚动视图滚动到当一个新的视图中添加的列表的末尾。

我几乎都有它的工作 - 不过它总是滚动的最后一个视图中的一个视图短。这似乎是它的滚动没有先计算列入新的观点。

在我的应用程序,我使用的是定制的视图对象,但我做了一个使用ImageView的,并且具有相同症状的一个小的测试应用程序。我试着像requestLayout()的布局和滚动型各种事情,我想scrollTo(Integer.MAX_VALUE的),并滚动到netherverse :)我是不是违反了UI线程的问题还是什么?

  • 里克

==

 公共类主要扩展活动{
        / **第一次创建活动时调用。 * /
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main);

             按钮B =(按钮)findViewById(R.id.addButton);
             b.setOnClickListener(新的addListener());

             加();
        }

        私人无效的add(){
             的LinearLayout L =(的LinearLayout)findViewById(R.id.list);
             Horizo​​ntalScrollView S =
                 (Horizo​​ntalScrollView)findViewById(R.id.scroller);

             ImageView的我=新ImageView的(getApplicationContext());
             i.setImageResource(android.R.drawable.btn_star_big_on);
             l.addView(ⅰ);

             s.fullScroll(Horizo​​ntalScrollView.FOCUS_RIGHT);
        }

        私有类的addListener实现View.OnClickListener {
             @覆盖
             公共无效的onClick(视图v){
                 加();
             }
        }
    }
 

布局XML:

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>

        < Horizo​​ntalScrollView
            机器人:ID =@ + ID /滚轮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:scrollbarSize =50像素>
            <的LinearLayout
                机器人:ID =@ + ID /列表
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:填充=递四方/>
        < / Horizo​​ntalScrollView>

        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_gravity =中心>
            <按钮
                机器人:ID =@ + ID / Add按钮
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =中心
                机器人:以下属性来=80px
                机器人:paddingRight =80px
                机器人:paddingTop =40PX
                机器人:paddingBottom会=40PX
                机器人:文本=添加/>
        < / LinearLayout中>

    < / LinearLayout中>
 

解决方案

我觉得有一个时机的问题。布局不这样做,当一个视图被添加。它要求并做了很短的时间后。当您添加视图后,立即拨打fullScroll的的LinearLayout的宽度一直没有发展的机会。

尝试更换:

  s.fullScroll(Horizo​​ntalScrollView.FOCUS_RIGHT);
 

  s.postDelayed(新的Runnable(){
    公共无效的run(){
        s.fullScroll(Horizo​​ntalScrollView.FOCUS_RIGHT);
    }
},100L);
 

在短暂的延迟应该给予系统足够的时间来解决。

I have a HorizontalScrollView containing a LinearLayout. On screen I have a Button that will add new Views to the LinearLayout at runtime, and I'd like the scroll view to scroll to the end of the list when a new View is added.

I almost have it working - except that it always scrolls one view short of the last view. It seems like it's scrolling without first calculating the inclusion of the new view.

In my app I am using a custom View object, but I made a small test application that uses ImageView and has the same symptom. I tried various things like requestLayout() on both the Layout and ScrollView, I tried scrollTo(Integer.MAX_VALUE) and it scrolled into the netherverse :) Am I violating a UI thread issue or something?

  • Rick

======

    public class Main extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

             Button b = (Button) findViewById(R.id.addButton);
             b.setOnClickListener(new AddListener());

             add();
        }

        private void add() {
             LinearLayout l = (LinearLayout) findViewById(R.id.list);
             HorizontalScrollView s = 
                 (HorizontalScrollView) findViewById(R.id.scroller);

             ImageView i = new ImageView(getApplicationContext());
             i.setImageResource(android.R.drawable.btn_star_big_on);
             l.addView(i);

             s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
        }

        private class AddListener implements View.OnClickListener {
             @Override
             public void onClick(View v) {
                 add();
             }
        }
    }

Layout XML:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <HorizontalScrollView
            android:id="@+id/scroller"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbarSize="50px">
            <LinearLayout
                android:id="@+id/list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="4px"/>
        </HorizontalScrollView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"            
            android:layout_gravity="center">
            <Button
                android:id="@+id/addButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="80px"
                android:paddingRight="80px"
                android:paddingTop="40px"
                android:paddingBottom="40px"
                android:text="Add"/>
        </LinearLayout>

    </LinearLayout>

解决方案

I think there's a timing issue. Layout isn't done when a view is added. It is requested and done a short time later. When you call fullScroll immediately after adding the view, the width of the linearlayout hasn't had a chance to expand.

Try replacing:

s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);

with:

s.postDelayed(new Runnable() {
    public void run() {
        s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
    }
}, 100L);

The short delay should give the system enough time to settle.

这篇关于Horizo​​ntalScrollView:自动滚动结束时都增加了新的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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