Copyright notice: This article is the blogger’s original article, reprint please indicate the source.
Recommendation: welcome attention I create Android TV Jane books project, regularly to share some AndroidTv related content: www.jianshu.com/c/37efc6e97…
Introduction: In an article about the custom RecyclerView of Android Tv, some usage and realization methods of transverse RecyclerView are introduced. On the basis of the previous article, to achieve RecyclerView horizontal and vertical linkage effect. ###
The new version builds on the previous version with some additions and bugs reported by readers. The specific added content is as follows: 1. Added vertical RecyclerView, and can control the effect of transverse RecyclerView. 2. After clicking the left and right arrow, RecyclerView items will not get the focus, which solves the sliding conflict. 3. Transverse RecyclerView5.0 after the following version of the entry will be pressed after the situation, here to fix the bug. 4. In RecyclerView, the operation similar to enlarge and raise z axis is no longer provided. Here, only the interface of focus state is provided, and the specific logic can provide callback in RecyclerView. ### Implement the following specific analysis. 1. Here the vertical RecyclerView is displayed in 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();
Copy the code
Note: popupWindow sets the background. I added an entry animation when popupWindow entered:
mPopRecyclerView.setLayoutAnimation(PopLayoutAnimation.orderAnimation());
Copy the code
The code for PopLayoutAnimation is as follows:
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.33 f);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.33 f); 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(); }}Copy the code
2. The horizontal RecyclerView is found in the process of use, when the mouse is placed on Item, RecyclerView will stop rolling in response to Item to obtain the focus event, and the effect of page turning cannot be achieved. Here we deal with the item response timing. RecyclerView responds when the RecyclerView is idle.
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)); // In order to prevent scrolling conflicts, when the focus is captured, it is rolled back in order to display the full, which will cause scrolling to stopif(recyclerView. GetScrollState () = = recyclerView. SCROLL_STATE_IDLE) {/ / when beyond the edges of the recyclerView don't respond to scrollif (event.getRawX() > recyclerView.getWidth() + x || event.getRawX() < x) {
return true; EquestFocusFromTouch (); v.requestFocus(); // LogUtil.i(this,"HomeTvAdapter.onHover.position:"+position);
focusStatus(v, position);
}
break;
caseACTION_HOVER_MOVE: // Move the mouse over the viewbreak;
caseACTION_HOVER_EXIT: // Mouse away from view normalStatus(v, position);break;
}
return false; }});Copy the code
3. Horizontal RecyclerView in the system after Android5.0, item in hover state is to raise the z axis, after the 5.0 version of the following items put on the situation will be covered, here adjust the layout. Compatibility is achieved. ### End: RecyclerView usage and common RecyclerView usage is similar, as for the layout of the display, can be defined according to their own projects. Project address: github.com/songwenju/C… If it helps you, welcome Star and Fork. If you have any questions, welcome to feedback and exchange.
Recommendation: welcome attention I create Android TV Jane books project, regularly to share some AndroidTv related content: www.jianshu.com/c/37efc6e97…
Copyright notice: This article is the blogger’s original article, reprint please indicate the source.