2016年8月11日 星期四

How to arage the recycle view better in the StaggeredGridLayoutManager?

How to arrange the recycle view better in the StaggeredGridLayoutManager?


the concept is:need to know the width of the screen first,that calculate each column width

how to get the width of a fragment?


int mHeaderViewHeight;
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
       new OnGlobalLayoutListener() {
           @Override
           public void onGlobalLayout() {
                                                                                                                                                                                                                                     
               mHeaderViewHeight = mHeaderView.getHeight();
               getViewTreeObserver()
                       .removeGlobalOnLayoutListener(this);
           }
       });

calculate the right width of the cell,then set it


imv.getLayoutParams().width= (int) cellWidth;
       imv.getLayoutParams().height= (int) (cellWidth*3/2);

to handle the Landscape and Portrait

when Landscape

nee to return the height as width

when Portrait

use the width as width


getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE



SpacesItemDecoration extends RecyclerView.ItemDecoration

set the Decoration between the item