Android GridView 添加 HeadView

1package com.example.test; 2 3import android.content.Context; 4import android.util.AttributeSet; 5import android.util.Log; 6import android.view.MotionEvent; 7import android.view.ViewConfiguration; 8import android.widget.GridView; 9import android.widget.LinearLayout; 10 11public class HeadGridView extends LinearLayout{ 12 private static final String TAG = HeadGridView.class.getSimpleName(); 13 private LinearLayout mHeadLayout; 14 private GridView mGridView; 15 private int mHeadHeight; 16 private boolean mIsBeingDragged; 17 private float mLastMotionY; 18 private float mTouchSlop; 19 private boolean mGridViewFocused = true; 20 private State mState = State.getDefaultMode(); 21 22 public HeadGridView(Context context) { 23 this(context, null); 24 } 25 26 public HeadGridView(Context context, AttributeSet attrs) { 27 super(context, attrs); 28 initContentView(context); 29 } 30 31 private void initContentView(Context context) { 32 ViewConfiguration config = ViewConfiguration.get(context); 33 mTouchSlop = config.getScaledTouchSlop(); 34 setOrientation(VERTICAL); 35 GridView gridView = new GridView(context); 36 LayoutParams gridParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 37 gridView.setNumColumns(2); 38 gridView.setLayoutParams(gridParams); 39 mGridView = gridView; 40 initGridView(gridView); 41 addView(gridView); 42 43 LinearLayout headLayout = new LinearLayout(context); 44 LayoutParams headParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 45 headLayout.setLayoutParams(headParams); 46 mHeadLayout = headLayout; 47 initHead(headLayout); 48 addView(headLayout,0); 49 } 50 51 @Override 52 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 53 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 54 int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 55 int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 56 int gridWidthSpec = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY); 57 int gridHeightSpec = MeasureSpec.makeMeasureSpec(parentHeight, MeasureSpec.EXACTLY); 58 mGridView.measure(gridWidthSpec, gridHeightSpec); 59 setMeasuredDimension(parentWidth, parentHeight); 60 } 61 62 @Override 63 protected void onLayout(boolean changed, int l, int t, int r, int b) { 64 super.onLayout(changed, l, t, r, b); 65 mHeadHeight = getChildAt(0).getMeasuredHeight(); 66 } 67 68 protected void initGridView(GridView gridView) { 69 70 } 71 72 protected void initHead(LinearLayout headLayout) { 73 74 } 75 76 @Override 77 public final boolean onInterceptTouchEvent(MotionEvent event) { 78 Log.d(TAG, "currentState = "+mState+" event = "+event.getAction()+" mIsBeingDragged = "+mIsBeingDragged); 79 final int action = event.getAction(); 80 81 if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { 82 mIsBeingDragged = false; 83 return false; 84 } 85 86 if (mIsBeingDragged) { 87 return true; 88 } 89 90 switch (action) { 91 case MotionEvent.ACTION_MOVE: 92 final float y = event.getY(); 93 final float absDiff = Math.abs(- mLastMotionY);; 94 95 if (absDiff > mTouchSlop) { 96 mLastMotionY = y; 97 mIsBeingDragged = true; 98 if (mState != State.Head_Visible) { 99 mGridViewFocused = false; 100 } 101 } 102 break; 103 case MotionEvent.ACTION_DOWN: 104 mLastMotionY = event.getY(); 105 mIsBeingDragged = false; 106 break; 107 default: 108 break; 109 } 110 return mIsBeingDragged; 111 } 112 113 @Override 114 public boolean onTouchEvent(MotionEvent event) { 115 116 if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) { 117 return false; 118 } 119 120 switch (event.getAction()) { 121 case MotionEvent.ACTION_MOVE: { 122 if (mIsBeingDragged) { 123 pullEvent(event); 124 return true; 125 } 126 break; 127 } 128 case MotionEvent.ACTION_DOWN:  129 mLastMotionY = event.getY(); 130 return true; 131 case MotionEvent.ACTION_CANCEL: 132 case MotionEvent.ACTION_UP:  133 if (mIsBeingDragged) { 134 mIsBeingDragged = false; 135 } 136 return true; 137 } 138 return false; 139 } 140 141 private void dispatchDownToGridView(MotionEvent e, float downY) { 142 int action = e.getAction(); 143 MotionEvent de = MotionEvent.obtain(e); 144 de.setLocation(de.getX(), downY); 145 de.setAction(MotionEvent.ACTION_DOWN); 146 mGridView.dispatchTouchEvent(de); 147 e.setAction(action); 148 } 149 150 private void pullEvent(MotionEvent e) { 151 if (!mGridViewFocused) { 152 dispatchDownToGridView(e, mLastMotionY); 153 mGridViewFocused = true; 154 } 155 156 float y = e.getY(); 157 int scrollValue = Math.round(mLastMotionY - y); 158 if (mState == State.Bottom_Top) { 159 if (scrollValue > 0){ 160 if (mState == State.Bottom_Top) { 161 dispatchDownToGridView(e, mLastMotionY); 162 } 163 mGridView.dispatchTouchEvent(e); 164 mState = State.Bottom_Medium; 165 } else { 166 mState = State.Head_Visible; 167 } 168 } 169 170 if (mState.canHeadViewScroll()) { 171 int currentScrollY = getScrollY(); 172 int maxScroolY = mHeadHeight - currentScrollY; 173 if (scrollValue > 0) { //向上滑动 174 if (maxScroolY < scrollValue) { 175 scrollValue = maxScroolY; 176 mState = State.Bottom_Top; 177 } 178 scrollBy(0, scrollValue); 179 } else { //向下滑动 180 if (currentScrollY > 0) { 181 if (currentScrollY < - scrollValue) { 182 scrollValue = - currentScrollY; 183 } 184 scrollBy(0, scrollValue); 185 } 186 } 187 } else { 188 mGridView.dispatchTouchEvent(e); 189 if (scrollValue < 0) { 190 int top = mGridView.getChildAt(0).getTop(); 191 if (top == 0) { 192 mState = State.Bottom_Top; 193 } 194 } 195 } 196 mLastMotionY = y; 197 } 198 199 public static enum State{ 200 Head_Visible(0x01), 201 Bottom_Top(0x02), 202 Bottom_Bottom(0x03), 203 Bottom_Medium(0x04); 204 205 private int id; 206 207 private State(int id) { 208 this.id = id; 209 } 210 211 public int getId(){ 212 return id; 213 } 214 215 public static State getDefaultMode(){ 216 return State.Head_Visible; 217 } 218 219 static State mapState(final int id) { 220 for (State value : State.values()) { 221 if (id == value.getId()) { 222 return value; 223 } 224 } 225 return getDefaultMode(); 226 } 227 228 boolean canHeadViewScroll() { 229 return this == Head_Visible || this == Bottom_Top; 230 } 231 } 232}

简单实现,目前还有些许问题,后续可参考:https://android.googlesource.com/platform/packages/apps/Gallery2/+/idea133/src/com/android/photos/views/HeaderGridView.java

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Android GridView 添加 HeadView - HelloWorld