遇到scrollview和recycleview时的冲突解决方法

    xiaoxiao2025-05-18  7

    GridGridLayoutManager

    package ico.ico.widget.recycler; import android.content.Context; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; /** * Created by ICO on 2016/4/7 0007. */ public class AdaptiveGridLayoutManager extends GridLayoutManager { protected int mBorderWidth = 0; public AdaptiveGridLayoutManager(Context context, int spanCount, int borderWidth) { super(context, spanCount); this.mBorderWidth = borderWidth; } public AdaptiveGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout, int borderWidth) { super(context, spanCount, orientation, reverseLayout); this.mBorderWidth = borderWidth; } @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { int itemCount = state.getItemCount(); if (itemCount == 0) { super.onMeasure(recycler, state, widthSpec, heightSpec); return; } //计算行数 int rowCount = 1; if (itemCount > getSpanCount()) { rowCount = itemCount % getSpanCount() > 0 ? itemCount / getSpanCount() + 1 : itemCount / getSpanCount(); } View view = recycler.getViewForPosition(0); if (view != null) { measureChild(view, widthSpec, heightSpec); int measuredWidth = View.MeasureSpec.getSize(widthSpec); int measuredHeight = view.getMeasuredHeight(); setMeasuredDimension(measuredWidth, measuredHeight * rowCount + mBorderWidth * (rowCount - 1)); } super.onMeasure(recycler, state, widthSpec, heightSpec); } }

    LinearLayoutManager

    package ico.ico.widget.recycler; import android.content.Context; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.View; /** * Created by ICO on 2016/4/7 0007. */ public class AdaptiveLinearLayoutManager extends LinearLayoutManager { protected int mBorderWidth = 0; public AdaptiveLinearLayoutManager(Context context, int borderWidth) { super(context); this.mBorderWidth = borderWidth; } public AdaptiveLinearLayoutManager(Context context, int orientation, boolean reverseLayout, int borderWidth) { super(context, orientation, reverseLayout); this.mBorderWidth = borderWidth; } public AdaptiveLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int borderWidth) { super(context, attrs, defStyleAttr, defStyleRes); this.mBorderWidth = borderWidth; } @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST); heightSpec = expandSpec; super.onMeasure(recycler, state, widthSpec, heightSpec); // int itemCount = getItemCount(); // if (itemCount == 0) { // super.onMeasure(recycler, state, widthSpec, heightSpec); // return; // } // int rowCount = itemCount / getSpanCount() + 1; // View view = recycler.getViewForPosition(0); // if (view != null) { // measureChild(view, widthSpec, heightSpec); // int measuredWidth = View.MeasureSpec.getSize(widthSpec); // int measuredHeight = view.getMeasuredHeight(); // setMeasuredDimension(measuredWidth, measuredHeight * rowCount + mBorderWidth * (rowCount - 1)); // } } } 瀑布流的还没想过,希望大神们指点

    转载请注明原文地址: https://ju.6miu.com/read-1299031.html
    最新回复(0)