android仿qq二级列表,android中仿QQ列表的显示形式

public class MainActivity extends Activity implements

OnScrollListener {

private ExpandableListView

expandableListView_main_listinfo;

private List> childList = new ArrayList>();

private List parentList = new ArrayList();

private int[] icons = { R.drawable.h1, R.drawable.h2,

R.drawable.h3,

R.drawable.h4, R.drawable.h5, R.drawable.h1,

R.drawable.h2,

R.drawable.h3, R.drawable.h4, R.drawable.h5 };

private String names[] = { "kali", "suli", "Lily", "Amy",

"susan", "kali",

"suli", "Lily", "Amy", "susan" };

private int positionTop = -1;//

group和child联排的时候的位置:联排即是视group和child同一级(item),然后给他们position,

private long posTop;//

ExpandableListView静态类打包了group和child位置的信息

private int groupPositionTop = -1;// 从打包中取出来的group的位置

private int childPositionTop = -1;// 从打包中取出来的child的位置

private int positionNext = -1;

private long posNext;

private int groupPositionNext = -1;

private int childPositionNext = -1;

private LinearLayout floatLoayout;

private TextView text_main_groupname;

private MarginLayoutParams lp;

@Override

public void onScroll(AbsListView view, int

firstVisibleItem,

int visibleItemCount, int totalItemCount) {

getExpandableListviewPosition();

if (firstVisibleItem == 0) {//

防止listView已经到数据最顶端时继续下拉产生悬浮View和Group同时出现的问题

floatLoayout.setVisibility(View.GONE);

}

if (groupPositionTop !=

ExpandableListView.INVALID_POSITION

&& childPositionTop ==

ExpandableListView.INVALID_POSITION) {// 第一个可见项是group

if (expandableListView_main_listinfo

.isGroupExpanded(groupPositionTop)) {

lp = (MarginLayoutParams)

floatLoayout.getLayoutParams();

text_main_groupname.setText(parentList.get(groupPositionTop));

floatLoayout.setVisibility(View.VISIBLE);

lp.topMargin = 0;// 把悬浮框拉回来

floatLoayout.setLayoutParams(lp);// 设置的是悬浮框在屏幕上的位置

floatLoayout.setVisibility(View.VISIBLE);

text_main_groupname.setText(parentList.get(groupPositionTop));

} else {

floatLoayout.setVisibility(View.GONE);

}

} else if (groupPositionTop !=

ExpandableListView.INVALID_POSITION

&& childPositionTop !=

ExpandableListView.INVALID_POSITION) {//

第一个可见项是child就要把悬浮框固定在顶端不动

lp = (MarginLayoutParams)

floatLoayout.getLayoutParams();

text_main_groupname.setText(parentList.get(groupPositionTop));

floatLoayout.setVisibility(View.VISIBLE);

lp.topMargin = 0;// 把悬浮框拉回来

floatLoayout.setLayoutParams(lp);// 设置的是悬浮框在屏幕上的位置

getExpandableListviewNext();

// 判断下一个点是在group中还是child中

if (groupPositionNext !=

ExpandableListView.INVALID_POSITION

&& childPositionNext ==

ExpandableListView.INVALID_POSITION) {// 在group的情况下

lp.topMargin = -(floatLoayout.getHeight() -

expandableListView_main_listinfo

.getChildAt(1).getTop());// 把悬浮框推出去

floatLoayout.setLayoutParams(lp);

}

}

}

public void getExpandableListviewPosition() {

// 定义初始group和child联排的时候的位置

positionTop =

expandableListView_main_listinfo.pointToPosition(0, 0);

//

判断是否是无效信息,即当前得头位置是group还是child,group是无效的位置它没有第二个值(两个值分别是group的位置值和child的位置值)

if (positionTop != ExpandableListView.INVALID_POSITION) {//

INVALID_POSITION是-1意味着还没有在屏幕上定点

// 打包了group和child位置的信息

posTop = expandableListView_main_listinfo

.getExpandableListPosition(positionTop);//

获取最顶端的ListView的item的编号,次编号与group和child无关,是连排后的标号

groupPositionTop = ExpandableListView

.getPackedPositionGroup(posTop);// 获取item的group位置

childPositionTop = ExpandableListView

.getPackedPositionChild(posTop);// 获取item的child的位置

}

}

public void getExpandableListviewNext() {

int y =

expandableListView_main_listinfo.getChildAt(1).getHeight();//

// 1的位置不会错,0的位置会错

// int y = floatLoayout.getHeight();

// int y =

expandableListView_main_listinfo.getChildAt(1).getTop();

positionNext =

expandableListView_main_listinfo.pointToPosition(0, y);

if (positionNext != ExpandableListView.INVALID_POSITION)

{

posNext = expandableListView_main_listinfo

.getExpandableListPosition(positionNext);

groupPositionNext = ExpandableListView

.getPackedPositionGroup(posNext);

childPositionNext = ExpandableListView

.getPackedPositionChild(posNext);

}

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

expandableListView_main_listinfo = (ExpandableListView)

findViewById(R.id.expandableListView_main_listinfo);

floatLoayout = (LinearLayout)

findViewById(R.id.floatLayout);

text_main_groupname = (TextView)

findViewById(R.id.text_main_groupname);

initData();

expandableListView_main_listinfo

.setAdapter(new MyExpandableListAdapter(childList,

parentList));

expandableListView_main_listinfo

.setOnChildClickListener(new OnChildClickListener() {

@Override

public boolean onChildClick(ExpandableListView parent,

View v, int groupPosition, int childPosition,

long id) {

Toast.makeText(

MainActivity.this,

childList.get(groupPosition).get(childPosition)

.getUserName(), Toast.LENGTH_SHORT)

.show();

return true;

}

});

expandableListView_main_listinfo.setOnScrollListener(this);

floatLoayout.setClickable(true);

floatLoayout.setFocusable(true);

floatLoayout.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

floatLoayout.setVisibility(View.GONE);

expandableListView_main_listinfo

.collapseGroup(groupPositionTop);

}

});

}

