ListView中item定位

来源:互联网 发布:网络大电影 罪 上映 编辑:程序博客网 时间:2024/05/20 13:17

ListView 实现定位特定 item

最近在项目中需要使 ListView 能跳转到特定的 item,查阅文档后,发现 ListView 有以下几种方法可供使用:

  • smoothScrollToPosition(int position):平滑滚动到指定位置。如果 position 为负,则滚动到第一条;如果 position 大于 ListView item 的最大值,则滚动到最后一条。
  • smoothScrollByOffset(int offset):平滑滚动偏移量 offset 指定的 item。如果 offset 大于 0,则表示下移;如果 offset 小于 0,则表示上移。当 offset 超过可以移动的最大值时,默认移动到顶部或尾部。
  • setSelection(int position):直接跳转到指定的位置,没有任何滚动效果。
  • setSelectionFromTop(int position,int offset):直接跳转到指定的位置,并在Y轴设置一个偏移量。该偏移量是个 padding 值,可以用来实现类似 QQ 查看历史消息的功能。可参考ListView的setSelection()和setSelectionFromTop()联系

:通过查看源代码,其实可以发现 setSelection(int position) 其实调用的是 setSelectionFromTop(int position,int offset) 方法,只不过 offset = 0 而已。

源码:

/*** 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.*/@Overridepublic void setSelection(int position) {   setSelectionFromTop(position, 0);}    /*** 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);       }       if (mPositionScroller != null) {           mPositionScroller.stop();       }       requestLayout();   }} 

简单小Demo

MainActivity.java:

public class MainActivity extends Activity implements View.OnClickListener {    private EditText mEdt;    private Button mBtnJump, mBtnMoveUp, mBtnMoveDown;    private Button mBtnSelect;    private Button mBtnSelectPositive,mBtnSelectNagetive;    private ListView mLv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mEdt = (EditText) findViewById(R.id.edt);        mBtnJump = (Button) findViewById(R.id.btn_jump);        mBtnMoveUp = (Button) findViewById(R.id.btn_move_up);        mBtnMoveDown = (Button) findViewById(R.id.btn_move_down);        mBtnSelect = (Button) findViewById(R.id.btn_select);        mBtnSelectPositive = (Button) findViewById(R.id.btn_select_from_top_positive_offset);        mBtnSelectNagetive = (Button) findViewById(R.id.btn_select_from_top_nagetive_offset);        mLv = (ListView) findViewById(R.id.lv);        mLv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, generateString()));        mBtnJump.setOnClickListener(this);        mBtnMoveUp.setOnClickListener(this);        mBtnMoveDown.setOnClickListener(this);        mBtnSelect.setOnClickListener(this);        mBtnSelectPositive.setOnClickListener(this);        mBtnSelectNagetive.setOnClickListener(this);    }    private List<String> generateString() {        List<String> list = new ArrayList<>();        for (int i = 0; i < 10; i++) {            list.add("item " + i);        }        return list;    }    @Override    public void onClick(View v) {        String input = mEdt.getText().toString();        int position = Integer.valueOf(input);        switch (v.getId()) {            case R.id.btn_jump:                //平滑滚动到指定位置                mLv.smoothScrollToPosition(position);                break;            case R.id.btn_move_up:                //平滑上移指定数量的item                mLv.smoothScrollByOffset(-position);                break;            case R.id.btn_move_down:                //平滑下移指定数量的item                mLv.smoothScrollByOffset(position);                break;            case R.id.btn_select:                //跳转到指定的位置,不平移                mLv.setSelection(position);                break;            case R.id.btn_select_from_top_positive_offset:                //跳转到指定的位置,第二个参数表示Y轴偏移量                mLv.setSelectionFromTop(position, 100);                break;            case R.id.btn_select_from_top_nagetive_offset:                //跳转到指定的位置,第二个参数表示Y轴偏移量                mLv.setSelectionFromTop(position, -100);                break;        }    }}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.littlejie.listview.MainActivity">    <EditText        android:id="@+id/edt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入你要跳转的行数(0~9999)"        android:inputType="number" />    <Button        android:id="@+id/btn_jump"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点我跳转(smoothScrollToPosition)" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_move_up"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="上移指定偏移量" />        <Button            android:id="@+id/btn_move_down"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="下移指定偏移量" />    </LinearLayout>    <Button        android:id="@+id/btn_select"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="直接跳转到position" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_select_from_top_positive_offset"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="setSelectionFromTop(+)" />        <Button            android:id="@+id/btn_select_from_top_nagetive_offset"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="setSelectionFromTop(-)" />    </LinearLayout>    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Hello World!" /></LinearLayout>
0 0
原创粉丝点击