Android RecyclerView item選中放大被遮擋問題

在Android TV上一般選中某個View, 都會有焦點突出放大的效果, 但是當(dāng)在RecyclerView中(ListView或GridView)實現(xiàn)當(dāng)item View執(zhí)行放大動畫后會被其他的item View遮擋. 原因是: RecyclerView的機(jī)制是越靠后的View z-order越高, 所以bringToFront方法是不管用的.在實現(xiàn)針對TV端的自定義控件 TvRecyclerView 時遇到此問題, 最后的解決方案是: 自定義RecyclerView, 重寫getChildDrawingOrder方法, 讓選中的item最后繪制, 這樣就不會讓其他view遮擋.public class ScaleRecyclerView extends RecyclerView {? ? private int mSelectedPosition = 0;? ? public ScaleRecyclerView(Context context) {? ? ? ? super(context);? ? ? ? init();? ? }? ? public ScaleRecyclerView(Context context, @Nullable AttributeSet attrs) {? ? ? ? super(context, attrs);? ? ? ? init();? ? }? ? public ScaleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {? ? ? ? super(context, attrs, defStyle);? ? ? ? init();? ? }? ? private void init() {? ? ? ? //啟用子視圖排序功能? ? ? ? setChildrenDrawingOrderEnabled(true);? ? }? ? @Override? ? public void onDraw(Canvas c) {? ? ? ? mSelectedPosition = getChildAdapterPosition(getFocusedChild());? ? ? ? super.onDraw(c);? ? }? ? @Override? ? protected int getChildDrawingOrder(int childCount, int i) {? ? ? ? int position = mSelectedPosition;? ? ? ? if (position < 0) {? ? ? ? ? ? return i;? ? ? ? } else {? ? ? ? ? ? if (i == childCount - 1) {? ? ? ? ? ? ? ? if (position > i) {? ? ? ? ? ? ? ? ? ? position = i;? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? return position;? ? ? ? ? ? }? ? ? ? ? ? if (i == position) {? ? ? ? ? ? ? ? return childCount - 1;? ? ? ? ? ? }? ? ? ? }? ? ? ? return i;? ? }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484912345678910111213141516171819202122232425262728293031323334353637383940414243444546474849最好還需要設(shè)置RecyclerView的父類的屬性: clipChildren = false, clipToPadding = false, 避免邊緣的子view被父類遮擋.123456789101112123456789101112使用介紹: (1) 自定具有放大縮小的布局:public class FocusRelativeLayout extends RelativeLayout {? ? private Animation scaleSmallAnimation;? ? private Animation scaleBigAnimation;? ? public FocusRelativeLayout(Context context) {? ? ? ? super(context);? ? }? ? public FocusRelativeLayout(Context context, AttributeSet attrs) {? ? ? ? super(context, attrs);? ? }? ? public FocusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {? ? ? ? super(context, attrs, defStyleAttr);? ? }? ? @Override? ? protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {? ? ? ? super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);? ? ? ? if (gainFocus) {? ? ? ? ? ? getRootView().invalidate();? ? ? ? ? ? zoomOut();? ? ? ? } else {? ? ? ? ? ? zoomIn();? ? ? ? }? ? }? ? private void zoomIn() {? ? ? ? if (scaleSmallAnimation == null) {? ? ? ? ? ? scaleSmallAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_small);? ? ? ? }? ? ? ? startAnimation(scaleSmallAnimation);? ? }? ? private void zoomOut() {? ? ? ? if (scaleBigAnimation == null) {? ? ? ? ? ? scaleBigAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_big);? ? ? ? }? ? ? ? startAnimation(scaleBigAnimation);? ? }}123456789101112131415161718192021222324252627282930313233343536373839404142123456789101112131415161718192021222324252627282930313233343536373839404142(2) 放大動畫xml配置如下:123456789101112131415123456789101112131415(3) 主布局xml:12345678910111213141234567891011121314(4) 子視圖的xml:12345678910111234567891011(5) adapter配置:public class MyAdapter extends RecyclerView.Adapter{

private Context mContext;

private OnItemStateListener mListener;

private static int[] mColorIds = {R.color.amber, R.color.brown, R.color.cyan,

R.color.deepPurple, R.color.green, R.color.lightBlue, R.color.lightGreen,

R.color.lime, R.color.orange, R.color.pink, R.color.cyan, R.color.deepPurple};

MyAdapter(Context context) {

mContext = context;

}

public void setOnItemStateListener(OnItemStateListener listener) {

mListener = listener;

}

@Override

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

return new RecyclerViewHolder(View.inflate(mContext, R.layout.item_recyclerview, null));

}

@Override

public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

final RecyclerViewHolder viewHolder = (RecyclerViewHolder) holder;

viewHolder.mRelativeLayout.setBackgroundColor(ContextCompat.getColor(mContext, mColorIds[position]));

}

