我有一个 LinearLayout 这包含其他一些意见和其中一个 ListView.这种观点是从另外一个点击一下按钮。

这个按钮以某种方式指定什么样的元件中的列表视图需要的可见第一一个在名单。元素填充名单检索通过HTTP从外部服务器。

问题是,我可以得到的N元是第一个在名单。请注意,我不想要移动的形式,它目前的位置到一个新的,我要的名单滚动。

我已经尝试与 setSelected()scrollTo(x,y)scrollBy(x,y) 但是没有运气。

我还给了一个尝试这个另外我们的代码,作为丑陋的,但是我只是想试试f它的工作:

ListView categoryList = (ListView)findViewById(R.id.category_list);
        categoryList.post(new Runnable() {
            @Override
            public void run() {
                Log.d(this.getClass().getName(), "CategoryActivity.scrollToIndex: " + CategoryActivity.scrollToIndex);
                if(CategoryActivity.scrollToIndex>0){
                    ListView categoryList = (ListView)findViewById(R.id.category_list);
                    categoryList.setScrollContainer(true);
                    categoryList.scrollTo(4, CategoryActivity.scrollToIndex * 50);
                    categoryList.requestLayout();

                }
            }
        });

这给了我一些成功,但列表视图是,然后表现 疯狂的 在一个我甚至不能描述的。...

任何想法?

有帮助吗?

解决方案

试着将它添加到消息队列

categoryList.post(new Runnable() {
    public void run() {
        categoryList.scrollTo(4, CategoryActivity.scrollToIndex * 50);
    }
});

它的工作对我来说在一个滚动型(检查这个答案).

其他提示

我提出的功能,可能是有用的他人列表视图滚动,它们对我来说在每个机器人版本工作,仿真器和设备,在这里itemheight是视在列表视图的固定高度。

int itemheight=60;
public void scrollToY(int position)
{
    int item=(int)Math.floor(position/itemheight);
    int scroll=(int) ((item*itemheight)-position);
    this.setSelectionFromTop(item, scroll);// Important
}
public void scrollByY(int position)
{
    position+=getListScrollY();
    int item=(int)Math.floor(position/itemheight);
    int scroll=(int) ((item*itemheight)-position);
    this.setSelectionFromTop(item, scroll);// Important
}
public int getListScrollY()
{
    try{
    //int tempscroll=this.getFirstVisiblePosition()*itemheight;// Important
    View v=this.getChildAt(0);
    int tempscroll=(this.getFirstVisiblePosition()*itemheight)-v.getTop();// Important
    return tempscroll;
    }catch(Exception e){}
    return 0;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top