| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
|
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.content.Context; |
| 22 |
import android.view.MotionEvent; |
| 23 |
import android.view.View; |
| 24 |
|
| 25 |
import com.android.internal.widget.AutoScrollHelper.AbsListViewAutoScroller; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class DropDownListView extends ListView { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private boolean mListSelectionHidden; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private boolean mHijackFocus; |
| 70 |
|
| 71 |
|
| 72 |
private boolean mDrawsInPressedState; |
| 73 |
|
| 74 |
|
| 75 |
private AbsListViewAutoScroller mScrollHelper; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private ResolveHoverRunnable mResolveHoverRunnable; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public DropDownListView(@NonNull Context context, boolean hijackFocus) { |
| 89 |
this(context, hijackFocus, com.android.internal.R.attr.dropDownListViewStyle); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public DropDownListView(@NonNull Context context, boolean hijackFocus, int defStyleAttr) { |
| 98 |
super(context, null, defStyleAttr); |
| 99 |
mHijackFocus = hijackFocus; |
| 100 |
|
| 101 |
setCacheColorHint(0); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
boolean shouldShowSelector() { |
| 106 |
return isHovered() || super.shouldShowSelector(); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public boolean onTouchEvent(MotionEvent ev) { |
| 111 |
if (mResolveHoverRunnable != null) { |
| 112 |
|
| 113 |
mResolveHoverRunnable.cancel(); |
| 114 |
} |
| 115 |
|
| 116 |
return super.onTouchEvent(ev); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public boolean onHoverEvent(@NonNull MotionEvent ev) { |
| 121 |
final int action = ev.getActionMasked(); |
| 122 |
if (action == MotionEvent.ACTION_HOVER_EXIT && mResolveHoverRunnable == null) { |
| 123 |
|
| 124 |
|
| 125 |
mResolveHoverRunnable = new ResolveHoverRunnable(); |
| 126 |
mResolveHoverRunnable.post(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
final boolean handled = super.onHoverEvent(ev); |
| 131 |
|
| 132 |
if (action == MotionEvent.ACTION_HOVER_ENTER |
| 133 |
|| action == MotionEvent.ACTION_HOVER_MOVE) { |
| 134 |
final int position = pointToPosition((int) ev.getX(), (int) ev.getY()); |
| 135 |
if (position != INVALID_POSITION && position != mSelectedPosition) { |
| 136 |
final View hoveredItem = getChildAt(position - getFirstVisiblePosition()); |
| 137 |
if (hoveredItem.isEnabled()) { |
| 138 |
|
| 139 |
|
| 140 |
requestFocus(); |
| 141 |
|
| 142 |
positionSelector(position, hoveredItem); |
| 143 |
setSelectedPositionInt(position); |
| 144 |
setNextSelectedPositionInt(position); |
| 145 |
} |
| 146 |
updateSelectorState(); |
| 147 |
} |
| 148 |
} else { |
| 149 |
|
| 150 |
|
| 151 |
if (!super.shouldShowSelector()) { |
| 152 |
setSelectedPositionInt(INVALID_POSITION); |
| 153 |
setNextSelectedPositionInt(INVALID_POSITION); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return handled; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
protected void drawableStateChanged() { |
| 162 |
if (mResolveHoverRunnable == null) { |
| 163 |
super.drawableStateChanged(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public boolean onForwardedEvent(@NonNull MotionEvent event, int activePointerId) { |
| 174 |
boolean handledEvent = true; |
| 175 |
boolean clearPressedItem = false; |
| 176 |
|
| 177 |
final int actionMasked = event.getActionMasked(); |
| 178 |
switch (actionMasked) { |
| 179 |
case MotionEvent.ACTION_CANCEL: |
| 180 |
handledEvent = false; |
| 181 |
break; |
| 182 |
case MotionEvent.ACTION_UP: |
| 183 |
handledEvent = false; |
| 184 |
|
| 185 |
case MotionEvent.ACTION_MOVE: |
| 186 |
final int activeIndex = event.findPointerIndex(activePointerId); |
| 187 |
if (activeIndex < 0) { |
| 188 |
handledEvent = false; |
| 189 |
break; |
| 190 |
} |
| 191 |
|
| 192 |
final int x = (int) event.getX(activeIndex); |
| 193 |
final int y = (int) event.getY(activeIndex); |
| 194 |
final int position = pointToPosition(x, y); |
| 195 |
if (position == INVALID_POSITION) { |
| 196 |
clearPressedItem = true; |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
final View child = getChildAt(position - getFirstVisiblePosition()); |
| 201 |
setPressedItem(child, position, x, y); |
| 202 |
handledEvent = true; |
| 203 |
|
| 204 |
if (actionMasked == MotionEvent.ACTION_UP) { |
| 205 |
final long id = getItemIdAtPosition(position); |
| 206 |
performItemClick(child, position, id); |
| 207 |
} |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
if (!handledEvent || clearPressedItem) { |
| 213 |
clearPressedItem(); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
if (handledEvent) { |
| 218 |
if (mScrollHelper == null) { |
| 219 |
mScrollHelper = new AbsListViewAutoScroller(this); |
| 220 |
} |
| 221 |
mScrollHelper.setEnabled(true); |
| 222 |
mScrollHelper.onTouch(this, event); |
| 223 |
} else if (mScrollHelper != null) { |
| 224 |
mScrollHelper.setEnabled(false); |
| 225 |
} |
| 226 |
|
| 227 |
return handledEvent; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public void setListSelectionHidden(boolean hideListSelection) { |
| 238 |
mListSelectionHidden = hideListSelection; |
| 239 |
} |
| 240 |
|
| 241 |
private void clearPressedItem() { |
| 242 |
mDrawsInPressedState = false; |
| 243 |
setPressed(false); |
| 244 |
updateSelectorState(); |
| 245 |
|
| 246 |
final View motionView = getChildAt(mMotionPosition - mFirstPosition); |
| 247 |
if (motionView != null) { |
| 248 |
motionView.setPressed(false); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
private void setPressedItem(@NonNull View child, int position, float x, float y) { |
| 253 |
mDrawsInPressedState = true; |
| 254 |
|
| 255 |
|
| 256 |
drawableHotspotChanged(x, y); |
| 257 |
if (!isPressed()) { |
| 258 |
setPressed(true); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
if (mDataChanged) { |
| 263 |
layoutChildren(); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
final View motionView = getChildAt(mMotionPosition - mFirstPosition); |
| 269 |
if (motionView != null && motionView != child && motionView.isPressed()) { |
| 270 |
motionView.setPressed(false); |
| 271 |
} |
| 272 |
mMotionPosition = position; |
| 273 |
|
| 274 |
|
| 275 |
final float childX = x - child.getLeft(); |
| 276 |
final float childY = y - child.getTop(); |
| 277 |
child.drawableHotspotChanged(childX, childY); |
| 278 |
if (!child.isPressed()) { |
| 279 |
child.setPressed(true); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
setSelectedPositionInt(position); |
| 284 |
positionSelectorLikeTouch(position, child, x, y); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
refreshDrawableState(); |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
boolean touchModeDrawsInPressedState() { |
| 293 |
return mDrawsInPressedState || super.touchModeDrawsInPressedState(); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@Override |
| 304 |
View obtainView(int position, boolean[] isScrap) { |
| 305 |
View view = super.obtainView(position, isScrap); |
| 306 |
|
| 307 |
if (view instanceof TextView) { |
| 308 |
((TextView) view).setHorizontallyScrolling(true); |
| 309 |
} |
| 310 |
|
| 311 |
return view; |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public boolean isInTouchMode() { |
| 316 |
|
| 317 |
return (mHijackFocus && mListSelectionHidden) || super.isInTouchMode(); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
@Override |
| 326 |
public boolean hasWindowFocus() { |
| 327 |
return mHijackFocus || super.hasWindowFocus(); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
@Override |
| 336 |
public boolean isFocused() { |
| 337 |
return mHijackFocus || super.isFocused(); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
@Override |
| 346 |
public boolean hasFocus() { |
| 347 |
return mHijackFocus || super.hasFocus(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
private class ResolveHoverRunnable implements Runnable { |
| 354 |
@Override |
| 355 |
public void run() { |
| 356 |
|
| 357 |
mResolveHoverRunnable = null; |
| 358 |
drawableStateChanged(); |
| 359 |
} |
| 360 |
|
| 361 |
public void cancel() { |
| 362 |
mResolveHoverRunnable = null; |
| 363 |
removeCallbacks(this); |
| 364 |
} |
| 365 |
|
| 366 |
public void post() { |
| 367 |
DropDownListView.this.post(this); |
| 368 |
} |
| 369 |
} |
| 370 |
} |