iphone里面系统自带的滚动控件,在多个分组的情况下,头部总能显示当前所在分组,点击头部可以收缩该分组,效果很不错。参考了android现有的资源实现,做个优化。参考地址如下:http://androiddada.iteye.com/blog/1340228 http://www.cnblogs.com/chenyingzhong/archive/2011/07/23/2114692.html 大家也可以在原先基础上修改,也可以参考本篇拿来直接用(如果符合你的需求的话)。
上代码,共三个java文件,一个xml文件。为什么不上项目源码,在机密模式下工作,被限制,有些同志应该了解。。。
第一个是自定义ExpandableListView:
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.PopupWindow;
/**
*
* @Description 起名:快速定位可扩展ListView
* @author zg
* @date 2014-1-20 下午3:41:38
*/
public class QuickLocExpandableLisView extends ExpandableListView implements
OnScrollListener {
private Context context;
private PopupWindow groupPop;
private int[] location = new int[2];// QuickLocExpandableLisView所在屏幕位置坐标
@Override
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
}
private View _groupLayout;
public int _groupIndex = -1;
public QuickLocExpandableLisView(Context context) {
super(context);
this.context = context;
init();
}
public QuickLocExpandableLisView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public QuickLocExpandableLisView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
}
private void init() {
super.setOnScrollListener(this);
groupPop = new PopupWindow(context);
groupPop.setBackgroundDrawable(new BitmapDrawable());
groupPop.setWidth(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
groupPop.setHeight(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
groupPop.setOutsideTouchable(false);
}
private void getLocation() {
this.getLocationInWindow(location);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
getLocation();
int ptp = view.pointToPosition(0, 0);
if (ptp != AdapterView.INVALID_POSITION) {
QuickLocExpandableLisView qExlist = (QuickLocExpandableLisView) view;
long pos = qExlist.getExpandableListPosition(ptp);
int groupPos = ExpandableListView.getPackedPositionGroup(pos);
int childPos = ExpandableListView.getPackedPositionChild(pos);
if (childPos < 0) {
groupPos = -1;
}
if (groupPos < _groupIndex) {
_groupIndex = groupPos;
if (groupPop.isShowing()) {
groupPop.dismiss();
}
if (_groupIndex != -1 && !groupPop.isShowing()) {// 此情况出现在点击_groupLayout收缩时
_groupLayout = updateGroupPop(_groupIndex);
}
} else if (groupPos > _groupIndex) {
_groupIndex = groupPos;
_groupLayout = updateGroupPop(_groupIndex);
}
}
}
private View updateGroupPop(final int groupPos) {
View groupLayout = (View) getExpandableListAdapter().getGroupView(
groupPos, true, null, null);
groupLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
collapseGroup(groupPos);
}
});
groupPop.dismiss();
groupPop.setContentView(groupLayout);
groupPop.showAtLocation((View) getParent(), Gravity.TOP, location[0],
location[1]);
return groupLayout;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
第二个适配器文件 :
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class IdeasExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private String[] groups = { "家人", "朋友", "同事", "同事1", "同事2" };
private String[] familis = { "老爸", "老妈", "妹妹"};
private String[] friends = { "小李", "张三", "李四" };
private String[] colleagues = { "陈总", "李工", "李客户"};
private String[] colleagues1 = { "陈总", "李工", "李客户"};
private String[] colleagues2 = { "陈总", "李工", "李客户"};
private List<String> groupList = null;
private List<List<String>> itemList = null;
public IdeasExpandableListAdapter(Context context) {
this.context = context;
groupList = new ArrayList<String>();
itemList = new ArrayList<List<String>>();
initData();
}
private void initData() {
for (int i = 0; i < groups.length; i++) {
groupList.add(groups[i]);
}
List<String> item1 = new ArrayList<String>();
for (int i = 0; i < familis.length; i++) {
item1.add(familis[i]);
}
List<String> item2 = new ArrayList<String>();
for (int i = 0; i < friends.length; i++) {
item2.add(friends[i]);
}
List<String> item3 = new ArrayList<String>();
for (int i = 0; i < colleagues.length; i++) {
item3.add(colleagues[i]);
}
List<String> item4 = new ArrayList<String>();
for (int i = 0; i < colleagues1.length; i++) {
item4.add(colleagues1[i]); }
List<String> item5 = new ArrayList<String>();
for (int i = 0; i < colleagues2.length; i++) {
item5.add(colleagues2[i]);}
itemList.add(item1);
itemList.add(item2);
itemList.add(item3);
itemList.add(item4);
itemList.add(item5);}
@Override
public Object getChild(int groupPosition, int childPosition) {
return itemList.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView text = null;
if (convertView == null) {
text = new TextView(context);
} else {
text = (TextView) convertView;
}
// 获取子节点要显示的名称
String name = (String) itemList.get(groupPosition).get(childPosition);
// 设置文本视图的相关属性
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 40);
text.setLayoutParams(lp);
text.setTextSize(18);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(77, 0, 0, 0);
text.setText(name);
return text;}
@Override
public int getChildrenCount(int groupPosition) {
return itemList.get(groupPosition).size();
}
@Override
public int getGroupTypeCount() {
return 5;
}
@Override
public int getGroupType(int groupPosition) {
return groupPosition;
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView text = null;
if (convertView == null) {
text = new TextView(context);
} else {
text = (TextView) convertView;
}
String name = (String) groupList.get(groupPosition);
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 40);
text.setLayoutParams(lp);
text.setBackgroundColor(Color.BLACK);
text.setTextSize(18);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(56, 0, 0, 0);
text.setText(name);
return text;}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
}
第三个activity:
public class ExpandableActivity extends Activity {
private QuickLocExpandableLisView eListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eListView = (QuickLocExpandableLisView) findViewById(R.id.expandableListView);
eListView.setAdapter(new IdeasExpandableListAdapter(this));
int groupCount = eListView.getCount();
eListView.setGroupIndicator(null);
for (int i = 0; i < groupCount; i++) {
eListView.expandGroup(i);
}
}}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.ExpandableListActivity.QuickLocExpandableLisView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.ExpandableListActivity.QuickLocExpandableLisView>
</RelativeLayout>