【问题标题】:how to maintain scroll bar position in android listview?如何在 android listview 中保持滚动条位置?
【发布时间】:2011-01-10 09:54:57
【问题描述】:

朋友们,

我在 ListView 中显示一个数组。

我在按钮单击和调用时动态更改数组数据

adapter.notifyDataSetInvalidated();

它不保持列表的滚动条位置。 (要列出的数据源的长度始终相同)。 谁能指导我如何保持 ListView 的最后状态?

任何帮助将不胜感激。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    试试这个:

    //Get the top position from the first visible element
    int idx = list.getFirstVisiblePosition();
    View vfirst = list.getChildAt(0);
    int pos = 0;
    if (vfirst != null) pos = vfirst.getTop();
    
    //Restore the position
    list.setSelectionFromTop(idx, pos);
    

    【讨论】:

    • 请更正代码,否则这里的 v 是什么难以理解?是不是像 if(vfirst != null)
    • 代码中的小错误:v.getTop() 应该是 vfirst.getTop()
    【解决方案2】:

    如果只想刷新数据的一些变化,使用

    notifyDataSetChanged();
    

    相反。并且滚动将在相同的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 2011-03-02
      相关资源
      最近更新 更多