單個Acticity顯示多個列表,仿內(nèi)涵段子詳情頁的熱門評論、全部評論

如果本文幫助到你,本人不勝榮幸,如果浪費了你的時間,本人深感抱歉。
希望用最簡單的大白話來幫助那些像我一樣的人。如果有什么錯誤,請一定指出,以免誤導大家、也誤導我。
本文來自:http://www.itdecent.cn/u/320f9e8f7fc9
感謝您的關(guān)注。

首先看實現(xiàn)效果圖:

類似的這種需求在實際的項目中還是挺多的。
說說的詳情頁,頂部顯示內(nèi)容,然后顯示一個熱門評論,最后顯示全部評論。
兩個評論列表數(shù)量都是動態(tài)的,并且全部評論還可以下拉刷新。

先放項目地址:https://github.com/Wing-Li/DoubleList
建議把項目下載下來看看,項目非常簡單,此文主要是流程。

我們來實現(xiàn)這個效果。


1. 布局

最關(guān)鍵的是 NestedScrollView 控件,看這張圖

NestedScrollView  // 滾動頁
 - LinearLayout  // NestedScrollView 只能包含一個 LinearLayout
   - LinearLayout // 說說詳情
   - LinearLayout // 熱門評論
     - RecyclerView
   - LinearLayout // 全部評論
     - RecyclerView
2. 重寫 LinearLayoutManager

初始化 RecyclerView 時需要設(shè)置 setLayoutManager(),我們需要重寫它來計算列表的高度。
代碼如下:

package com.lyl.doublelist;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lyl on 2017/6/6.
 */
public class WrappingLinearLayoutManager extends LinearLayoutManager
{

    public WrappingLinearLayoutManager(Context context) {
        super(context);
    }

    private int[] mMeasuredDimension = new int[2];

    @Override
    public boolean canScrollVertically() {
        return false;
    }

    @Override
    public 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 view
        super.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 measurements
        measuredDimension[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 List<CommentsBean> mHotCommentsList = new ArrayList<>();
private List<CommentsBean> mAllCommentsList = new ArrayList<>();

private void setView() {
    // 設(shè)置熱門評論列表
    WrappingLinearLayoutManager wrappingLinearLayoutManager = new WrappingLinearLayoutManager(mContext);
    wrappingLinearLayoutManager.setAutoMeasureEnabled(false);// 如果導入的包是  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);// 如果導入的包是  Android Support Library 23.2.0  以上的,需要加這句
    RecyclerAll.setLayoutManager(wrappingLinearLayoutManager2);

    mAllCommentAdapter = new DetailCommentAdapter(mContext, mAllCommentsList, DetailCommentAdapter.COMMENT_TYPE_ALL);
    RecyclerAll.setAdapter(mAllCommentAdapter);
    RecyclerAll.setNestedScrollingEnabled(true);
}

<br />
注意:

  1. wrappingLinearLayoutManager.setAutoMeasureEnabled(false);
    如果導入的包是 Android Support Library 23.2.0 以上的,需要加這句。
  2. RecyclerHot.setNestedScrollingEnabled(false);
    在這里setNestedScrollingEnabled(false)禁用滾動為RecyclerView,它不會攔截從NestedScrollView滾動事件。
  3. setHasFixedSize(false) (默認false)
    確定適配器內(nèi)容中的更改可以更改RecyclerView的大小。

<br />
至此就可以實現(xiàn)想要達到的效果。
其中,主要的核心就是 NestedScrollView 的使用 和 WrappingLinearLayoutManager 的自定義。


<br />
項目地址:https://github.com/Wing-Li/DoubleList
建議把項目下載下來看看,項目非常簡單,此文主要是流程。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容