單個Acticity顯示多個列表,仿內涵段子詳情頁的熱門評論、所有評論

若是本文幫助到你,本人不勝榮幸,若是浪費了你的時間,本人深感抱歉。 但願用最簡單的大白話來幫助那些像我同樣的人。若是有什麼錯誤,請必定指出,以避免誤導你們、也誤導我。 本文來自:www.jianshu.com/u/320f9e8f7… 感謝您的關注。android

首先看實現效果圖:git

相似的這種需求在實際的項目中仍是挺多的。 說說的詳情頁,頂部顯示內容,而後顯示一個熱門評論,最後顯示所有評論。 兩個評論列表數量都是動態的,而且所有評論還能夠下拉刷新。github

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

咱們來實現這個效果。ide


1. 佈局

最關鍵的是 NestedScrollView 控件,看這張圖佈局

NestedScrollView  // 滾動頁
 - LinearLayout  // NestedScrollView 只能包含一個 LinearLayout
   - LinearLayout // 說說詳情
   - LinearLayout // 熱門評論
     - RecyclerView
   - LinearLayout // 所有評論
     - RecyclerView
複製代碼
2. 重寫 LinearLayoutManager

初始化 RecyclerView 時須要設置 setLayoutManager(),咱們須要重寫它來計算列表的高度。 代碼以下:spa

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。code

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() {
    // 設置熱門評論列表
    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);


    // 設置所有評論列表
    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);
}
複製代碼
**注意:** 1. wrappingLinearLayoutManager.setAutoMeasureEnabled(false); 若是導入的包是 Android Support Library 23.2.0 以上的,須要加這句。 2. RecyclerHot.setNestedScrollingEnabled(false); 在這裏setNestedScrollingEnabled(false)禁用滾動爲RecyclerView,它不會攔截從NestedScrollView滾動事件。 3. setHasFixedSize(false) (默認false) 肯定適配器內容中的更改能夠更改RecyclerView的大小。 至此就能夠實現想要達到的效果。 其中,主要的核心就是 NestedScrollView 的使用 和 WrappingLinearLayoutManager 的自定義。 *** 項目地址:https://github.com/Wing-Li/DoubleList 建議把項目下載下來看看,項目很是簡單,此文主要是流程。
相關文章
相關標籤/搜索