Scrollview 嵌套 RecyclerView 如何全部显示
Scrollview 嵌套 RecyclerView 如何全部显示
发布时间:2018-02-01 来源:查字典编辑
摘要:Scrollview嵌套RecyclerView如何全部显示重写了一个LinearLayoutManager和一个LinearLayoutM...

Scrollview 嵌套 RecyclerView 如何全部显示 

重写了一个LinearLayoutManager  和一个LinearLayoutManager  放到一起 然后加了10条,显示的数据只有4个。

这是重写的 LinearLayoutManager  

public class FullyLinearLayoutManager extends LinearLayoutManager {

    private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();

    public FullyLinearLayoutManager(Context context) {

        super(context);

    }

    public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {

        super(context, orientation, reverseLayout);

    }

    private int[] mMeasuredDimension = new int[2];

    @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);

        Log.i(TAG, "onMeasure called. nwidthMode " + widthMode

                + " nheightMode " + heightSpec

                + " nwidthSize " + widthSize

                + " nheightSize " + heightSize

                + " ngetItemCount() " + getItemCount());

        int width = 0;

        int height = 0;

        for (int i = 0; i <  getItemCount(); i++) {

            measureScrapChild(recycler, i,

                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),

                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),

                    mMeasuredDimension);

            if (getOrientation() == HORIZONTAL) {

                width = width + mMeasuredDimension[0];

                if (i == 0) {

                    height = mMeasuredDimension[1];

                }

            } else {

                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) {

        try {

            View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException

            if (view != null) {

                RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();

                int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,

                        getPaddingLeft() + getPaddingRight(), p.width);

                int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,

                        getPaddingTop() + getPaddingBottom(), p.height);

                view.measure(childWidthSpec, childHeightSpec);

                measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;

                measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;

                recycler.recycleView(view);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

        }

    }

}

然后   

 FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(mContext);

 mRecyclerView.setLayoutManager(linearLayoutManager);

这是重写的ScrollView 

public class MyScrollview extends ScrollView {

    private int downX;

    private int downY;

    private int mTouchSlop;

    public MyScrollview(Context context) {

        super(context);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    }

    public MyScrollview(Context context, AttributeSet attrs) {

        super(context, attrs);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    }

    public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    }

    @Override

    public boolean onInterceptTouchEvent(MotionEvent e) {

        int action = e.getAction();

        switch (action) {

            case MotionEvent.ACTION_DOWN:

                downX = (int) e.getRawX();

                downY = (int) e.getRawY();

                break;

            case MotionEvent.ACTION_MOVE:

                int moveY = (int) e.getRawY();

                if (Math.abs(moveY - downY)  > mTouchSlop) {

                    return true;

                }

        }

        return super.onInterceptTouchEvent(e);

    }

}

回复讨论(解决方案)

。。问题解决了。在RecyclerView  嵌套一层RelativeLayout < RelativeLayout

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:descendantFocusability="blocksDescendants" >

                < android.support.v7.widget.RecyclerView

                    android:id="@+id/recyclerView"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:layout_marginTop="@dimen/margin_10"

                    android:background="@color/color_white" / >

            < /RelativeLayout >

  LinearLayoutManager LinearLayoutManager = new LinearLayoutManager(this);

//        mRecyclerView.setNestedScrollingEnabled(false);

        mRecyclerView.setLayoutManager(LinearLayoutManager);

        mRecyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.HORIZONTAL,R.drawable.shape_divider_line));

        mAdapter = new CommonAdapter< String >(this,R.layout.item_check_detail,strings) {

            @Override

            protected void convert(ViewHolder holder, String s, int position) {

            }

        };

        mRecyclerView.setAdapter(mAdapter);

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类