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

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

android实现仿照QQ好友列表滑动效果_第1张图片android实现仿照QQ好友列表滑动效果_第2张图片

关键代码如下:

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 LinearLayout			ll_top;
	private int					the_group_expand_position	= -1;
	private int					indicatorGroupHeight;
	private boolean				isExpanding					= false;

	private TextView			tv_citylist_province;
	private ExpandableListView	expandableListView;

	private Context				context;

	@Override
	protected 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() {
			@Override
			public 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() {
			@Override
			public 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() {
			@Override
			public 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() {

			@Override
			public 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() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {}

			@Override
			public 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;
	}
}
布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ExpandableListView
        android:id="@+id/el_expandableListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ExpandableListView>

    <LinearLayout
        android:id="@+id/ll_citylist_top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/city_province_bg" >

        <TextView
            android:id="@+id/tv_citylist_province"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="5dp" />
    </LinearLayout>

</FrameLayout>

声明:参考了eoe上一个哥们的博客,具体的地址忘记了。

需要工程可以留言联系我。

马上元旦放假:顺道祝大家元旦快乐!游玩注意安全。

你可能感兴趣的:(android实现仿照QQ好友列表滑动效果)