Android ListView实现快速定位联系人功能【转发】

整合了Android ListView分组界面布局改进java实现List中汉字字母混排序,实现了类似iOS那样的快速定位效果,如下图:

 

 

主要是MainActivity实现了 OnTouchListenerOnGestureListener接口。

其中onScroll方法如下:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {

    util(e2);

    return false;
}

util方法,根据手势位置,确定快速定位到那个字母:

public void util(MotionEvent e2) {
    int i = (int) ((e2.getRawY() – view.getTop()) / ((float) view
            .getHeight() / 26f));
    int j=0;
    switch (i) {
    case 0:
        int pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);
        break;

    case 1:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);
        break;

    case 2:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);
        break;

    case 3:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);
        break;

    case 4:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);
        break;

    case 5:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 6:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 7:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 8:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 9:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 10:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 11:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 12:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 13:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 14:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 15:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 16:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

    case 17:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 18:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 19:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

    case 20:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 21:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 22:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 23:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 24:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    case 25:
        pos = (myAdapter).getPositionForSection(j++);
        listView.setSelectionFromTop(pos, 0);

        break;

    default:
        break;
    }
}

这样就实现了随手势在右边区域滑动,listview快速定位。

快速滑动首字母提示参见:ListView快速滑动示例增加首字母提示

源代码见:http://bigcateasymorse.googlecode.com/svn/trunk/android-listview-separate-demo0.5/

 

 

 

 

 

你可能感兴趣的:(android,ListView)