Android仿內(nèi)涵段子詳情頁評論功能
首先看實(shí)現(xiàn)效果圖:

類似的這種需求在實(shí)際的項目中還是挺多的。說說的詳情頁,頂部顯示內(nèi)容,然后顯示一個熱門評論,最后顯示全部評論。兩個評論列表數(shù)量都是動態(tài)的,并且全部評論還可以下拉刷新。
我們來實(shí)現(xiàn)這個效果。
1、布局
最關(guān)鍵的是 NestedScrollView 控件,看這張圖

NestedScrollView // 滾動頁- LinearLayout // NestedScrollView 只能包含一個 LinearLayout- LinearLayout // 說說詳情- LinearLayout // 熱門評論- RecyclerView- LinearLayout // 全部評論- RecyclerView
2、重寫 LinearLayoutManager
初始化 RecyclerView 時需要設(shè)置 setLayoutManager(),我們需要重寫它來計算列表的高度。
代碼如下:
public?class?WrappingLinearLayoutManager?extends?LinearLayoutManager{public WrappingLinearLayoutManager(Context context) {super(context);}private int[] mMeasuredDimension = new int[2];@Overridepublic boolean canScrollVertically() {return false;}@Overridepublic void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,int widthSpec, int heightSpec) {final int widthMode = View.MeasureSpec.getMode(widthSpec);final int heightMode = View.MeasureSpec.getMode(heightSpec);final int widthSize = View.MeasureSpec.getSize(widthSpec);final int heightSize = View.MeasureSpec.getSize(heightSpec);int width = 0;int height = 0;for (int i = 0; i < getItemCount(); i++) {if (getOrientation() == HORIZONTAL) {measureScrapChild(recycler, i,View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),heightSpec,mMeasuredDimension);width = width + mMeasuredDimension[0];if (i == 0) {height = mMeasuredDimension[1];}} else {measureScrapChild(recycler, i,widthSpec,View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),mMeasuredDimension);height = height + mMeasuredDimension[1];if (i == 0) {width = mMeasuredDimension[0];}}}switch (widthMode) {case View.MeasureSpec.EXACTLY:width = widthSize;case View.MeasureSpec.AT_MOST:case View.MeasureSpec.UNSPECIFIED:}switch (heightMode) {case View.MeasureSpec.EXACTLY:height = heightSize;case View.MeasureSpec.AT_MOST:case View.MeasureSpec.UNSPECIFIED:}setMeasuredDimension(width, height);}private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,int heightSpec, int[] measuredDimension) {View view = recycler.getViewForPosition(position);if (view.getVisibility() == View.GONE) {measuredDimension[0] = 0;measuredDimension[1] = 0;return;}// For adding Item Decor Insets to viewsuper.measureChildWithMargins(view, 0, 0);RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view),p.width);int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view),p.height);view.measure(childWidthSpec, childHeightSpec);// Get decorated measurementsmeasuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;recycler.recycleView(view);}}
3、初始化 RecyclerView
就是按照正常的流程初始化 RecyclerView,只不過在 setLayoutManager() 時,
使用我們自定的 WrappingLinearLayoutManager。
至于其中的 DetailCommentAdapter 就是正常的 RecyclerView.Adapter。
RecyclerView RecyclerHot;RecyclerView RecyclerAll;private DetailCommentAdapter mHotCommentAdapter;private DetailCommentAdapter mAllCommentAdapter;private ListmHotCommentsList = new ArrayList<>(); private ListmAllCommentsList = new ArrayList<>(); private void setView() {// 設(shè)置熱門評論列表WrappingLinearLayoutManager wrappingLinearLayoutManager = new WrappingLinearLayoutManager(mContext);wrappingLinearLayoutManager.setAutoMeasureEnabled(false);// 如果導(dǎo)入的包是 Android Support Library 23.2.0 以上的,需要加這句RecyclerHot.setLayoutManager(wrappingLinearLayoutManager);mHotCommentAdapter = new DetailCommentAdapter(mContext, mHotCommentsList, DetailCommentAdapter.COMMENT_TYPE_HOT);RecyclerHot.setAdapter(mHotCommentAdapter);RecyclerHot.setNestedScrollingEnabled(false);// 設(shè)置全部評論列表WrappingLinearLayoutManager wrappingLinearLayoutManager2 = new WrappingLinearLayoutManager(mContext);wrappingLinearLayoutManager2.setAutoMeasureEnabled(false);// 如果導(dǎo)入的包是 Android Support Library 23.2.0 以上的,需要加這句RecyclerAll.setLayoutManager(wrappingLinearLayoutManager2);mAllCommentAdapter = new DetailCommentAdapter(mContext, mAllCommentsList, DetailCommentAdapter.COMMENT_TYPE_ALL);RecyclerAll.setAdapter(mAllCommentAdapter);RecyclerAll.setNestedScrollingEnabled(true);}
注意:
wrappingLinearLayoutManager.setAutoMeasureEnabled(false);
如果導(dǎo)入的包是 ?Android Support Library 23.2.0 ?以上的,需要加這句。RecyclerHot.setNestedScrollingEnabled(false);
在這里setNestedScrollingEnabled(false)禁用滾動為RecyclerView,它不會攔截從NestedScrollView滾動事件。setHasFixedSize(false) (默認(rèn)false)
確定適配器內(nèi)容中的更改可以更改RecyclerView的大小。
至此就可以實(shí)現(xiàn)想要達(dá)到的效果。
源碼地址:
https://github.com/Wing-Li/DoubleList
到這里就結(jié)束啦。