class MyExpandableListAdapter extends

BaseExpandableListAdapter {

private List> childList;

private List parentList;

public MyExpandableListAdapter(List> childList,

List parentList) {

this.childList = childList;

this.parentList = parentList;

}

@Override

public int getGroupCount() {

return parentList.size();

}

@Override

public int getChildrenCount(int groupPosition) {

return childList.get(groupPosition).size();

}

@Override

public Object getGroup(int groupPosition) {

return parentList.get(groupPosition);

}

@Override

public Object getChild(int groupPosition, int childPosition)

{

return childList.get(groupPosition).get(childPosition);

}

@Override

public long getGroupId(int groupPosition) {

return groupPosition;

}

@Override

public long getChildId(int groupPosition, int childPosition)

{

return childPosition;

}

@Override

public boolean hasStableIds() {

return true;

}

@Override

public View getGroupView(int groupPosition, boolean

isExpanded,

View convertView, ViewGroup parent) {

ViewHolder mHolder = null;

if (convertView == null) {

mHolder = new ViewHolder();

convertView = getLayoutInflater().inflate(

R.layout.parent_expandablelv_main, null);

mHolder.text_parent_groupname = (TextView) convertView

.findViewById(R.id.text_parent_groupname);

convertView.setTag(mHolder);

} else {

mHolder = (ViewHolder) convertView.getTag();

}

mHolder.text_parent_groupname

.setText(parentList.get(groupPosition));

return convertView;

}

@Override

public View getChildView(int groupPosition, int

childPosition,

boolean isLastChild, View convertView, ViewGroup parent)

{

ViewHolder mHolder = null;

if (convertView == null) {

mHolder = new ViewHolder();

convertView = getLayoutInflater().inflate(

R.layout.child_expandablelv_main, null);

mHolder.text_child_username = (TextView) convertView

.findViewById(R.id.text_child_username);

mHolder.imageView_child_icon = (ImageView) convertView

.findViewById(R.id.imageView_child_icon);

convertView.setTag(mHolder);

} else {

mHolder = (ViewHolder) convertView.getTag();

}

mHolder.text_child_username.setText(childList.get(groupPosition)

.get(childPosition).getUserName());

mHolder.imageView_child_icon.setImageResource(childList

.get(groupPosition).get(childPosition).getIcon());

return convertView;

}

class ViewHolder {

private ImageView imageView_child_icon;

private TextView text_child_username;

private TextView text_parent_groupname;

}

@Override

public boolean isChildSelectable(int groupPosition, int

childPosition) {

return true;

}

}

private void initData() {

List users = null;

QQUser user = null;

for (int i = 0; i < 16; i++) {

users = new ArrayList();

for (int j = 0; j < 10; j++) {

user = new QQUser(names[j], icons[j]);

users.add(user);

}

childList.add(users);

parentList.add("分组" + i);

}

}

@Override

public void onScrollStateChanged(AbsListView view, int

scrollState) {

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值