listview的定位

listview的定位问题

一:概述

       如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题:

       解决办法:

         android中ListView的定位:使用setSelectionFromTop

         在android的开发中,经常会遇到需要主动去设定某条ListItem的位置的需求。设置位置的函数有

                    ListView.setSelection(int position)

                    ListView.setSelectionFromTop(int position, int y);

      其中position指的是指定的item的在ListView中的索引,注意如果有Header存在的情况下,索引是从Header就开始算的。

      y指的是到ListView可见范围内最上边边缘的距离。

 

 二: setSelectionFromTop与setSelection的区别

     解决的办法如下:

    // 保存当前第一个可见的item的索引和偏移量
     int index = mList.getFirstVisiblePosition();
    View v = mList.getChildAt(0);
    int top = (v == null) ? 0 : v.getTop();
    // ...
    //根据上次保存的index和偏移量恢复上次的位置
     mList.setSelectionFromTop(index, top);

      这里使用了setSelectionFromTop来定位ListView。其实还可以使用setSelection也可以定位,只是setSelectionFromTop要比setSelection更精准。因为通过getFirstVisiblePosition得到的第一个item可能已经有一部分是不可见的了,如果用setSelection无法反映出这不可见的部分。

三:源码分析

      setSelectionFromTop源码

/**
  * Sets the selected item and positions the selection y pixels from the top edge
  * of the ListView. (If in touch mode, the item will not be selected but it will
  * still be positioned appropriately.)
  *
  * @param position Index (starting at 0) of the data item to be selected.
  * @param y The distance from the top edge of the ListView (plus padding) that the
  *        item will be positioned.
  */
 public void setSelectionFromTop(int position, int y) {
     if (mAdapter == null) {
         return;
     }
     if (!isInTouchMode()) {
         position = lookForSelectablePosition(position, true);
         if (position >= 0) {
             setNextSelectedPositionInt(position);
         }
     } else {
         mResurrectToPosition = position;
     }
     if (position >= 0) {
         mLayoutMode = LAYOUT_SPECIFIC;
         mSpecificTop = mListPadding.top + y;
         if (mNeedSync) {
             mSyncPosition = position;
             mSyncRowId = mAdapter.getItemId(position);
         }
         requestLayout();
     }
 }

从上面的代码可以得知,setSelectionFromTop()的作用是设置ListView选中的位置,同时在Y轴设置一个偏移量

 

setSelection()的具体实现,代码如下:

/**
  * Sets the currently selected item. If in touch mode, the item will not be selected
  * but it will still be positioned appropriately. If the specified selection position
  * is less than 0, then the item at position 0 will be selected.
  *
  * @param position Index (starting at 0) of the data item to be selected.
  */
 @Override
 public void setSelection(int position) {
     setSelectionFromTop(position, 0);
 }

原来,setSelection()内部就是调用了setSelectionFromTop(),只不过是Y轴的偏移量是0而已。现在应该对setSelection()和setSelectionFromTop()有了更深刻的认识了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值