@Override

public int getItemCount() {

return mColorIds.length;

}

private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

FocusRelativeLayout mRelativeLayout;

RecyclerViewHolder(View itemView) {

super(itemView);

mRelativeLayout = (FocusRelativeLayout) itemView.findViewById(R.id.rl_main_layout);

mRelativeLayout.setOnClickListener(this);

}

@Override

public void onClick(View v) {

if (mListener != null) {

mListener.onItemClick(v, getAdapterPosition());

}

}

}

public interface OnItemStateListener {

void onItemClick(View view, int position);

}

}

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

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

(6) Activity中的配置:

public class RecyclerActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_recycler);

final ScaleRecyclerView recyclerView = (ScaleRecyclerView) findViewById(R.id.main_recyclerView);

GridLayoutManager manager = new GridLayoutManager(RecyclerActivity.this, 1);

manager.setOrientation(LinearLayoutManager.HORIZONTAL);

manager.supportsPredictiveItemAnimations();

recyclerView.setLayoutManager(manager);

int itemSpace = getResources().

getDimensionPixelSize(R.dimen.recyclerView_item_space);

recyclerView.addItemDecoration(new SpaceItemDecoration(itemSpace));

final MyAdapter mAdapter = new MyAdapter(RecyclerActivity.this);

recyclerView.setAdapter(mAdapter);

}

private class SpaceItemDecoration extends RecyclerView.ItemDecoration {

private int space;

SpaceItemDecoration(int space) {

this.space = space;

}

@Override

public void getItemOffsets(Rect outRect, View view, RecyclerView parent,

RecyclerView.State state) {

outRect.left = space;

}

}

}

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

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

效果圖如下:

這里寫圖片描述

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肮柜,一起剝皮案震驚了整個濱河市试浙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌婴谱,老刑警劉巖控乾,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匙铡,死亡現(xiàn)場離奇詭異撵摆,居然都是意外死亡诞挨,警方通過查閱死者的電腦和手機(jī)莉撇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惶傻,“玉大人棍郎,你說我怎么就攤上這事∫遥” “怎么了涂佃?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蜈敢。 經(jīng)常有香客問我辜荠,道長,這世上最難降的妖魔是什么抓狭? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任伯病,我火速辦了婚禮,結(jié)果婚禮上辐宾,老公的妹妹穿的比我還像新娘狱从。我一直安慰自己,他們只是感情好叠纹,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布季研。 她就那樣靜靜地躺著,像睡著了一般誉察。 火紅的嫁衣襯著肌膚如雪与涡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天持偏,我揣著相機(jī)與錄音驼卖,去河邊找鬼。 笑死鸿秆,一個胖子當(dāng)著我的面吹牛酌畜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播卿叽,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼桥胞,長吁一口氣:“原來是場噩夢啊……” “哼恳守!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起贩虾,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤催烘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后缎罢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體伊群,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年策精,在試婚紗的時候發(fā)現(xiàn)自己被綠了舰始。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛮寂,死狀恐怖蔽午,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酬蹋,我是刑警寧澤及老,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站范抓,受9級特大地震影響骄恶,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜匕垫,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一僧鲁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧象泵,春花似錦寞秃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至忽孽,卻和暖如春绑改,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背兄一。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工厘线, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人出革。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓造壮,卻偏偏與公主長得像,于是被迫代替她去往敵國和親骂束。 傳聞我的和親對象是個殘疾皇子耳璧,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容