Skip to content

Commit ba6b186

Browse files
committed
lastView 为空问题。
1 parent 9a2b0f2 commit ba6b186

File tree

8 files changed

+29
-15
lines changed

8 files changed

+29
-15
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66

77
defaultConfig {
88
applicationId "evilbinary.org.tvwidget"
9-
minSdkVersion 19
10-
targetSdkVersion 19
9+
minSdkVersion 14
10+
targetSdkVersion 14
1111
versionCode 1
1212
versionName "1.0"
1313
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="org.evilbinary.tv">
44

55
<application
6+
android:screenOrientation="sensor"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"

app/src/main/java/org/evilbinary/tv/DemoGridViewActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
2424

2525
GridView gridView = (GridView) findViewById(R.id.gridView);
2626

27-
gridView.getColumnWidth();
28-
gridView.getVerticalSpacing();
27+
2928

3029
MyGridViewAdapter myAdapter = new MyGridViewAdapter(R.layout.item_grid);
3130
gridView.setAdapter(myAdapter);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
4+
<stroke android:color="@color/greenyellow" android:width="4dp" android:dashWidth="4dp" android:dashGap="4dp"/>
5+
<corners android:radius="4dp"/>
6+
<solid android:color="#2287CEFA" />
7+
</shape>

app/src/main/res/layout/custom_item.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
android:layout_gravity="center"
66
android:layout_margin="12dp"
77
android:focusable="true"
8-
android:background="@drawable/border_shape"
9-
android:gravity="center">
8+
9+
android:background="@drawable/border_shape_with_border"
10+
>
1011

1112
<TextView
1213
android:id="@+id/textView"
1314
android:layout_width="match_parent"
1415
android:layout_height="match_parent"
1516
android:layout_alignParentStart="true"
1617
android:layout_alignParentTop="true"
17-
android:layout_gravity="center"
18+
android:layout_gravity="top"
1819
android:focusable="false"
1920
android:gravity="center"
2021
android:text="自定义border样式"
2122
android:textAlignment="center"
22-
android:textColor="@color/white"
23+
android:textColor="@color/coral"
2324
android:textSize="20sp" />
2425

2526
</RelativeLayout>

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android {
55
buildToolsVersion "23.0.2"
66

77
defaultConfig {
8-
minSdkVersion 19
8+
minSdkVersion 14
99
targetSdkVersion 19
1010
versionCode 1
1111
versionName "1.0"

lib/src/main/java/org/evilbinary/tv/widget/BorderEffect.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public void onFocusChanged(View oldFocus, View newFocus) {
9393
}
9494
};
9595

96-
private List<AdapterView.OnItemSelectedListener> onItemSelectedListenerList = new ArrayList<>();
9796

9897
public FocusListener absListViewFocusListener = new FocusListener() {
98+
private List<AdapterView.OnItemSelectedListener> onItemSelectedListenerList = new ArrayList<>();
9999
@Override
100100
public void onFocusChanged(View oldFocus, View newFocus) {
101101
for (View attachView : attacheViews) {
@@ -116,14 +116,16 @@ public void onFocusChanged(View oldFocus, View newFocus) {
116116
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
117117
newFocus = view;
118118

119+
newFocus.bringToFront();
119120

120121
int w = view.getMeasuredWidth();
121122
int h = view.getMeasuredHeight();
122-
int ww = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY);
123-
int hh = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY);
124-
view.measure(ww,hh);
123+
int ww = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.AT_MOST);
124+
int hh = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.AT_MOST);
125+
view.measure(ww, hh);
125126

126-
Log.d(TAG, "onItemSelected:" + newFocus.getMeasuredWidth()+" w:"+w );
127+
ViewGroup vg= (ViewGroup) newFocus.getParent();
128+
Log.d(TAG, "onItemSelected:" +vg.getWidth()+" "+ newFocus.getMeasuredWidth()+" w:"+w );
127129

128130

129131
if (onItemSelectedListener != null) {

lib/src/main/java/org/evilbinary/tv/widget/BorderView.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class BorderView<X extends View> implements ViewTreeObserver.OnGlobalFocu
2525
private Effect borderEffect;
2626

2727
private X mView;
28+
private View mLastView;
2829

2930
public interface Effect {
3031
public void onFocusChanged(View target, View oldFocus, View newFocus);
@@ -93,9 +94,12 @@ public void onTouchModeChanged(boolean isInTouchMode) {
9394

9495
@Override
9596
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
96-
97+
if(oldFocus==null){
98+
oldFocus=mLastView;
99+
}
97100
if (borderEffect != null)
98101
borderEffect.onFocusChanged(mView, oldFocus, newFocus);
102+
mLastView=newFocus;
99103

100104
}
101105

0 commit comments

Comments
 (0)