public class Content {
Private String letter;
private String name;
Public Content (String letter, String name) {
Enclosing letter=letter;
This. Name=name;
}
Public String getLetter () {
The return letter;
}
Public void setLetter (String letter) {
Enclosing letter=letter;
}
Public String getName () {
Return the name;
}
Public void elegantly-named setName (String name) {
This. Name=name;
}
}
public class MainActivity extends the Activity
{
Private ListView mListView;
Public void onCreate (Bundle savedInstanceState)
{
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);
MListView=(ListView) the findViewById (R.i d.l istInfo);
//initialization data
ListList=new ArrayList (a);
For (int I=0; I & lt; 10; I++) {
The Content of m.
If (I & lt; 3)
M=new Content (" A ", "b");
Else if (I & lt; 6)
M=new Content (" F ", "party");
The else
M=new Content (" D ", "east");
List. Add (m);
}
//instantiate custom content adaptation class
MyAdapter adapter=new MyAdapter (this, a list);
//for listView
MListView. SetAdapter (adapter);
}
}
public class MyAdapter extends BaseAdapter {
Private ListList=null;
Private Context mContext;
Public MyAdapter (Context mContext ListThe list) {
Enclosing mContext=mContext;
Enclosing a list=list;
}
Public int getCount () {
Return this. The list. The size ();
}
Public Object the getItem (int position) {
return null;
}
Public long getItemId (int position) {
Return the position;
}
Public View getView (final int position, View View and ViewGroup arg2) {
ViewHolder ViewHolder=null;
If (view==null) {
ViewHolder=new viewHolder ();
View=LayoutInflater. The from (mContext). Inflate (R.l ayout. Item, null);
ViewHolder. TvTitle=(TextView) view. The findViewById (R.i which itle);
ViewHolder. TvLetter=(TextView) view. The findViewById (R.i, dc atalog);
The setTag (viewHolder);
} else {
ViewHolder=(viewHolder) the getTag ();
}
The final Content mContent=list. Get (position);
If (position==0) {
ViewHolder. TvLetter. SetVisibility (the VISIBLE).
ViewHolder. TvLetter. SetText (mContent getLetter ());
} else {
String lastCatalog=list. Get (position 1) getLetter ();
If (mContent. GetLetter (.) the equals (lastCatalog)) {
ViewHolder. TvLetter. SetVisibility (View. GONE);
} else {
ViewHolder. TvLetter. SetVisibility (the VISIBLE).
ViewHolder. TvLetter. SetText (mContent getLetter ());
}
}
ViewHolder. TvTitle. SetText (enclosing a list. The get (position). The getName ());
Return the view;
}
Final static class ViewHolder {
TextView tvTitle;
TextView tvLetter;
}
Public Object [] getSections () {
//TODO Auto - generated method stub
return null;
}
Public int getSectionForPosition (int position) {
return 0;
}
Public int getPositionForSection (int section) {
The Content mContent;
String l;
If (section=='! ') {
return 0;
} else {
For (int I=0; I & lt; GetCount (); I++) {
MContent=(Content) list. Get (I);
L=mContent. GetLetter ();
Char firstChar=l.t oUpperCase (). The charAt (0);
If (firstChar==section) {
Return the I + 1;
}
}
}
MContent=null;
L=null;
return -1;
}
}
CodePudding user response:
& lt; RelativeLayout 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"
Tools: context="com. Example. Action. MainActivity" & gt;The android: id="@ + id/list_title"
Android: layout_width="fill_parent"
Android: layout_height="wrap_content"
Android: textSize="18 dp"
Android: padding="5 dp"
Android: background="# e1e1e1"
Android: textColor="# f00"
Android: text="B"
Android: layout_alignParentTop="true"
Android: visibility="visible"/& gt;Android: layout_below="@ id/list_title"
Android: focusable="true"
Android: layout_height="wrap_content"
Android: layout_width="fill_parent"
Android: scrollbars="none"
Android: cacheColorHint="# 00000000"
Android: background="# FFF"/& gt;
& lt; ? The XML version="1.0" encoding="utf-8"?>nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull