android实现仿照QQ好友列表滑动效果

实现效果如下:ExpandableListView滑动的时候,父视图停留在activity的最上方。

关键代码如下:

package com.telecom.video;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.MarginLayoutParams;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.AdapterView;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnGroupClickListener;import android.widget.ExpandableListView.OnGroupCollapseListener;import android.widget.ExpandableListView.OnGroupExpandListener;import android.widget.LinearLayout;import android.widget.TextView;import com.telecom.video.beans.CityBeans;import com.telecom.video.beans.Province;import com.telecom.video.view.adp.CityListViewAdapter;/** * 城市选择列表 * * */public class CityListActivity extends Activity {public LinearLayoutll_top;private intthe_group_expand_position= -1;private intindicatorGroupHeight;private booleanisExpanding= false;private TextViewtv_citylist_province;private ExpandableListViewexpandableListView;private Contextcontext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.city_list);context = CityListActivity.this;ll_top = (LinearLayout) findViewById(R.id.ll_citylist_top);ll_top.setVisibility(View.GONE);tv_citylist_province = (TextView) findViewById(R.id.tv_citylist_province);expandableListView = (ExpandableListView) findViewById(R.id.el_expandableListView);final List<Province> list = getLocalData();CityListViewAdapter adapter = new CityListViewAdapter(context, list);expandableListView.setAdapter(adapter);expandableListView.setGroupIndicator(null);// int groupCount = expandableListView.getCount();// for (int i = 0; i < groupCount; i++) {// expandableListView.expandGroup(i);// }expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {@Overridepublic void onGroupExpand(int groupPosition) {the_group_expand_position = groupPosition;ll_top.setVisibility(View.VISIBLE);// lineView.setVisibility(View.VISIBLE);isExpanding = true;if (the_group_expand_position != -1) {tv_citylist_province.setText(list.get(the_group_expand_position).getInitial() + " "+ list.get(the_group_expand_position).getProvince());}}});expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {@Overridepublic void onGroupCollapse(int groupPosition) {if (isExpanding) {ll_top.setVisibility(View.GONE);// lineView.setVisibility(View.GONE);}the_group_expand_position = -1;isExpanding = false;}});ll_top.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// linear.setVisibility(View.GONE);if (isExpanding) {ll_top.setVisibility(View.GONE);// lineView.setVisibility(View.GONE);expandableListView.collapseGroup(the_group_expand_position);isExpanding = false;}}});expandableListView.setOnGroupClickListener(new OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {if (the_group_expand_position == -1) {expandableListView.expandGroup(groupPosition);expandableListView.setSelectedGroup(groupPosition);the_group_expand_position = groupPosition;ll_top.setVisibility(View.VISIBLE);// lineView.setVisibility(View.VISIBLE);isExpanding = true;}else if (the_group_expand_position == groupPosition) {ll_top.setVisibility(View.GONE);expandableListView.collapseGroup(groupPosition);the_group_expand_position = -1;isExpanding = false;}else {expandableListView.collapseGroup(the_group_expand_position);expandableListView.expandGroup(groupPosition);expandableListView.setSelectedGroup(groupPosition);the_group_expand_position = groupPosition;}return true;}});expandableListView.setOnScrollListener(new OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {int npos = view.pointToPosition(0, 0);if (npos != AdapterView.INVALID_POSITION) {long pos = expandableListView.getExpandableListPosition(npos);int childPos = ExpandableListView.getPackedPositionChild(pos);int groupPos = ExpandableListView.getPackedPositionGroup(pos);if (childPos == AdapterView.INVALID_POSITION) {View groupView = expandableListView.getChildAt(npos- expandableListView.getFirstVisiblePosition());indicatorGroupHeight = groupView.getHeight();}if (indicatorGroupHeight == 0) {return;}if (isExpanding) {if (the_group_expand_position != -1) {tv_citylist_province.setText(list.get(the_group_expand_position).getInitial() + " "+ list.get(the_group_expand_position).getProvince());}if (the_group_expand_position != groupPos) {ll_top.setVisibility(View.GONE);}else {ll_top.setVisibility(View.VISIBLE);}}}if (the_group_expand_position == -1) {return;}int showHeight = t();MarginLayoutParams layoutParams = (MarginLayoutParams) ll_top.getLayoutParams();layoutParams.topMargin = -(indicatorGroupHeight – showHeight);}});}private int t() {int showHeight = indicatorGroupHeight;int nEndPos = expandableListView.pointToPosition(0, indicatorGroupHeight);if (nEndPos != AdapterView.INVALID_POSITION) {long pos = expandableListView.getExpandableListPosition(nEndPos);int groupPos = ExpandableListView.getPackedPositionGroup(pos);if (groupPos != the_group_expand_position) {View viewNext = expandableListView.getChildAt(nEndPos – expandableListView.getFirstVisiblePosition());showHeight = viewNext.getTop();}}return showHeight;}private List<Province> getLocalData() {try {String[] cityData = this.getResources().getStringArray(R.array.arrays_city);String[] provinceData = this.getResources().getStringArray(R.array.arrays_province);Map<String, List<CityBeans>> map = new HashMap<String, List<CityBeans>>();List<Province> list = new ArrayList<Province>();for (int i = 0; i < cityData.length; i++) {String item = cityData[i];String[] a = item.split("\\|");CityBeans city = new CityBeans(a[0], a[1], a[2]);if (map.containsKey(city.getProvince())) {List<CityBeans> citys = map.get(city.getProvince());citys.add(city);}else {List<CityBeans> citys = new ArrayList<CityBeans>();citys.add(city);map.put(city.getProvince(), citys);}}for (int i = 0; i < provinceData.length; i++) {String item = provinceData[i];String[] a = item.split("\\|");Province province = new Province();province.setInitial(a[0]);province.setProvince(a[1]);if (map.containsKey(province.getProvince())) {province.setList_citys(map.get(province.getProvince()));}list.add(province);}return list;} catch (Exception e) {e.printStackTrace();}return null;}}布局:

接受失败等于放松自己高压的心理,

android实现仿照QQ好友列表滑动效果

相关文章:

你感兴趣的文章:

标签云: