本文介绍AndroID实现首字母导航条,先看张效果图,具体怎么实现看代码吧
具体的步骤
1.整体布局的显示
2. 实现A-Z的分组
3. 自定义A-Z的导航条
4. 中间显示/隐藏触摸到导航条具体的字母
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:background="#fff" androID:orIEntation="vertical" tools:context="com.example.firstnavigation.MainActivity"> <FrameLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <!--ListVIEw--> <ListVIEw androID:ID="@+ID/frIEnd_ListvIEw" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center" /> <!--中间显示的字母--> <TextVIEw androID:ID="@+ID/tv_first" androID:layout_wIDth="80dp" androID:layout_height="80dp" androID:layout_gravity="center" androID:text="A" androID:textcolor="#ff0000" androID:textSize="30sp" androID:visibility="gone" /> <!--自定义导航条--> <com.example.firstnavigation.SlIDVIEw androID:ID="@+ID/slIDVIEw" androID:layout_wIDth="30dp" androID:layout_height="match_parent" androID:layout_gravity="right|center" /> </FrameLayout></linearLayout>
item.xml ―-》ListVIEw对应item
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#fff" androID:orIEntation="vertical"> <!--首字母--> <TextVIEw androID:ID="@+ID/tv" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="#9e9d9d" androID:textcolor="#fff" androID:textSize="16sp" /> <!--首字母对应的首个汉字--> <TextVIEw androID:ID="@+ID/name" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textcolor="#262626" androID:textSize="16sp" /></linearLayout>
联系人Bean
public class Contact { //姓名 private String name; //姓名的首字母 private String firstWord; public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getFirstWord() { return firstWord; } public voID setFirstWord(String ch) { this.firstWord = ch; }}
ContactAdapter.java
public class ContactAdapter extends BaseAdapter { private ArrayList<Contact> arrayList; private Context context; private String pre = "A"; public ContactAdapter(ArrayList<Contact> arrayList,Context context) { this.arrayList = arrayList; this.context = context; } @OverrIDe public int getCount() { return arrayList.size(); } @OverrIDe public Object getItem(int position) { return arrayList.get(position); } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder vIEwHolder; if (convertVIEw == null) { vIEwHolder = new VIEwHolder(); convertVIEw = LayoutInflater.from(context).inflate(R.layout.item,parent,false); vIEwHolder.tv_firstWord = (TextVIEw) convertVIEw.findVIEwByID(R.ID.tv); vIEwHolder.name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.name); convertVIEw.setTag(vIEwHolder); } else { vIEwHolder = (VIEwHolder) convertVIEw.getTag(); } vIEwHolder.tv_firstWord.setText(String.valueOf(arrayList.get(position).getFirstWord())); vIEwHolder.name.setText(arrayList.get(position).getname()); /** * 分组:根据汉字的首字母 */ vIEwHolder.tv_firstWord.setVisibility(!arrayList.get(position).getFirstWord().equals(pre) ? VIEw.VISIBLE : VIEw.GONE); pre = arrayList.get(position).getFirstWord(); return convertVIEw; } class VIEwHolder { TextVIEw tv_firstWord; TextVIEw name; }
MainActivity
public class MainActivity extends AppCompatActivity { private ListVIEw ListVIEw; private TextVIEw tv_first; private ArrayList<Contact> contacts; private SlIDVIEw slIDVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEw(); initData(); } private voID initVIEw() { ListVIEw = (ListVIEw) findVIEwByID(R.ID.frIEnd_ListvIEw); tv_first = (TextVIEw) findVIEwByID(R.ID.tv_first); slIDVIEw = (SlIDVIEw) findVIEwByID(R.ID.slIDVIEw); slIDVIEw.setFirstListener(new OntouchFirstListener() { @OverrIDe public voID ontouch(String str) { tv_first.setVisibility(VIEw.VISIBLE); tv_first.setText(str); for (int i = 0; i < contacts.size(); i++) { if (str.equals(contacts.get(i).getFirstWord())) { ListVIEw.setSelection(i); } } } @OverrIDe public voID onRelease() { new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { tv_first.setVisibility(VIEw.GONE); } },300); } }); } private voID initData() { contacts = new ArrayList<>(); //获取字符数组资源 String[] attrs = getResources().getStringArray(R.array.myFrIEnd); Contact contact; for (int i = 0; i < attrs.length; i++) { contact = new Contact(); contact.setname(attrs[i]); contact.setFirstWord(getPinYinheadChar(attrs[i],2)); contacts.add(contact); } //排序A-Z Collections.sort(contacts,new Comparator<Contact>() { @OverrIDe public int compare(Contact lhs,Contact rhs) { return lhs.getFirstWord().compareto(rhs.getFirstWord()); } }); ContactAdapter contactAdapter = new ContactAdapter(contacts,this); ListVIEw.setAdapter(contactAdapter); } /** * 提取汉字的首字母,如果里面含有费中文字符则忽略之;如果全为非中文则返回""。 * * @param caseType 当为1时获取的首字母为小写,否则为大写。 */ public static String getPinYinheadChar(String zn_str,int caseType) { if (zn_str != null && !zn_str.trim().equalsIgnoreCase("")) { char[] strChar = zn_str.tochararray(); // 汉语拼音格式输出类 HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat(); // 输出设置,大小写,音标方式等 if (1 == caseType) { hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); } else { hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); } hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuffer pyStringBuffer = new StringBuffer(); char c = strChar[0]; char pyc; if (String.valueOf(c).matches("[\u4E00-\u9FA5]+")) {//是中文或者a-z或者A-Z转换拼音 try { String[] pyStirngArray = PinyinHelper.toHanyuPinyinStringArray(strChar[0],hanYuPinOutputFormat); if (null != pyStirngArray && pyStirngArray[0] != null) { pyc = pyStirngArray[0].charat(0); pyStringBuffer.append(pyc); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printstacktrace(); } } return pyStringBuffer.toString(); } return null; }
提取首字母需要用到Pingyin4j.jar,小编在这不提供,大家可以在网上下载
//自定义字母导航控件public class SlIDVIEw extends VIEw { private String[] strs = {"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"}; private Paint paint; private OntouchFirstListener Listener; public SlIDVIEw(Context context) { this(context,null); } public SlIDVIEw(Context context,AttributeSet attrs) { this(context,attrs,0); } public SlIDVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); paint = new Paint(); paint.setAntiAlias(true); paint.setcolor(color.GRAY); paint.setTypeface(Typeface.DEFAulT_BolD); } // px---->sp protected int sp2px(int spVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getdisplayMetrics()); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < strs.length; i++) { paint.setcolor(i == index ? color.RED : color.BLACK); paint.setTextSize(i == index ? sp2px(18) : sp2px(16)); int x = (int) (getWIDth() - paint.measureText(strs[i])) / 2; int y = getHeight() / strs.length * (i + 1); canvas.drawText(strs[i],x,y,paint); } } int index = -1; //获取触摸到字母索引的位置 //触碰事件 //按下,松开,拖动 @OverrIDe public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: float y = event.getY(); this.setBackgroundcolor(color.GRAY); //获取触摸到字母的位置 index = (int) y * strs.length / getHeight(); Listener.ontouch(strs[index]); break; case MotionEvent.ACTION_UP: this.setBackgroundcolor(androID.R.color.transparent); index = -1; Listener.onRelease(); break; } //重绘 invalIDate(); return true; } public voID setFirstListener(OntouchFirstListener Listener) { this.Listener = Listener; }}/** * OntouchFirstListener 接口 * ontouch:触摸到了那个字母 * onRelease:up释放时中间显示的字母需要设置为GONE */public interface OntouchFirstListener { voID ontouch(String ch); voID onRelease();}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android仿微信实现首字母导航条全部内容,希望文章能够帮你解决Android仿微信实现首字母导航条所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)