欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android之IphoneTreeView带组指示器的ExpandableListView效果

程序员文章站 2023-12-01 20:37:46
之前实现过一次这种效果的expandablelistview:,带效果比较挫,最近,在参考联系人源码pinnedheaderlistview,以及网上各位大侠的源码,封装了...
之前实现过一次这种效果的expandablelistview:,带效果比较挫,最近,在参考联系人源码pinnedheaderlistview,以及网上各位大侠的源码,封装了一个效果最好,而且使用最简单的iphonetreeview,下面先看看效果图:
Android之IphoneTreeView带组指示器的ExpandableListView效果 
首先让我们看看封装得比较完善的iphonetreeview:
复制代码 代码如下:

public class iphonetreeview extends expandablelistview implements
onscrolllistener, ongroupclicklistener {
public iphonetreeview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
registerlistener();
}
public iphonetreeview(context context, attributeset attrs) {
super(context, attrs);
registerlistener();
}
public iphonetreeview(context context) {
super(context);
registerlistener();
}
/**
* adapter 接口 . 列表必须实现此接口 .
*/
public interface iphonetreeheaderadapter {
public static final int pinned_header_gone = 0;
public static final int pinned_header_visible = 1;
public static final int pinned_header_pushed_up = 2;
/**
* 获取 header 的状态
*
* @param groupposition
* @param childposition
* @return
* pinned_header_gone,pinned_header_visible,pinned_header_pushed_up
* 其中之一
*/
int gettreeheaderstate(int groupposition, int childposition);
/**
* 配置 qqheader, 让 qqheader 知道显示的内容
*
* @param header
* @param groupposition
* @param childposition
* @param alpha
*/
void configuretreeheader(view header, int groupposition,
int childposition, int alpha);
/**
* 设置组按下的状态
*
* @param groupposition
* @param status
*/
void onheadviewclick(int groupposition, int status);
/**
* 获取组按下的状态
*
* @param groupposition
* @return
*/
int getheadviewclickstatus(int groupposition);
}
private static final int max_alpha = 255;
private iphonetreeheaderadapter madapter;
/**
* 用于在列表头显示的 view,mheaderviewvisible 为 true 才可见
*/
private view mheaderview;
/**
* 列表头是否可见
*/
private boolean mheaderviewvisible;
private int mheaderviewwidth;
private int mheaderviewheight;
public void setheaderview(view view) {
mheaderview = view;
abslistview.layoutparams lp = new abslistview.layoutparams(
viewgroup.layoutparams.match_parent,
viewgroup.layoutparams.wrap_content);
view.setlayoutparams(lp);
if (mheaderview != null) {
setfadingedgelength(0);
}
requestlayout();
}
private void registerlistener() {
setonscrolllistener(this);
setongroupclicklistener(this);
}
/**
* 点击 headerview 触发的事件
*/
private void headerviewclick() {
long packedposition = getexpandablelistposition(this
.getfirstvisibleposition());
int groupposition = expandablelistview
.getpackedpositiongroup(packedposition);
if (madapter.getheadviewclickstatus(groupposition) == 1) {
this.collapsegroup(groupposition);
madapter.onheadviewclick(groupposition, 0);
} else {
this.expandgroup(groupposition);
madapter.onheadviewclick(groupposition, 1);
}
this.setselectedgroup(groupposition);
}
private float mdownx;
private float mdowny;
/**
* 如果 headerview 是可见的 , 此函数用于判断是否点击了 headerview, 并对做相应的处理 , 因为 headerview
* 是画上去的 , 所以设置事件监听是无效的 , 只有自行控制 .
*/
@override
public boolean ontouchevent(motionevent ev) {
if (mheaderviewvisible) {
switch (ev.getaction()) {
case motionevent.action_down:
mdownx = ev.getx();
mdowny = ev.gety();
if (mdownx <= mheaderviewwidth && mdowny <= mheaderviewheight) {
return true;
}
break;
case motionevent.action_up:
float x = ev.getx();
float y = ev.gety();
float offsetx = math.abs(x - mdownx);
float offsety = math.abs(y - mdowny);
// 如果 headerview 是可见的 , 点击在 headerview 内 , 那么触发 headerclick()
if (x <= mheaderviewwidth && y <= mheaderviewheight
&& offsetx <= mheaderviewwidth
&& offsety <= mheaderviewheight) {
if (mheaderview != null) {
headerviewclick();
}
return true;
}
break;
default:
break;
}
}
return super.ontouchevent(ev);
}
@override
public void setadapter(expandablelistadapter adapter) {
super.setadapter(adapter);
madapter = (iphonetreeheaderadapter) adapter;
}
/**
*
* 点击了 group 触发的事件 , 要根据根据当前点击 group 的状态来
*/
@override
public boolean ongroupclick(expandablelistview parent, view v,
int groupposition, long id) {
if (madapter.getheadviewclickstatus(groupposition) == 0) {
madapter.onheadviewclick(groupposition, 1);
parent.expandgroup(groupposition);
parent.setselectedgroup(groupposition);
} else if (madapter.getheadviewclickstatus(groupposition) == 1) {
madapter.onheadviewclick(groupposition, 0);
parent.collapsegroup(groupposition);
}
// 返回 true 才可以弹回第一行 , 不知道为什么
return true;
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
super.onmeasure(widthmeasurespec, heightmeasurespec);
if (mheaderview != null) {
measurechild(mheaderview, widthmeasurespec, heightmeasurespec);
mheaderviewwidth = mheaderview.getmeasuredwidth();
mheaderviewheight = mheaderview.getmeasuredheight();
}
}
private int moldstate = -1;
@override
protected void onlayout(boolean changed, int left, int top, int right,
int bottom) {
super.onlayout(changed, left, top, right, bottom);
final long flatpostion = getexpandablelistposition(getfirstvisibleposition());
final int grouppos = expandablelistview
.getpackedpositiongroup(flatpostion);
final int childpos = expandablelistview
.getpackedpositionchild(flatpostion);
int state = madapter.gettreeheaderstate(grouppos, childpos);
if (mheaderview != null && madapter != null && state != moldstate) {
moldstate = state;
mheaderview.layout(0, 0, mheaderviewwidth, mheaderviewheight);
}
configureheaderview(grouppos, childpos);
}
public void configureheaderview(int groupposition, int childposition) {
if (mheaderview == null || madapter == null
|| ((expandablelistadapter) madapter).getgroupcount() == 0) {
return;
}
int state = madapter.gettreeheaderstate(groupposition, childposition);
switch (state) {
case iphonetreeheaderadapter.pinned_header_gone: {
mheaderviewvisible = false;
break;
}
case iphonetreeheaderadapter.pinned_header_visible: {
madapter.configuretreeheader(mheaderview, groupposition,
childposition, max_alpha);
if (mheaderview.gettop() != 0) {
mheaderview.layout(0, 0, mheaderviewwidth, mheaderviewheight);
}
mheaderviewvisible = true;
break;
}
case iphonetreeheaderadapter.pinned_header_pushed_up: {
view firstview = getchildat(0);
int bottom = firstview.getbottom();
// intitemheight = firstview.getheight();
int headerheight = mheaderview.getheight();
int y;
int alpha;
if (bottom < headerheight) {
y = (bottom - headerheight);
alpha = max_alpha * (headerheight + y) / headerheight;
} else {
y = 0;
alpha = max_alpha;
}
madapter.configuretreeheader(mheaderview, groupposition,
childposition, alpha);
if (mheaderview.gettop() != y) {
mheaderview.layout(0, y, mheaderviewwidth, mheaderviewheight
+ y);
}
mheaderviewvisible = true;
break;
}
}
}
@override
/**
* 列表界面更新时调用该方法(如滚动时)
*/
protected void dispatchdraw(canvas canvas) {
super.dispatchdraw(canvas);
if (mheaderviewvisible) {
// 分组栏是直接绘制到界面中,而不是加入到viewgroup中
drawchild(canvas, mheaderview, getdrawingtime());
}
}
@override
public void onscroll(abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {
final long flatpos = getexpandablelistposition(firstvisibleitem);
int groupposition = expandablelistview.getpackedpositiongroup(flatpos);
int childposition = expandablelistview.getpackedpositionchild(flatpos);
configureheaderview(groupposition, childposition);
}
@override
public void onscrollstatechanged(abslistview view, int scrollstate) {
}
}

