public class Content {
private String letter;
private String name;
public Content(String letter, String name) {
this.letter = letter;
this.name = name;
}
public String getLetter() {
return letter;
}
public void setLetter(String letter) {
this.letter = letter;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class MainActivity extends Activity
{
private ListView mListView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listInfo);
//初始化資料
List<Content> list = new ArrayList<Content>();
for (int i = 0; i < 10; i++) {
Content m;
if (i < 3)
m = new Content("A", "啊");
else if (i < 6)
m = new Content("F", "方");
else
m = new Content("D", "東");
list.add(m);
}
// 實體化自定義內容適配類
MyAdapter adapter = new MyAdapter(this, list);
// 為listView設定適配
mListView.setAdapter(adapter);
}
}
public class MyAdapter extends BaseAdapter {
private List<Content> list = null;
private Context mContext;
public MyAdapter(Context mContext, List<Content> list) {
this.mContext = mContext;
this.list = list;
}
public int getCount() {
return this.list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder viewHolder = null;
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
final Content mContent = list.get(position);
if (position == 0) {
viewHolder.tvLetter.setVisibility(View.VISIBLE);
viewHolder.tvLetter.setText(mContent.getLetter());
} else {
String lastCatalog = list.get(position - 1).getLetter();
if (mContent.getLetter().equals(lastCatalog)) {
viewHolder.tvLetter.setVisibility(View.GONE);
} else {
viewHolder.tvLetter.setVisibility(View.VISIBLE);
viewHolder.tvLetter.setText(mContent.getLetter());
}
}
viewHolder.tvTitle.setText(this.list.get(position).getName());
return 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) {
Content mContent;
String l;
if (section == '!') {
return 0;
} else {
for (int i = 0; i < getCount(); i++) {
mContent = (Content) list.get(i);
l = mContent.getLetter();
char firstChar = l.toUpperCase().charAt(0);
if (firstChar == section) {
return i + 1;
}
}
}
mContent = null;
l = null;
return -1;
}
}
uj5u.com熱心網友回復:
<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" >
<TextView
android:id="@+id/list_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:padding="5dp"
android:background="#e1e1e1"
android:textColor="#f00"
android:text="B"
android:layout_alignParentTop="true"
android:visibility="visible" />
<ListView android:id="@+id/listInfo"
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" />
</RelativeLayout>
<?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:orientation="vertical" >
<TextView
android:id="@+id/catalog"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingRight="4.0dip"
android:background="#aa1885d9"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="1.0"
android:paddingLeft="5dip"
android:gravity="center_vertical" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="8dip"
android:gravity="center_vertical"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:singleLine="true" />
</LinearLayout>
uj5u.com熱心網友回復:
使用 ExpandableListView,可以實作多級效果;并且點擊的時候獲取到position;轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/256937.html
標籤:Android
上一篇:webview加載的網頁如何實作能夠橫豎屏切換,就是手機旋轉會橫豎屏切換顯示
下一篇:求大佬幫助,開發安卓軟體咋辦?
