版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請注明出處。
推薦:
歡迎關(guān)注我創(chuàng)建的Android TV 簡書專題,會定期給大家分享一些AndroidTv相關(guān)的內(nèi)容:
http://www.itdecent.cn/c/37efc6e9799b
前言:在上一篇針對Android Tv的自定義RecyclerView文章中介紹了橫向的RecyclerView一些用法和實(shí)現(xiàn)方法。在上一篇的基礎(chǔ)上,來實(shí)現(xiàn)RecyclerView橫向和縱向的連動效果。
首先上效果圖:

新的版本在上一個版本的基礎(chǔ)上添加了一些內(nèi)容以及讀者反饋的bug.具體添加的內(nèi)容如下:
1.添加了垂直的RecyclerView,并實(shí)現(xiàn)了可以控制橫向recyclerView的效果。
2.左右箭頭點(diǎn)擊后RecyclerView的條目不會獲得焦點(diǎn),解決了滑動沖突。
3.橫向RecyclerView5.0以下版本之后條目放上之后會出現(xiàn)被壓蓋的情況,這里修復(fù)了該bug。
4.在RecyclerView內(nèi)部不再提供類似于放大抬高z軸的操作,這里只提供了focus狀態(tài)的接口,具體的邏輯在RecyclerView使用處提供回調(diào)。
實(shí)現(xiàn)
下面具體來分析一下。
1.這里豎直的RecyclerView是在popupWindow中展示的。
View popupView = getLayoutInflater().inflate(R.layout.list_menu_popwindow, null, false);
mPopRecyclerView = (CustomRecyclerView) popupView.findViewById(R.id.recycler_view);
mUpArr = (ImageButton) popupView.findViewById(R.id.up_arrow);
mDownArr = (ImageButton) popupView.findViewById(R.id.down_arrow);
mPopupWindow = new PopupWindow(popupView, 404, 1920);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.update();
注意:popupWindow要設(shè)置背景。
在popupWindow進(jìn)入的時候我添加了一個進(jìn)入的動畫:
mPopRecyclerView.setLayoutAnimation(PopLayoutAnimation.orderAnimation());
PopLayoutAnimation相關(guān)代碼如下:
public class PopLayoutAnimation {
private static Animation transAnim, alphaAnim;
static LayoutAnimationController controller;
public static LayoutAnimationController orderAnimation() {
LayoutAnimationController controller;
Animation transAnim, alphaAnim;
AnimationSet set = new AnimationSet(false);
transAnim = new TranslateAnimation(0, 0, -50, 0);
transAnim.setDuration(167);
transAnim.setFillEnabled(true);
transAnim.setFillAfter(true);
alphaAnim = new AlphaAnimation(0, 1);
alphaAnim.setDuration(167);
alphaAnim.setFillAfter(true);
set.addAnimation(transAnim);
set.addAnimation(alphaAnim);
controller = new LayoutAnimationController(set, 1);
controller.setDelay(0.33f);
return controller;
}
public static LayoutAnimationController reserverAnimation(){
AnimationSet set = new AnimationSet(false);
transAnim = new TranslateAnimation(0, 0,50, 0);
transAnim.setDuration(167);
transAnim.setFillEnabled(true);
transAnim.setFillAfter(true);
// transAnim.setFillBefore(true);
alphaAnim = new AlphaAnimation(0,1);
alphaAnim.setDuration(167);
alphaAnim.setFillAfter(true);
set.addAnimation(transAnim);
set.addAnimation(alphaAnim);
controller = new LayoutAnimationController(set, 1);
controller.setDelay(0.33f);
controller.setOrder(LayoutAnimationController.ORDER_REVERSE);
return controller;
}
public static boolean isNullAnimation(){
if (controller==null||controller.isDone()){
return true;
}
else{
return false;
}
}
public static void resetAnimation(){
controller.start();
}
}
2.橫向的RecyclerView在使用的過程中發(fā)現(xiàn)在滾動的時候,鼠標(biāo)放到Item上RecyclerView為了響應(yīng)item獲取焦點(diǎn)事件會停止?jié)L動,達(dá)不到翻頁的效果。這里處理了item響應(yīng)時機(jī)。在RecyclerView處于idle狀態(tài)時去響應(yīng)。
holder.itemView.setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
int what = event.getAction();
switch (what) {
case MotionEvent.ACTION_HOVER_ENTER:
RecyclerView recyclerView = (RecyclerView) holder.itemView.getParent();
int[] location = new int[2];
recyclerView.getLocationOnScreen(location);
int x = location[0];
// LogUtil.i("swj","GalleryAdapter.onHover.x="+x +",width = "+(recyclerView.getWidth()+x));
//為了防止?jié)L動沖突,在滾動時候,獲取焦點(diǎn)為了顯示全,會回滾,這樣會導(dǎo)致滾動停止
if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
//當(dāng)超出RecyclerView的邊緣時不去響應(yīng)滾動
if (event.getRawX() > recyclerView.getWidth() + x || event.getRawX() < x) {
return true;
}
//鼠標(biāo)進(jìn)入view,爭取到焦點(diǎn)
v.requestFocusFromTouch();
v.requestFocus();
// LogUtil.i(this,"HomeTvAdapter.onHover.position:"+position);
focusStatus(v, position);
}
break;
case MotionEvent.ACTION_HOVER_MOVE: //鼠標(biāo)在view上移動
break;
case MotionEvent.ACTION_HOVER_EXIT: //鼠標(biāo)離開view
normalStatus(v, position);
break;
}
return false;
}
});
3.橫向RecyclerView在Android5.0以后的系統(tǒng),item在hover態(tài)時是抬高z軸,5.0以下版本之后條目放上之后會出現(xiàn)被壓蓋的情況,這里調(diào)整了一下布局。達(dá)到了兼容。
結(jié)束:
這里的RecyclerView用法和普通的RecyclerView用法類似,至于布局的顯示,可以根據(jù)自己的項(xiàng)目去定義。
項(xiàng)目的地址:https://github.com/songwenju/CustomTvRecyclerView
如果對你有幫助,歡迎star和fork。如有問題,歡迎反饋交流。
推薦:
歡迎關(guān)注我創(chuàng)建的Android TV 簡書專題,會定期給大家分享一些AndroidTv相關(guān)的內(nèi)容:
http://www.itdecent.cn/c/37efc6e9799b
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請注明出處。