Android實現(xiàn)qq列表式的分類懸浮提示
效果圖:

這種效果的實現(xiàn)這里是采用自定義ExpandableListView,給它設置一個指示布局,在滑動過程中監(jiān)聽當前是否應該懸浮顯示分類來實現(xiàn)的。今天抽時間,整理了下代碼,記錄一下使用過程,以便有類似的需求的時候可以快速搞定。
話不多說,我們直接看代碼和使用方法。
一 項目結構

上邊兒三個類分別是我們的自定義ExpandableListView,主界面,以及ExpandableListView使用的Adapter。下邊兒幾個xml文件分別是主界面布局,指示器布局,ExpandableListView子項布局,ExpandableListView組布局。
二 實現(xiàn)代碼
1.在xml中聲明自定義ExpandableListView
<test.com.expandablelistviewdemo.CustomExpandListview //這里不唯一,看你具體把CustomExpandListview放在哪里 android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"></test.com.expandablelistviewdemo.CustomExpandListview>
2.聲明數(shù)據(jù)源相關(這里為了演示,數(shù)據(jù)全是String類型,看具體需求可改變)
private String[] parentSource = {"分類1", "分類2", "分類3", "分類4", "分類5"};
private ArrayList<String> parent = new ArrayList<>();
private Map<String, ArrayList<String>> datas = new HashMap<>();
3.初始化演示數(shù)據(jù)
//種類
for (int i = 0; i < parentSource.length; i++) {
parent.add(parentSource[i]);
}
//給每個種類添加模擬數(shù)據(jù)
for (int i = 0; i < parent.size(); i++) {
String str = parent.get(i);
ArrayList<String> temp = new ArrayList<>();
for (int j = 0; j < 20; j++) {
temp.add("" + j);
}
datas.put(str, temp);
}
4.初始化Adapter以及使用
myAdapter = new MyAdapter(this, parent, datas, listview); listview.setAdapter(myAdapter);
在初始化adapter的時候,可以看到我們在構造方法中傳入了上下文對象,種類,數(shù)據(jù),以及我們的CustomExpandListview對象,所以在CustomExpandListview 中我們要添加相應的構造方法。
5.設置懸浮提示布局
listview.setHeaderView(getLayoutInflater().inflate(R.layout.indictor_layout, listview, false));
6.其他
默認全部展開
for (int i = 0; i < parent.size(); i++) {
listview.expandGroup(i);
}
item點擊事件
listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
Toast.makeText(MainActivity.this, "點擊了第" + (i + 1) + " 類的第" + i1 + "項", Toast.LENGTH_SHORT).show();
return true;
}
}
);
三 總結
從上邊兒的步驟可以看出,使用CustomExpandListview實現(xiàn)圖中的效果是非常容易的,以上就是這篇文章的全部內容,希望對大家的學習或工作帶來一定的幫助,如果有疑問可以留言交流。
- Android仿京東分類模塊左側分類條目效果
- Android使用Scroll+Fragment仿京東分類效果
- Android 仿京東、拼多多商品分類頁的示例代碼
- Android實現(xiàn)網(wǎng)易Tab分類排序控件實現(xiàn)
- Android使用分類型RecyclerView仿各大商城首頁
- Android編程實現(xiàn)仿美團或淘寶的多級分類菜單效果示例【附demo源碼下載】
- android使用 ScrollerView 實現(xiàn) 可上下滾動的分類欄實例
- Android 仿網(wǎng)易新聞客戶端分類排序功能
- Android學習教程之分類側滑菜單(5)
- Android實現(xiàn)京東App分類頁面效果
相關文章
Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法
這篇文章主要介紹了Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法的相關資料,需要的朋友可以參考下2016-05-05
android將圖片轉換存到數(shù)據(jù)庫再從數(shù)據(jù)庫讀取轉換成圖片實現(xiàn)代碼
有時候我們想把圖片存入到數(shù)據(jù)庫中,盡管這不是一種明智的選擇,但有時候還是不得以會用到,下面說說將圖片轉換成byte[]數(shù)組存入到數(shù)據(jù)庫中去,并從數(shù)據(jù)庫中取出來轉換成圖像顯示出來2013-11-11
Android中backgroundDimEnabled的作用
這篇文章主要介紹了Android中backgroundDimEnabled的作用的相關資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內容,需要的朋友可以參考下2017-10-10

