package org.evilbinary.tv.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * 作者:evilbinary on 2/2/16.
 * 邮箱:rootdebug@163.com
 */
public class TvZorderRelativeLayout extends RelativeLayout {
    private int position = 0;

    public TvZorderRelativeLayout(Context context) {
        super(context);

    }

    public TvZorderRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setChildrenDrawingOrderEnabled(true);
    }


    public void setCurrentPosition(int pos) {
        this.position = pos;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        View focused=findFocus();
        int pos = indexOfChild(focused);
        if (pos >= 0 && pos < getChildCount() ) {
            setCurrentPosition(pos);
            postInvalidate();
        }

        return super.dispatchKeyEvent(event);
    }

    @Override
    protected int getChildDrawingOrder(int childCount, int i) {

        if (i == childCount - 1) {//这是最后一个需要刷新的item
            return position;
        }
        if (i == position) {//这是原本要在最后一个刷新的item
            return childCount - 1;
        }
        return i;//正常次序的item
    }

}

@@ -60,16 +60,41 @@
 ontext;%0A
+import android.os.Build;%0A
 import a
@@ -764,25 +764,102 @@
 vent) %7B%0A
-%0A
+        if (Build.VERSION.SDK_INT %3C= Build.VERSION_CODES.JELLY_BEAN_MR2) %7B%0A   
         
 View foc
@@ -850,16 +850,17 @@
         
+ 
 View foc
@@ -863,17 +863,19 @@
  focused
-=
+ = 
 findFocu
@@ -875,24 +875,28 @@
 indFocus();%0A
+    
         int 
@@ -928,24 +928,28 @@
 d);%0A        
+ 
+   
 if (pos %3E= 0
@@ -973,17 +973,16 @@
 dCount()
- 
 ) %7B%0A    
@@ -989,16 +989,20 @@
         
+ 
+   
 setCurre
@@ -1030,16 +1030,20 @@
         
+ 
+   
 postInva
@@ -1048,24 +1048,38 @@
 validate();%0A
+            %7D%0A
         %7D%0A%0A 
@@ -1208,16 +1208,174 @@
 nt i) %7B%0A
+        View v = getFocusedChild();%0A        int pos = indexOfChild(v);%0A        if (pos %3E= 0 && pos %3C childCount)%0A            setCurrentPosition(pos);%0A        
 %0A       

修复android4.0以下bug,zorder 为空问题