如果本文幫助到你,本人不勝榮幸窃植,如果浪費(fèi)了你的時(shí)間帝蒿,本人深感抱歉。
希望用最簡單的大白話來幫助那些像我一樣的人巷怜。如果有什么錯(cuò)誤葛超,請一定指出,以免誤導(dǎo)大家延塑、也誤導(dǎo)我绣张。
本文來自:http://www.reibang.com/u/320f9e8f7fc9
感謝您的關(guān)注。
首先看實(shí)現(xiàn)效果圖:
類似的這種需求在實(shí)際的項(xiàng)目中還是挺多的页畦。
說說的詳情頁胖替,頂部顯示內(nèi)容,然后顯示一個(gè)熱門評論豫缨,最后顯示全部評論。
兩個(gè)評論列表數(shù)量都是動(dòng)態(tài)的端朵,并且全部評論還可以下拉刷新好芭。
先放項(xiàng)目地址:https://github.com/Wing-Li/DoubleList
建議把項(xiàng)目下載下來看看,項(xiàng)目非常簡單冲呢,此文主要是流程舍败。
我們來實(shí)現(xiàn)這個(gè)效果。
1. 布局
最關(guān)鍵的是 NestedScrollView 控件敬拓,看這張圖
NestedScrollView // 滾動(dòng)頁
- LinearLayout // NestedScrollView 只能包含一個(gè) LinearLayout
- LinearLayout // 說說詳情
- LinearLayout // 熱門評論
- RecyclerView
- LinearLayout // 全部評論
- RecyclerView
2. 重寫 LinearLayoutManager
初始化 RecyclerView 時(shí)需要設(shè)置 setLayoutManager(),我們需要重寫它來計(jì)算列表的高度邻薯。
代碼如下:
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() 時(shí)乘凸,使用我們自定的 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);// 如果導(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);
}
<br />
注意:
- wrappingLinearLayoutManager.setAutoMeasureEnabled(false);
如果導(dǎo)入的包是 Android Support Library 23.2.0 以上的壹罚,需要加這句。 - RecyclerHot.setNestedScrollingEnabled(false);
在這里setNestedScrollingEnabled(false)禁用滾動(dòng)為RecyclerView寿羞,它不會攔截從NestedScrollView滾動(dòng)事件猖凛。 - setHasFixedSize(false) (默認(rèn)false)
確定適配器內(nèi)容中的更改可以更改RecyclerView的大小。
<br />
至此就可以實(shí)現(xiàn)想要達(dá)到的效果绪穆。
其中辨泳,主要的核心就是 NestedScrollView 的使用 和 WrappingLinearLayoutManager 的自定義。
<br />
項(xiàng)目地址:https://github.com/Wing-Li/DoubleList
建議把項(xiàng)目下載下來看看玖院,項(xiàng)目非常簡單菠红,此文主要是流程。