国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 為ListView添加分段標(biāo)頭的方法

2019-12-12 03:04:20
字體:
供稿:網(wǎng)友

效果圖:

我記得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)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 英吉沙县| 登封市| 伽师县| 界首市| 柘城县| 灵璧县| 阿尔山市| 涿鹿县| 寿阳县| 屯门区| 兴宁市| 衡阳县| 芮城县| 霍邱县| 三河市| 河北区| 临西县| 盱眙县| 孙吴县| 农安县| 屯门区| 诸城市| 贡嘎县| 巫山县| 西盟| 大新县| 东乌| 万全县| 白山市| 胶州市| 罗田县| 阿城市| 棋牌| 渭源县| 安仁县| 交口县| 全州县| 娱乐| 周宁县| 青神县| 玉环县|