/**
* 计算需要滑动的距离,使焦点在滑动中始终居中
*
* @param recyclerView 父布局
* @param view 当前焦点view
*/
private int[] getScrollAmount(RecyclerView recyclerView, View view) {
int[] out = new int[2];
final int halfParentWight = recyclerView.getWidth() / 2; //recyclerView宽度一半
final int halfItemViewWight = view.getWidth() / 2; //item宽度一半
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0]; //当前焦点的X坐标
// 父布局的一半宽度+ 除去recyclerView的宽度的一半-item一半的宽度 就是居中
int centerPoint = halfParentWight + (CtvitScreenUtils.getWidth() - recyclerView.getWidth()) / 2 - halfItemViewWight;
final int dy = 0;//y轴不动
out[0] = (int) (x - centerPoint);
out[1] = dy;
return out;
}
Android tv端一种焦点居中办法
最新推荐文章于 2023-02-20 11:16:59 发布