android ListView下拉刷新之头布局的完成
2015-12-18 11:08
405 查看
首先要在listview添加一个头布局,
然后进行隐藏,
再进行touch监听,显示头布局
然后进行隐藏,
再进行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); } }
相关文章推荐
- Android 通过点击EditText之外的地方隐藏软键盘
- Android计时器项目
- android之ViewFlipper实现左右滑动动画效果
- android:scaleType的各个属性解释
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- Android Design Support Library 中控件的使用简单介绍(一)
- html5高价值的android屏幕适配精华教程(HTML5+CSS3)!
- 转,android Selector
- Android学习之 使用依赖注入函数库Roboguice
- android圆形图片,圆形背景文字的CircleTextImageView开源组件
- Android实现播放GIF动画的强大ImageView
- 性能优化系列第三篇--Java(Android)代码优化
- 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
- Android push apk - bat
- Android下利用SharePreference存储序列化对象的方法
- [Android] Activity configChanges属性
- 使用 ant 编译 android 工程
- Android实现GIF图片解码与播放
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- android键值传递