android 按字母搜索

分享到:

在看Oplayer的时候看见滑动字母来实现listView的内容搜索,所以就把里面的核心的函数扣除来做了一个demo,分为两部分一个是布局,另一个就是代码了,具体的如下:

布局:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <FrameLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent"
            android:divider="@drawable/ic_line"
            android:drawSelectorOnTop="false"
            android:fadingEdge="none"
            android:listSelector="@android:color/transparent"
            android:scrollbars="none" />

        <TextView
            android:id="@+id/first_letter_overlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="50.0dip"
            android:layout_marginTop="49.0dip"
            android:background="@drawable/fast_scroller_overlay"
            android:gravity="center"
            android:paddingBottom="5.0dip"
            android:scaleType="center"
            android:textSize="43.0dip"
            android:visibility="gone" />
    </FrameLayout>

    <ImageView
        android:id="@+id/alphabet_scroller"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:paddingRight="0dip"
        android:scaleType="fitXY"
        android:src="@drawable/alphabet_scroller_bg" />

</LinearLayout>

 


里面涉及到了一个ImageView的图片

 

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/contact_list_scroll_pressed" />
    <item android:drawable="@drawable/contact_list_scroll_normal" />
</selector>


具体的代码如下:

 

 

package com.example.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Main6 extends Activity{

	ListView iListView;
	TextView showChar;
	ImageView myChar;
	List<String> iList;
	MAdapter mAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main5);
		iListView = (ListView)findViewById(R.id.list);
		showChar = (TextView)findViewById(R.id.first_letter_overlay);
		myChar = (ImageView)findViewById(R.id.alphabet_scroller);
		iList = initChar();
		Log.e("t", " "+iList.size());
		mAdapter = new MAdapter();
		iListView.setAdapter(mAdapter);
		myChar.setClickable(true);
		myChar.setOnTouchListener(asOnTouch);
	}
	
	/**
	 * A-Z
	 */
	private OnTouchListener asOnTouch = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:// 0
				myChar.setPressed(true);
				showChar.setVisibility(View.VISIBLE);
				mathScrollerPosition(event.getY());
				break;
			case MotionEvent.ACTION_UP:// 1
				myChar.setPressed(false);
				showChar.setVisibility(View.GONE);
				break;
			case MotionEvent.ACTION_MOVE:
				mathScrollerPosition(event.getY());
				break;
			}
			return false;
		}
	};
	/**
	 * 显示字符
	 * 
	 * @param y
	 */
	private void mathScrollerPosition(float y) {
		int height = myChar.getHeight();
		float charHeight = height / 28.0f;
		char c = 'A';
		if (y < 0)
			y = 0;
		else if (y > height)
			y = height;

		int index = (int) (y / charHeight) - 1;
		if (index < 0)
			index = 0;
		else if (index > 25)
			index = 25;

		String key = String.valueOf((char) (c + index));
		showChar.setText(key);

		int position = 0;
		if (index == 0)
			iListView.setSelection(0);
		else if (index == 25)
			iListView.setSelection(mAdapter.getCount() - 1);
		else {
			if (mAdapter != null) {
				for (int i = 0; i < iList.size(); i++) {
					if (iList.get(i).startsWith(key)) {
						iListView.setSelection(position);
						break;
					}
					position++;
				}
			}
		}
	}
	/**
	 * ListView的数据
	 * @return
	 */
	public List<String> initChar(){
		List<String> list = new ArrayList<String>();
		char c = 'A';
		for (int i = 0; i < 26; i++) {
			list.add(String.valueOf((char) (c + i)));
			list.add(String.valueOf((char) (c + i)));
			list.add(String.valueOf((char) (c + i)));
		}
		return list;
	}
	class MAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return iList.size();
		}

		@Override
		public Object getItem(int position) {
			return iList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView text = new TextView(getApplicationContext());
			text.setText(iList.get(position));
			return text;
		}
		
	}
}


为了方便我就用了字母来填充listView,其实原理是很简单的。具体的显示如下:

 

拖动后的显示效果:


里面使用的那两张图片也发出来



 

昵    称:
验证码:

相关文档: