Recycler源码解读<二> fill 方法分析

深入剖析LinearLayoutManager中的fill方法,理解RecyclerView如何通过此方法填充布局,包括处理滑动引起的View回收,以及布局过程中对空间的计算与分配。

跟大家说明下,我分析的源码是LinearLayoutManager 里面的fill方法,Recycler系列都是采用这个布局里面的方法。

首先我提出了fill方法的所有源码。

    int fill(RecyclerView.Recycler recycler, LayoutState layoutState,
            RecyclerView.State state, boolean stopOnFocusable) {
        // max offset we should set is mFastScroll + available
        final int start = layoutState.mAvailable;
        if (layoutState.mScrollingOffset != LayoutState.SCROLLING_OFFSET_NaN) {
            // TODO ugly bug fix. should not happen
            if (layoutState.mAvailable < 0) {
                layoutState.mScrollingOffset += layoutState.mAvailable;
            }
            recycleByLayoutState(recycler, layoutState);
        }
        int remainingSpace = layoutState.mAvailable + layoutState.mExtra;
        LayoutChunkResult layoutChunkResult = mLayoutChunkResult;
        while ((layoutState.mInfinite || remainingSpace > 0) && layoutState.hasMore(state)) {
            layoutChunkResult.resetInternal();
            layoutChunk(recycler, state, layoutState, layoutChunkResult);
            if (layoutChunkResult.mFinished) {
                break;
            }
            layoutState.mOffset += layoutChunkResult.mConsumed * layoutState.mLayoutDirection;
            /**
             * Consume the available space if:
             * * layoutChunk did not request to be ignored
             * * OR we are laying out scrap children
             * * OR we are not doing pre-layout
             */
            if (!layoutChunkResult.mIgnoreConsumed || mLayoutState.mScrapList != null
                    || !state.isPreLayout()) {
                layoutState.mAvailable -= layoutChunkResult.mConsumed;
                // we keep a separate remaining space because m

class MainViewModel(application: Application) : AndroidViewModel(application) { private val _appInfo = MutableLiveData<String>() val appInfo: LiveData<String> = _appInfo.apply { value = getAppInfo() } private fun getAppInfo(): String { val context = getApplication<AppApplication>().applicationContext var appInfo = AppUtils.getAppName(context) + ":v" + AppUtils.getVersionName(context) if (getAppReleaseTime().isNotEmpty()) { appInfo += "_" + getAppReleaseTime() } return appInfo } private fun getAppReleaseTime(): String { val appReleaseTime = "" val context = getApplication<AppApplication>().applicationContext context.packageManager.getApplicationInfo( "com.nxg.app", PackageManager.GET_META_DATA ).metaData.getString("RELEASE_TIME") return appReleaseTime } private val _doubleColorBallNum = MutableLiveData<MutableList<List<TextBean>>>() val doubleColorBallNum: LiveData<MutableList<List<TextBean>>> = _doubleColorBallNum private fun createDoubleColorBallNum(): MutableList<List<TextBean>> { val data = mutableListOf<List<TextBean>>() for (i in 0..6) { val numberList = mutableListOf<TextBean>() val bound = if (i < 6) { 33 } else { 16 } LogUtil.i("MainViewModel", "i = $i, bound $bound") for (j in 1..bound) { numberList.add( TextBean( if (j < 10) "0$j" else { j.toString() } ) ) } //LogUtil.i("MainViewModel", "i = $i, before $numberList") numberList.shuffle() //LogUtil.i("MainViewModel", "i = $i, after $numberList") data.add(i, numberList) } return data } fun refreshDoubleColorBallNum() { _doubleColorBallNum.value = createDoubleColorBallNum() } /** * 生成6个红色双色球号码 */ private fun createSixRedDoubleColorBall(): List<String> { val redBalls = mutableListOf<String>() for (j in 1..33) { redBalls.add( if (j < 10) "0$j" else { j.toString() } ) } LogUtil.i("MainViewModel", "createSixRedDoubleColorBall before $redBalls") redBalls.shuffle() LogUtil.i("MainViewModel", "createSixRedDoubleColorBall shuffle after $redBalls") val balls = redBalls.subList(0, 6) LogUtil.i("MainViewModel", "createSixRedDoubleColorBall subList after $balls") balls.sort() LogUtil.i("MainViewModel", "createSixRedDoubleColorBall sort after $balls") return balls } /** * 生成1个蓝色双色球号码 */ private fun createSingleBlueDoubleColorBall(): String { val blueBall = Random().nextInt(16) + 1 LogUtil.i("MainViewModel", "createSingleBlueDoubleColorBall $blueBall") return if (blueBall < 10) "0$blueBall" else { blueBall.toString() } } /** * 随机生成6个红色双色球号码+1个蓝色双色球号码 */ fun createSevenDoubleColorBall(): List<String> { val balls = mutableListOf<String>() val redBalls = createSixRedDoubleColorBall() balls.addAll(redBalls) val blueBall = createSingleBlueDoubleColorBall() balls.add(blueBall) LogUtil.i("MainViewModel", "createSevenDoubleColorBall $balls") return balls } } 转Android java
最新发布
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值