您的位置:首页 > 移动开发 > Android开发

Android中recycle高度的计算

2016-07-22 10:27 417 查看
recyclerView.setLayoutManager(new GridLayoutManager(activity, 3) {  

                @Override  

                public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {  

                    super.onMeasure(recycler, state, widthSpec, heightSpec);  

                    int measuredWidth = recyclerView.getMeasuredWidth();  

                    int measuredHeight = recyclerView.getMeasuredHeight();  

                    int myMeasureHeight = 0;  

                    int count = state.getItemCount();  

                    for (int i = 0; i < count; i++) {  

                        View view = recycler.getViewForPosition(i);  

                        if (view != null) {  

                            if (myMeasureHeight < measuredHeight && i % 3 == 0) {  

                                RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();  

                                int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,  

                                        getPaddingLeft() + getPaddingRight(), p.width);  

                                int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,  

                                        getPaddingTop() + getPaddingBottom(), p.height);  

                                view.measure(childWidthSpec, childHeightSpec);  

                                myMeasureHeight += view.getMeasuredHeight() + p.bottomMargin + p.topMargin;  

                            }  

                            recycler.recycleView(view);  

                        }  

                    }  

//                    Log.i("Height", "" + Math.min(measuredHeight, myMeasureHeight));  

                    setMeasuredDimension(measuredWidth, Math.min(measuredHeight, myMeasureHeight));  

                }  





顶0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: