2015-04-07 64 views
12

我正在使用RecyclerView和GridLayoutManager。我想達到的目標是當我點擊一個項目時,它會放大並重疊相鄰的項目。就像下面的圖片(在Android的電視) enter image description here在RecyclerView中放大項目以重疊2個相鄰項目Android

當觸發onClick事件,我呼籲

v.animate().scaleX(1.2f).scaleY(1.2f).setDuration(500).start(); 

但結果是以下: enter image description here

它可以只重疊的項目,比自己低。
我應該怎麼做才能重疊所有相鄰的物品。提前致謝。
編輯
我已經嘗試過:

v.bringToFront(); 

(v.getParent()).bringChildToFront(v); 

但他們都不起作用。

+0

你解決了這個問題嗎?我面臨同樣的問題,thx。 – Xiaozou

+0

使用'旋轉木馬'。這是最適合這個要求.. !! ..請參閱:http://www.codeproject.com/Articles/146145/Android-3D-Carousel –

回答

0

嘗試在運行動畫之前調用v.bringToFront()
如果它不起作用另一個變體是使用View#setElevation(float val)其中val> 0。缺點是,當您取消選擇該項目時,需要將高度設置回0。

+0

它不起作用。我已經嘗試過了。 –

+0

@HuyDuongTu我已經更新了我的答案。 – aga

+0

它的工作原理。但是View#setElevation(float val)需要API 21.我的應用程序還支持API 15.是否有其他解決方法。謝謝 –

-1

您需要按照以前使用普通ViewGroups進行的操作,覆蓋子級的繪製順序。這個例子強制第一個視圖總是最後繪製 - 您需要爲您的用例進行自定義。

public TopForcingRecyclerView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setChildrenDrawingOrderEnabled(true); 
} 

@Override protected int getChildDrawingOrder(int childCount, int i) { 
    if (i >= childCount - 1) return 0; 
    //when asked for the last view to draw, answer ZERO 
    else return i + 1; 
    //when asked for the i-th view to draw, answer "the next one" 
} 

注意關閉一個錯誤。

4

AGA的答案,我使用ViewCompat.setElevation(查看視圖,浮動高度)支持API前21,它就像一個迷人。

static class ViewHolder extends RecyclerView.ViewHolder { 

    public ViewHolder(View root) { 
    // bind views 
    // ... 

    // bind focus listener 
    root.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
      // run scale animation and make it bigger 

      ViewCompat.setElevation(root, 1); 
      } else { 
      // run scale animation and make it smaller 

      ViewCompat.setElevation(root, 0); 
      } 
     } 
    }); 
    } 
} 

的項目佈局文件就像下面很簡單:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:background="@drawable/sl_live_item" /> 
+0

你能分享你的佈局文件嗎?我已經使用ViewCompat.setElevation(),但它沒有幫助... – anhtuannd

+1

@anhtuannd我重新編輯我的答案,希望它可以幫助。 – Xiaozou

3

1.Override的getChildDrawingOrder方法。

@Override 
protected int getChildDrawingOrder(int childCount, int i) { 
    View view = getLayoutManager().getFocusedChild(); 
    if (null == view) { 
     return super.getChildDrawingOrder(childCount, i); 
    } 
    int position = indexOfChild(view); 

    if (position < 0) { 
     return super.getChildDrawingOrder(childCount, i); 
    } 
    if (i == childCount - 1) { 
     return position; 
    } 
    if (i == position) { 
     return childCount - 1; 
    } 
    return super.getChildDrawingOrder(childCount, i); 
} 

2.RecyclerView setChildrenDrawingOrderEnabled

setChildrenDrawingOrderEnabled(true); 

3.當項目視圖具有焦點,無效其父。

@Override 
public void onFocusChange(View v, boolean hasFocus) { 
    if (hasFocus) { 
     v.setScaleX(1.5f); 
     v.setScaleY(1.5f); 
     mRecyclerView.invalidate(); 
    } else { 
     v.setScaleX(1.0f); 
     v.setScaleY(1.0f); 
    } 
} 
+0

您也可以使用'recyclerview.setChildDrawingOrderCallback'而不繼承和重寫'recyclerview'方法。 –

+0

@ramineftekhari精確! – EasonX