Android recycler查看divideritem 4.4兼容性

时间:2015-09-16 13:51:23

标签: android android-recyclerview

我在回收器视图中渲染分割器项目时只有for android 4.4,它在android 5.0中工作正常#

java.lang.NullPointerException
        at android.view.GLES20Canvas.setupModifiers(GLES20Canvas.java:1439)
        at android.view.GLES20Canvas.drawLines(GLES20Canvas.java:1019)
        at android.view.GLES20Canvas.drawLine(GLES20Canvas.java:1009)
        at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:538)
        at com.sistac.informecampos.fragments.DividerItemDecoration.onDrawOver(DividerItemDecoration.java:35)
        at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2444)
        at android.view.View.getDisplayList(View.java:13362)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1040)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.getDisplayList(View.java:13357)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.getDisplayList(View.java:13357)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.getDisplayList(View.java:13357)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1229)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.getDisplayList(View.java:13357)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.draw(View.java:14468)
        at android.widget.FrameLayout.draw(FrameLayout.java:472)
        at android.view.View.getDisplayList(View.java:13362)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.getDisplayList(View.java:13357)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.getDisplayList(View.java:13357)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.draw(View.java:14468)
        at android.view.View.getDisplayList(View.java:13362)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.View.draw(View.java:14182)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.draw(View.java:14468)
        at android.widget.FrameLayout.draw(FrameLayout.java:472)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326)
        at android.view.View.getDisplayList(View.java:13362)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
        at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2377)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2249)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1879)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:

我的DividerItem实现覆盖

 @Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<include layout="@layout/new_fab" />

非常简单。是否有任何建议的步骤来识别和修复这些错误?

谢谢, 乔治

2 个答案:

答案 0 :(得分:9)

这对我有帮助:从

更改divider xml
android:adjustViewBounds="true"

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <solid android:color="#00000000"/>
    <size android:height="1dp"/>
</shape>

实体更改为描边

答案 1 :(得分:0)

显然,缺少一些必要的形状属性。 在我自己的情况下,我不得不添加缺少的 stroke 属性:

<stroke
    android:width="1dp"
    android:color="@color/transparent"
    />