首先要在listview添加一个头布局,
然后进行隐藏,
再进行touch监听,显示头布局
package com.example.listviewf5; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ListView; public class MyListView extends ListView { private View v; private int headHeight;// 头部的高度 private int downY;// 按下时候Y坐标 public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub initView(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub initView(context); } public MyListView(Context context) { super(context); // TODO Auto-generated constructor stub initView(context); } /** * 加载顶部布局文件 * * @param context */ private void initView(Context context) { // LayoutInflater in =LayoutInflater.from(context); // v = in.inflate(R.layout.head,null); v = View.inflate(context, R.layout.head, null); this.addHeaderView(v); v.measure(0, 0);// 通知系统测量宽高 headHeight = v.getMeasuredHeight();// 得到测量后的高度 v.setPadding(0, -headHeight, 0, 0);// 进行隐藏head,就是把paddingtop设置成负高度 } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downY = (int) ev.getY(); break; case MotionEvent.ACTION_MOVE: int deltaY = (int) (ev.getY() - downY);//得到移动的距离 int currentHeight = -headHeight + deltaY; //判断当前的距离是不是大于headHeight 并且显示的是第一个位置 if(currentHeight>-headHeight && getFirstVisiblePosition()==0){ v.setPadding(0, currentHeight, 0, 0);//展现头部 return true;//拦截事件不让listview处理 } break; case MotionEvent.ACTION_UP: break; } return super.onTouchEvent(ev); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:paddingTop="10dp" android:layout_gravity="center_horizontal" > <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/head_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下拉可以刷新" /> <TextView android:id="@+id/head_tvtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上次刷新时间" /> </LinearLayout> <ImageView android:id="@+id/head_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_toLeftOf="@+id/ll" android:src="@drawable/ic_launcher" /> <ProgressBar android:id="@+id/pb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_toLeftOf="@+id/ll" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.listviewf5.MyListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"/> </RelativeLayout>
package com.example.listviewf5; 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.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private MyListView lv; private List<Map<String, Object>> list; private SimpleAdapter sa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (MyListView) findViewById(R.id.lv); list = new ArrayList<Map<String,Object>>(); for (int i = 0; i <20; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("key1","神马都是浮云"); map.put("key2","heheda"); list.add(map); } sa = new SimpleAdapter(this, list,R.layout.item_lv, new String[]{"key1","key2"},new int[]{R.id.tv,R.id.tv2}); lv.setAdapter(sa); } }