使用起来也是比较简单的,先在布局文件中声明activity_main.xml:
复制代码 代码如下:

<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=".mainactivity" >
<com.way.iphonetreeview.iphonetreeview
android:id="@+id/iphone_tree_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:cachecolorhint="@android:color/transparent"
android:divider="@null"
android:transcriptmode="normal" />
</relativelayout>

然后在mainactivity中调用,为了缩减代码,我把adapter作为内部类放在mainactivity中了:
复制代码 代码如下:

public class mainactivity extends activity {
private layoutinflater minflater;
private iphonetreeview iphonetreeview;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
initview();
}
private void initview() {
// todo auto-generated method stub
minflater = layoutinflater.from(this);
iphonetreeview = (iphonetreeview) findviewbyid(r.id.iphone_tree_view);
iphonetreeview.setheaderview(getlayoutinflater().inflate(
r.layout.list_head_view, iphonetreeview, false));
iphonetreeview.setgroupindicator(null);
iphonetreeview.setadapter(new iphonetreeviewadapter());
}
public class iphonetreeviewadapter extends baseexpandablelistadapter
implements iphonetreeheaderadapter {
// sample data set. children[i] contains the children (string[]) for
// groups[i].
private hashmap<integer, integer> groupstatusmap;
private string[] groups = { "第一组", "第二组", "第三组", "第四组" };
private string[][] children = {
{ "way", "arnold", "barry", "chuck", "david", "afghanistan",
"albania", "belgium", "lily", "jim", "liming", "jodan" },
{ "ace", "bandit", "cha-cha", "deuce", "bahamas", "china",
"dominica", "jim", "liming", "jodan" },
{ "fluffy", "snuggles", "ecuador", "ecuador", "jim", "liming",
"jodan" },
{ "goldy", "bubbles", "iceland", "iran", "italy", "jim",
"liming", "jodan" } };
public iphonetreeviewadapter() {
// todo auto-generated constructor stub
groupstatusmap = new hashmap<integer, integer>();
}
public object getchild(int groupposition, int childposition) {
return children[groupposition][childposition];
}
public long getchildid(int groupposition, int childposition) {
return childposition;
}
public int getchildrencount(int groupposition) {
return children[groupposition].length;
}
public object getgroup(int groupposition) {
return groups[groupposition];
}
public int getgroupcount() {
return groups.length;
}
public long getgroupid(int groupposition) {
return groupposition;
}
public boolean ischildselectable(int groupposition, int childposition) {
return true;
}
public boolean hasstableids() {
return true;
}
@override
public view getchildview(int groupposition, int childposition,
boolean islastchild, view convertview, viewgroup parent) {
// todo auto-generated method stub
if (convertview == null) {
convertview = minflater.inflate(r.layout.list_item_view, null);
}
textview tv = (textview) convertview
.findviewbyid(r.id.contact_list_item_name);
tv.settext(getchild(groupposition, childposition).tostring());
textview state = (textview) convertview
.findviewbyid(r.id.cpntact_list_item_state);
state.settext("爱生活...爱android...");
return convertview;
}
@override
public view getgroupview(int groupposition, boolean isexpanded,
view convertview, viewgroup parent) {
// todo auto-generated method stub
if (convertview == null) {
convertview = minflater.inflate(r.layout.list_group_view, null);
}
textview groupname = (textview) convertview
.findviewbyid(r.id.group_name);
groupname.settext(groups[groupposition]);
imageview indicator = (imageview) convertview
.findviewbyid(r.id.group_indicator);
textview onlinenum = (textview) convertview
.findviewbyid(r.id.online_count);
onlinenum.settext(getchildrencount(groupposition) + "/"
+ getchildrencount(groupposition));
if (isexpanded) {
indicator.setimageresource(r.drawable.indicator_expanded);
} else {
indicator.setimageresource(r.drawable.indicator_unexpanded);
}
return convertview;
}
@override
public int gettreeheaderstate(int groupposition, int childposition) {
final int childcount = getchildrencount(groupposition);
if (childposition == childcount - 1) {
return pinned_header_pushed_up;
} else if (childposition == -1
&& !iphonetreeview.isgroupexpanded(groupposition)) {
return pinned_header_gone;
} else {
return pinned_header_visible;
}
}
@override
public void configuretreeheader(view header, int groupposition,
int childposition, int alpha) {
// todo auto-generated method stub
((textview) header.findviewbyid(r.id.group_name))
.settext(groups[groupposition]);
((textview) header.findviewbyid(r.id.online_count))
.settext(getchildrencount(groupposition) + "/"
+ getchildrencount(groupposition));
}
@override
public void onheadviewclick(int groupposition, int status) {
// todo auto-generated method stub
groupstatusmap.put(groupposition, status);
}
@override
public int getheadviewclickstatus(int groupposition) {
if (groupstatusmap.containskey(groupposition)) {
return groupstatusmap.get(groupposition);
} else {
return 0;
}
}
}
}

好了,简单的一个例子就完成了,

总结一下:
原理: 在正在显示的最上面的组的标签位置添加一个和组视图完全一样的视图,作为组标签。这个标签的位置要随着列表的滑动不断变化,以保持总是显示在最上方,并且该消失的时候就消失。给这个标签添加点击事件,实现打开和关闭分组的功能。

组标签总是显示在上方,这是通过不断的调整其在布局中的位置来实现的。这个调整的过程,在初始化的时候,在 onlayout 方法中实现一次,后面都是在滚动过程中,根据对滚动状态的监听来实现的。

实例化要添加的标签的时候(在外面实现,即使调用 settreeheaderview之前),parent 要设为该expandablelistview.

要学习以及好好理解这个,最好的方法是将添加进来的组标签设为半透明,便于观察整个过程。
源码下载