androidtv development series two recycleview item enlargement effect

I won’t say much about the use of Recycleview here. Here I mainly talk about some problems encountered when developing TV and the ideas for solving them.

1. Focus acquisition and mobile The
   android system itself supports the operation of the remote control, the English name is D-pad (Direction pad), that is, moving up, down, left and right. The problem to be noted here is that the focus will be lost. There are several lines of data in my project, and each line is a recycleview. Here is a property to set
   RecyclerView ll = new RecyclerView(context);
     //http://blog.csdn.net/wx123ww/article/details/51567982
       ll.setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);//The relationship between the focus acquisition between the parent control and the child control means that the focus priority is that if the parent does not add this line after the descendant, the focus will sometimes be lost.


2 The zoom effect after getting the focus
Focus zoom is basically using animation to zoom in, mainly adding onfocuschange monitoring to the item. It should be noted that the following pits
(1) are not large enough to find that the zoomed-in picture has been cut off, and it always feels like
Can't get out    and add attributes in a box
 
     ll.setClipToPadding(false);
        ll.setClipChildren(false);
 


  There can only be one view in item.xml that is focusable, otherwise it will not be enlarged. For example, my item.xml has the following structure. I have focsable=true on both sides. Then I added onfocuschange to relativelayout and found that it didn't work at all. Just remove the focusable of the imageview.
  relativelayout
                      -- imageview


(2) It can zoom in left and right but not up and down or
   recycleveiw. It is best to padding a distance so that there is room to display the image after zooming in.


(3) After zooming in, the image is blocked and cannot
   be covered here There is a problem with the order of elements in recycleview drawing. By default, recycleview draws one by one from left to right. Although you enlarge the picture, when drawing, the picture behind will be drawn on top of your focused picture. The
solution is to re-customize The drawing order, let the last drawing that gets the focus, mainly implement the ChildDrawingOrderCallback interface,
  MyAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements RecyclerView.ChildDrawingOrderCallback{
  private ViewGroup viewGroup;

 private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private List<String> list;
    private Animation zoomin,zoomout;
    private RecyclerView recyclerView;
    private ViewGroup viewGroup;


    public MyAdapter(Context mContext, List<String> list,TopRecycleView recyclerView) {
        this.mContext = mContext;
        this.list = list;
        this.recyclerView=recyclerView;
        mLayoutInflater = LayoutInflater.from(mContext);
        zoomin = AnimationUtils.loadAnimation (mContext, R.anim.zoomin);
        zoomout = AnimationUtils.loadAnimation (mContext, R.anim.zoomout);
    }

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        viewGroup=parent;
        return new MovieHolder(mLayoutInflater.inflate(R.layout.item_tv, null));
    }

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MovieHolder cHolder = (MovieHolder) holder;


        cHolder.placeholder.setAnimation (zoomed in);
        cHolder.placeholder.setAnimation(zoomout);

        cHolder.placeholder.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {


                if(b){
                    //zoom in
//                    if(Build.VERSION.SDK_INT>=21) {
//                        ViewCompat.animate(view).scaleX(1.17f).scaleY(1.17f).translationZ(1).start();
//                    }

                    //view.bringToFront();
                    //zoomin.setFillAfter(true);//These two sentences are basically useless

                    view.startAnimation (zoomin);

                    //recyclerView.requestLayout(); will all shake

                    /**The following 3 codes have the same effect*/

                    /**1*****/
                    //recyclerView.invalidate();//recyclerView is passed

                    /**2***Because there is a Layout above the placeholder in the current example, the layout above is **/
                    //ViewGroup viewGroup=(ViewGroup) view.getParent().getParent();
                    //viewGroup.invalidate();

                    /**3*****/
                    viewGroup.invalidate();


                }else{
                    //restore
//                    if(Build.VERSION.SDK_INT>=21) {
//                        ViewCompat.animate(view).scaleX(1f).scaleY(1f).translationZ(0).start();
//                    }
                    //zoomout.setFillAfter(true);
                    view.startAnimation(zoomout);
                }

            }
        });
    }


/**Change the drawing order so that the last drawing that gets the focus will float on top of other items http://www.aichengxu.com/view/11147419*/
    @Override
    public int onGetChildDrawingOrder(int childCount, int i) {
        View focusedChild = viewGroup.getFocusedChild();
        int focusViewIndex =viewGroup.indexOfChild(focusedChild);
        if (focusViewIndex == -1) {
            return i;
        }

        int focusid=focusViewIndex;

        if (focusViewIndex == i) {
            focusid = i;
            return childCount - 1;
        } else if (i == childCount - 1) {
            return focusid;
        } else {
            return i;
        }
    }
}



where your call
recyclerView.setChildDrawingOrderCallback(myAdapter);//This sentence is very important, let the focused item float above other items


The unresolved problem, how to keep the selected item in the middle of the recycleview, now every time you move to the element on the edge, it is displayed on the edge.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326391114&siteId=291194637