SmoothGridView.java
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.xgimi.gimicinema.view;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.GridView;
/**
* 重写GridView使其滑动时没有那么生硬,适用于盒子项目
*
* @author 水手uuu
* @user 李攀 sissilove812[at]gmail[dot]com
* @date 2014-12-04
*/
public class SmoothGridView extends GridView {
private final static int SCROLL_ITEM_TIME = 200;//may not important
private int eventCount = 0;
private final static int DOUBLE_ROW = 2; //double row
private final static int SINGLE_ROW = 1; //single row
private int scrollRow = 1;//set the scroll rows if needed default is 1
private int position = 0;
public SmoothGridView(Context context) {
this(context, null);
}
public SmoothGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setChildrenDrawingOrderEnabled(false);
}
public SmoothGridView(Context context, AttributeSet attrs) {
this(context,attrs,0);
}
// @Override
// public boolean dispatchKeyEvent(KeyEvent event) {
// int height = this.getChildAt(0).getHeight();
// eventCount++;
// // for different item it may occurs twice
// if (eventCount % 2 != 0) {
// int row = 0;
// row = getItemCurrentRow();
// if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN && row == DOUBLE_ROW) {
// this.smoothScrollBy(height, SCROLL_ITEM_TIME);
// invalidate();
// } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP && row == SINGLE_ROW) {
// this.smoothScrollBy(-height, SCROLL_ITEM_TIME);
// invalidate();
// }
// }
// return super.dispatchKeyEvent(event);
// }
/**
*
* get GridView's item location
*
* @return
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int getItemCurrentRow() {
int position = 0;
int row = 0;
position = this.getSelectedItemPosition();
int temp = (position / this.getNumColumns() + 1) % 2;
if (temp == 0) {
row = DOUBLE_ROW;
} else {
row = SINGLE_ROW;
}
return row;
}
public void setCurrentPosition(int pos) {
this.position = pos - getFirstVisiblePosition();
}
@SuppressLint("NewApi")
@Override
protected void setChildrenDrawingOrderEnabled(boolean enabled) {
super.setChildrenDrawingOrderEnabled(enabled);
}
@Override
protected int getChildDrawingOrder(int childCount, int i) {
int sort = 0;
if (i == childCount - 1) {
sort = position;
}else
if (i == position) {
sort = childCount - 1;
}else{
sort = i;
}
Log.d("smooth grid view sort","sort:"+i);
return sort;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction,
Rect previouslyFocusedRect) {
int lastSelectItem = getSelectedItemPosition();
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (gainFocus) {
setSelection(lastSelectItem);
}
invalidate();
}
}