在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
效果圖如下:
這里寫圖片描述