使用RecyclerView實(shí)現(xiàn)瀑布流高度自適應(yīng)
使用RecyclerView實(shí)現(xiàn)的瀑布流高度自適應(yīng),供大家參考,具體內(nèi)容如下
背景:使用時(shí)在RecyclerView外嵌套了自定義的ScrollView,需要讓RecyclerView高度自適應(yīng),由于是瀑布流格式網(wǎng)上找了好多方法都無(wú)法實(shí)現(xiàn)或是動(dòng)態(tài)計(jì)算的高度不準(zhǔn)確。估計(jì)大家都知道recyclerview 內(nèi)容的高度不是 recyclerview 控制的而是由LayoutManager 來(lái)設(shè)置的。下面我來(lái)說(shuō)下我的解決方案吧:
布局中的使用
<android.support.v7.widget.RecyclerView android:id="@+id/rcv_indexfragment_article_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen12" android:padding="@dimen/dimen4" android:background="@color/bg_white"/>
方法一
個(gè)人認(rèn)為最簡(jiǎn)單有效的方法,解決問(wèn)題最終選用的方法。
官網(wǎng)博客翻譯資料:
RecyclerView 控件提供了靈活一種創(chuàng)建列表和網(wǎng)格的基本方案,而且還支持動(dòng)畫。這個(gè)版本為 LayoutManager API帶來(lái)了一個(gè)非常激動(dòng)人心的新特性:自動(dòng)測(cè)量!讓RecyclerView可以根據(jù)其內(nèi)容的大小調(diào)整自己。這意味著以前那些無(wú)解的場(chǎng)景,比如對(duì)RecyclerView的一個(gè)dimension 使用WRAP_CONTENT成為了可能。你會(huì)發(fā)現(xiàn)所有的內(nèi)置LayoutManager現(xiàn)在都支持自動(dòng)測(cè)量。
因?yàn)檫@個(gè)變化的原因,你得仔細(xì)檢查 item view的 layout parameters 了:以前會(huì)被自動(dòng)忽略的 layout parameters(比如在滾動(dòng)方向上的MATCH_PARENT ),現(xiàn)在會(huì)被完全考慮進(jìn)去。如果你有一個(gè)自定義的LayoutManager,且沒有繼承自內(nèi)置的LayoutManager,那么這就是一個(gè)可選的API - 你需要調(diào)用setAutoMeasureEnabled(true) 并且根據(jù)這個(gè)方法的Javadoc中的描述做一些微小的改變。
注意,雖然RecyclerView可以讓自己的子View動(dòng)畫,但是它不能動(dòng)畫自己的邊界改變。如果你想要讓RecyclerView的邊界變化也呈現(xiàn)動(dòng)畫,你可以使用 Transition API。
配置的版本:
compile 'com.android.support:recyclerview-v7:23.2.1'
想要內(nèi)容隨高度變化需設(shè)置:(注意:此方式是Android Support Library 23.2中引入的,如果配置之前的版本會(huì)報(bào)錯(cuò)哦~)
layoutManager.setAutoMeasureEnabled(true);
Activity中的使用:
recyclerview= ViewFindUtils.find(view, R.id.recyclerview); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); layoutManager.setAutoMeasureEnabled(true); recyclerview.setLayoutManager(layoutManager); recyclerview.setHasFixedSize(true); recyclerview.setNestedScrollingEnabled(false); SpacesItemDecoration decoration = new SpacesItemDecoration(6); recyclerview.addItemDecoration(decoration);
方法二
自定義CustomStaggeredGridLayoutManager類繼承自StaggeredGridLayoutManager。(注:此方法用到我的項(xiàng)目中計(jì)算的高度不準(zhǔn)確,列表后面總有一段空白,所以我放棄了~不過(guò)你們可以試試,可能是我布局嵌套的問(wèn)題)
配置的版本(建議使用最新的版本):
compile 'com.android.support:recyclerview-v7:23.1.1'
Activity中的使用:
recyclerview= ViewFindUtils.find(view, R.id.recyclerview); CustomStaggeredGridLayoutManager layoutManager = new CustomStaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); recyclerview.setLayoutManager(layoutManager); recyclerview.setHasFixedSize(true); recyclerview.setNestedScrollingEnabled(false); SpacesItemDecoration decoration = new SpacesItemDecoration(6); recyclerview.addItemDecoration(decoration);
SpacesItemDecoration.class
public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.top = space; outRect.bottom = space; outRect.left = space; outRect.right = space; } }
CustomStaggeredGridLayoutManager.class
package com.sunny.demo.widget; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.view.ViewGroup; import com.parkmecn.ehc.utils.LogUtil; /** * 解決ScrollView嵌套R(shí)ecyclerView時(shí)RecyclerView需要高度自適應(yīng)的問(wèn)題 */ public class CustomStaggeredGridLayoutManager extends StaggeredGridLayoutManager { public CustomStaggeredGridLayoutManager(int spanCount, int orientation) { super(spanCount, orientation); } // 尺寸的數(shù)組,[0]是寬,[1]是高 private int[] measuredDimension = new int[2]; // 用來(lái)比較同行/列那個(gè)item罪寬/高 private int[] dimension; @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { // 寬的mode+size final int widthMode = View.MeasureSpec.getMode(widthSpec); final int widthSize = View.MeasureSpec.getSize(widthSpec); // 高的mode + size final int heightMode = View.MeasureSpec.getMode(heightSpec); final int heightSize = View.MeasureSpec.getSize(heightSpec); // 自身寬高的初始值 int width = 0; int height = 0; // item的數(shù)目 int count = getItemCount(); // item的列數(shù) int span = getSpanCount(); // 根據(jù)行數(shù)或列數(shù)來(lái)創(chuàng)建數(shù)組 dimension = new int[span]; for (int i = 0; i < count; i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View .MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredDimension); // 如果是豎直的列表,計(jì)算item的高,否則計(jì)算寬度 // LogUtil.d("LISTENER", "position " + i + " height = " + measuredDimension[1]); if (getOrientation() == VERTICAL) { dimension[findMinIndex(dimension)] += measuredDimension[1]; } else { dimension[findMinIndex(dimension)] += measuredDimension[0]; } } if (getOrientation() == VERTICAL) { height = findMax(dimension); } else { width = findMax(dimension); } switch (widthMode) { // 當(dāng)控件寬是match_parent時(shí),寬度就是父控件的寬度 case View.MeasureSpec.EXACTLY: width = widthSize; break; case View.MeasureSpec.AT_MOST: break; case View.MeasureSpec.UNSPECIFIED: break; } switch (heightMode) { // 當(dāng)控件高是match_parent時(shí),高度就是父控件的高度 case View.MeasureSpec.EXACTLY: height = heightSize; break; case View.MeasureSpec.AT_MOST: break; case View.MeasureSpec.UNSPECIFIED: break; } // 設(shè)置測(cè)量尺寸 setMeasuredDimension(width, height); LogUtil.e("setMeasuredDimension(width, height)--->height==" + height); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { // 挨個(gè)遍歷所有item if (position < getItemCount()) { try { View view = recycler.getViewForPosition(position);//fix 動(dòng)態(tài)添加時(shí)報(bào)IndexOutOfBoundsException if (view != null) { RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight (), lp.width); LogUtil.e(position + "--->heightSpec=" + heightSpec + ";getPaddingTop()=" + getPaddingTop() + ";" + "getPaddingBottom()" + getPaddingBottom() + ";lp.height=" + lp.height); int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), lp.height); LogUtil.e(position + "--->viewchildHeightSpec=" + childHeightSpec); // 子view進(jìn)行測(cè)量,然后可以通過(guò)getMeasuredWidth()獲得測(cè)量的寬,高類似 view.measure(childWidthSpec, childHeightSpec); // 將item的寬高放入數(shù)組中 measuredDimension[0] = view.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; //FIXME 此處計(jì)算的高度總比實(shí)際高度要高一些,導(dǎo)致最后RecycerView的高度計(jì)算不對(duì)最后留有一段空白,暫時(shí)沒有找到問(wèn)題所在,待大神的解決啊~ measuredDimension[1] = view.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; LogUtil.e(position + "--->view.getMeasuredHeight()=" + view.getMeasuredHeight() + ";lp" + ".topMargin=" + lp.topMargin + ";lp.bottomMargin=" + lp.bottomMargin); recycler.recycleView(view); } } catch (Exception e) { e.printStackTrace(); } } } private int findMax(int[] array) { int max = array[0]; for (int value : array) { if (value > max) { max = value; } } return max; } /** * 得到最數(shù)組中最小元素的下標(biāo) * * @param array * @return */ private int findMinIndex(int[] array) { int index = 0; int min = array[0]; for (int i = 0; i < array.length; i++) { if (array[i] < min) { min = array[i]; index = i; } } return index; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義view實(shí)現(xiàn)圓的擴(kuò)散效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)圓的擴(kuò)散效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01android studio 4.0 新建類沒有修飾符的方法
這篇文章主要介紹了android studio 4.0 新建類沒有修飾符的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Recyclerview添加頭布局和尾布局、item點(diǎn)擊事件詳解
這篇文章主要為大家詳細(xì)介紹了Recyclerview添加頭布局和尾布局、item點(diǎn)擊事件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼
這篇文章主要介紹了android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問(wèn)
這篇文章主要介紹了Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的方法。接下來(lái)通過(guò)本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12解決eclipse啟動(dòng)時(shí)報(bào)錯(cuò)Failed to create the Java Virtural Machine.問(wèn)題的
這篇文章主要介紹了解決eclipse啟動(dòng)時(shí)報(bào)Failed to create the Java Virtural Machine.問(wèn)題的方法,感興趣的小伙伴們可以參考一下2016-01-01Android編程實(shí)現(xiàn)的一鍵鎖屏程序詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)的一鍵鎖屏程序,結(jié)合實(shí)例形式詳細(xì)分析了Android一鍵鎖屏的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10