源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android实现qq列表式的分类悬浮提示

  • 时间:2021-01-09 05:42 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现qq列表式的分类悬浮提示
[b]效果图:[/b] [img]http://files.jb51.net/file_images/article/201609/201698105439170.gif?201688105454[/img] 这种效果的实现这里是采用自定义[code]ExpandableListView[/code],给它设置一个指示布局,在滑动过程中监听当前是否应该悬浮显示分类来实现的。今天抽时间,整理了下代码,记录一下使用过程,以便有类似的需求的时候可以快速搞定。 话不多说,我们直接看代码和使用方法。 [b]一 项目结构[/b] [img]http://files.jb51.net/file_images/article/201609/201698105530314.png?201688105552[/img] 上边儿三个类分别是我们的自定义[code]ExpandableListView[/code],主界面,以及[code]ExpandableListView[/code]使用的[code]Adapter[/code]。下边儿几个xml文件分别是主界面布局,指示器布局,[code]ExpandableListView[/code]子项布局,[code]ExpandableListView[/code]组布局。 [b]二 实现代码[/b] [b]1.在xml中声明自定义ExpandableListView[/b]
<test.com.expandablelistviewdemo.CustomExpandListview //这里不唯一,看你具体把CustomExpandListview放在哪里
android:id="@+id/listView" 
android:layout_width="match_parent" 
android:layout_height="match_parent"></test.com.expandablelistviewdemo.CustomExpandListview>
[b]2.声明数据源相关(这里为了演示,数据全是String类型,看具体需求可改变)[/b]
private String[] parentSource = {"分类1", "分类2", "分类3", "分类4", "分类5"};
private ArrayList<String> parent = new ArrayList<>();
private Map<String, ArrayList<String>> datas = new HashMap<>();
[b]3.初始化演示数据[/b]
//种类
for (int i = 0; i < parentSource.length; i++) { 
parent.add(parentSource[i]);
}
//给每个种类添加模拟数据
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);
}
[b]4.初始化Adapter以及使用[/b]
myAdapter = new MyAdapter(this, parent, datas, listview);
listview.setAdapter(myAdapter);
在初始化[code]adapter[/code]的时候,可以看到我们在构造方法中传入了上下文对象,种类,数据,以及我们的[code]CustomExpandListview[/code]对象,所以在[code]CustomExpandListview [/code]中我们要添加相应的构造方法。 [b]5.设置悬浮提示布局[/b]
listview.setHeaderView(getLayoutInflater().inflate(R.layout.indictor_layout, listview, false));
[b]6.其他[/b] 默认全部展开
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; 
 }
}
);
[b]三 总结[/b] 从上边儿的步骤可以看出,使用CustomExpandListview实现图中的效果是非常容易的,以上就是这篇文章的全部内容,希望对大家的学习或工作带来一定的帮助,如果有疑问可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部