本文实例为大家分享了Android仿QQ可拉伸头部控件的具体实现代码,供大家参考,具体内容如下
该控件大致思路:
1.采用继承listview加入头部view。
2.监听listview滚动。
3.自定义动画回弹。
先看效果吧:
activity-main.xml布局如下:
<LinearLayout xmlns:andro xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" > <com.example.headerlistview.HeaderListView android: android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" android:divider="@android:color/darker_gray" android:dividerHeight="1dip" android:duplicateParentState="true" android:scrollbars="none" > </com.example.headerlistview.HeaderListView> </LinearLayout>
headerview.xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android: android:layout_width="match_parent" android:layout_height="150dip" android:scaleType="centerCrop" android:src="@drawable/lifei987" /> </LinearLayout>
list_item布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="80dip" android:gravity="center_vertical" android:orientation="horizontal" > " <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="80dip" android:layout_marginLeft="10dip" android:orientation="vertical" > <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="lifei" /> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="lifeiasdfasdfasfsadfasf" /> </LinearLayout> </LinearLayout>
activity代码:
package com.example.headerlistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private HeaderListView header_lv; private ImageView header_iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getHeaderView(); initView(); } private void initView() { // TODO Auto-generated method stub header_lv=(HeaderListView) findViewById(R.id.header_lv); header_lv.addHeaderView(getHeaderView()); header_lv.setHeaderView(header_iv); header_lv.setAdapter(getSimpleAdapter()); } public BaseAdapter getSimpleAdapter(){ List<Map<String, Object>> data=new ArrayList<Map<String,Object>>(); for(int i=0;i<15;i++){ Map<String, Object> map=new HashMap<String, Object>(); map.put("name", "郑州___"+i); map.put("describe", "asdfasdfasdfasdfasdfsadfsad"); data.add(map); } SimpleAdapter simpleAdapter=new SimpleAdapter(this, data, R.layout.list_item, new String[]{"name","describe"}, new int[]{R.id.tv_name,R.id.tv_describe}); return simpleAdapter; } public View getHeaderView(){ View view= getLayoutInflater().inflate(R.layout.headerview, null); header_iv =(ImageView) view.findViewById(R.id.header_image); return view; } }