3
回答
android字母导航条的简单实现
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

实现效果

#Activity

package a.b;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TestLetterNavActivity extends Activity {
	private final static char[] LETTERS = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(new SimpleAdapter(this, getDatas(),
				android.R.layout.simple_list_item_2, 
				new String[]{"id","val"}, 
				new int[]{android.R.id.text1,android.R.id.text2}));
        
        //动态添加组件
        LinearLayout.LayoutParams lp_ff = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout5);
        for(char a : LETTERS){
            final TextView tv = new TextView(this);
            tv.setLayoutParams(lp_ff);
            tv.setText(String.valueOf(a));
            tv.setTextAppearance(TestLetterNavActivity.this, R.style.letterNavStyle);
            tv.setGravity(Gravity.CENTER);
            tv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String s = tv.getText().toString();
					Toast.makeText(TestLetterNavActivity.this, s, Toast.LENGTH_SHORT).show();
					lv.setSelection(getStartIndex(s));
				}
			});
            ll.addView(tv);
        }
    }
    
    private List<Map<String,String>> getDatas(){
    	List<Map<String,String>> returnList = new ArrayList<Map<String,String>>();
    	for(int i=1;i<=260;i++){
    		Map<String,String> map = new HashMap<String,String>();
    		map.put("id", "id-"+i);
    		map.put("val", "value-"+i);
    		returnList.add(map);
   	}
    	return returnList;
    }
    
    private int getStartIndex(String letter){
    	int returnInt = 0;
    	for(int i=0;i<LETTERS.length;i++){
    		if(letter.equals(String.valueOf(LETTERS[i]))){
    			returnInt = (260/LETTERS.length)*i;
    			break;
    		}
    	}
    	return returnInt;
    }
}
#layout

<?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:orientation="vertical">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="horizontal" android:gravity="center_vertical|center_horizontal" android:layout_weight="7">
        <Spinner android:layout_weight="1" android:layout_width="wrap_content" android:id="@+id/spinner1" android:layout_height="wrap_content"></Spinner>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent" android:orientation="horizontal">
        <LinearLayout android:id="@+id/linearLayout4" android:orientation="vertical" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
            <ListView android:id="@+id/listView1" android:layout_height="fill_parent" android:layout_width="fill_parent"></ListView>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout5" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#a0a0a0" android:layout_weight="10">
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
#style

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="letterNavStyle">
        <item name="android:textColor">#000000</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">10dip</item>
    </style>
</resources>

逻辑说明:

自造了一个结果集,里边有260个元素

一共26个字母,每个10个元素

真是环境去查询数据库即可

大体实现思路是:

维护数据,如:录入联系人,通过pinyin4j将联系人的首字母弄出来后存在表中

然后当用户点击字母导航条时,通过所选择的字母到数据库中计算位置

然后通过listview.setSelection定位来实现

如果大家有更好的方法,记得要分享啊

欢迎斧正

个人声明:恺哥做事向来有始有终

此次抛砖引玉,是为了兑现http://www.oschina.net/question/96894_24665该贴的诺言

有方案后一定分享

共同学习,共同进步

举报
恺哥
发帖于6年前 3回/6K+阅
顶部