效果圖:

我記得github上有一個類似的效果github類似效果
說一下實現(xiàn)這個效果的思路:在列表項中嵌入分段標(biāo)頭,然后根據(jù)需要顯示或者隱藏分段標(biāo)頭,創(chuàng)建一個特殊的TextView,讓其疊加在列表的頂部,當(dāng)列表滾動到一個新的分段時,就更新其內(nèi)容
創(chuàng)建列表布局
創(chuàng)建一個xml,隨列表滾動的分段標(biāo)頭和列表頂部的固定分段標(biāo)頭復(fù)用這個布局文件
header.xml
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/header" style="@android:style/TextAppearance.Small" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#0000ff" />
主布局list.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <include layout="@layout/header" /></FrameLayout>
創(chuàng)建列表項布局文件list_item.xml,包含數(shù)據(jù)項和分段標(biāo)頭
<?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="wrap_content" android:orientation="vertical" > <include layout="@layout/header" /> <TextView android:id="@+id/label" style="@android:style/TextAppearance.Large" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
SectionAdapter.java
public class SectionAdapter extends ArrayAdapter<String> { private Activity activity; public SectionAdapter(Activity activity, String[] objects) { super(activity, R.layout.list_item, R.id.label, objects);//為自定義視圖指定XML布局文件 this.activity = activity; } @Override public View getView(int position, View view, ViewGroup parent) { if (view == null) { view = activity.getLayoutInflater().inflate(R.layout.list_item, parent, false); } TextView header = (TextView) view.findViewById(R.id.header); String label = getItem(position); if (position == 0//檢查列表項起始字母是否發(fā)生了改變,如果發(fā)生改變,該列表項就是分段中的第一項,修改分段標(biāo)頭的內(nèi)容并顯示該分段標(biāo)頭,否則隱藏 || getItem(position - 1).charAt(0) != label.charAt(0)) { header.setVisibility(View.VISIBLE); header.setText(label.substring(0, 1)); } else { header.setVisibility(View.GONE);//隱藏分段標(biāo)頭 } return super.getView(position, view, parent); }}主界面
public class Hack26Activity extends ListActivity { private TextView topHeader;//用于訪問分段標(biāo)頭 private int topVisiblePosition = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); topHeader = (TextView) findViewById(R.id.header); setListAdapter(new SectionAdapter(this, Countries.COUNTRIES)); //設(shè)置滾動監(jiān)聽器,當(dāng)用戶滾動列表時,檢查位置是否發(fā)生了變化,如果改變,調(diào)用setTopHeader更新懸浮的分段標(biāo)頭,當(dāng)列表第一次顯示時,確保根據(jù)第一個列表項初始化分段標(biāo)頭 getListView().setOnScrollListener( new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // Empty. } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem != topVisiblePosition) { topVisiblePosition = firstVisibleItem; setTopHeader(firstVisibleItem); } } }); setTopHeader(0); } private void setTopHeader(int pos) { final String text = Countries.COUNTRIES[pos].substring(0, 1); topHeader.setText(text);//更新文本內(nèi)容 }}以上這篇Android 為ListView添加分段標(biāo)頭的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答
圖片精選