前段时间在项目中用到了HorizontalListView的一些东东,后来在项目中数据刷新的时候需要回到第一项,想使用setSelection方法,却发现这个开源的代码没有实现这个方法。

1 @Override
2 public void setSelection(int position) {
3       
4 }

该如何解决呢?

后来通过查看源代码,发现数据刷新后,程序回到记录的上一个位置。

protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (mAdapter == null) {
            return;
        }

        if (mDataChanged) {
                    ...................................................
                }


.........................................

}

 

查看数据更新的源码:

if (mDataChanged) {
            // comment by lixin for reset x location(2013-03-21)
            // int oldCurrentX = mCurrentX;
            // modify by lixin 2012-04-02 因为有需求不改变位置
            int oldCurrentX = 0;
            if (!needResetLocate) {
                oldCurrentX = mCurrentX;
            }

            initView();
            removeAllViewsInLayout();
            // comment by lixin for reset x location(2013-03-21)
            // mNextX = oldCurrentX;
            // modify by lixin 2012-04-02 因为有需求不改变位置
            if (!needResetLocate) {
                mNextX = oldCurrentX;
            }
            mDataChanged = false;
        }

在代码中加入一个标志位用来对位置进行记录分支。

相关文章:

  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2021-07-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2021-12-26
  • 2021-08-16
  • 2021-12-15
  • 2021-09-08
相关资源
相似解决方案