Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 每个项目超过一行的RecyclerView不';充气分隔器_Android_Android Recyclerview - Fatal编程技术网

Android 每个项目超过一行的RecyclerView不';充气分隔器

Android 每个项目超过一行的RecyclerView不';充气分隔器,android,android-recyclerview,Android,Android Recyclerview,我到处都有RecyclerView,我发现当列表项只有一行时,DividerItemDecoration工作得很好,但当它有两行时,它就不显示了 我在这里充气: CenaListRecAdapter mAdapter = new CenaListRecAdapter(getApplicationContext(), cenaLokalList, point); recyclerView.setAdapter(mAdapter); recyclerView.addItemDecoration(ne

我到处都有RecyclerView,我发现当列表项只有一行时,
DividerItemDecoration
工作得很好,但当它有两行时,它就不显示了

我在这里充气:

CenaListRecAdapter mAdapter = new CenaListRecAdapter(getApplicationContext(), cenaLokalList, point);
recyclerView.setAdapter(mAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL_LIST));
和标准
DividerItemDecoration
class:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = new int[]{
        android.R.attr.listDivider
};

public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

private Drawable mDivider;

private int mOrientation;

public DividerItemDecoration(Context context, int orientation) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    setOrientation(orientation);
}

public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
        throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
        drawVertical(c, parent);
    } else {
        drawHorizontal(c, parent);
    }
}

public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int top = child.getBottom() + params.bottomMargin;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}
public类DividerItemDecoration扩展了RecyclerView.ItemDecoration{
私有静态最终整数[]属性=新整数[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST=LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST=LinearLayoutManager.VERTICAL;
专用可抽出式mDivider;
私人托管;
公共DividerItemDecoration(上下文,int方向){
最终类型Darray a=上下文。获取样式属性(属性);
mDivider=a.getDrawable(0);
a、 回收();
定向(定向);
}
公共方向(整数方向){
如果(方向!=水平列表和方向!=垂直列表){
抛出新的IllegalArgumentException(“无效方向”);
}
朝气=方向;
}
@凌驾
公共void onDraw(画布c、回收视图父对象){
if(morentation==垂直列表){
(c)父母;
}否则{
水平拉伸(c,母材);
}
}
公共void drawVertical(画布c、回收视图父对象){
final int left=parent.getPaddingLeft();
final int right=parent.getWidth()-parent.getPaddingRight();
final int childCount=parent.getChildCount();
for(int i=0;i
}

编辑:这就是我的行视图项目的外观:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/imageView"
        android:transitionName="obrazek"
        android:layout_marginRight="5dp" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="?android:attr/selectableItemBackground">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/nazwa"
            android:maxLines="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/odleglosc"
            android:layout_gravity="right"
            android:gravity="end"
            android:layout_below="@+id/nazwa"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text=" "
            android:id="@+id/cena"
            android:layout_below="@+id/nazwa"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>
</LinearLayout>`

`

这可能会对官方文档有所帮助:

所有项目装饰都是按照添加顺序绘制的,在 项目视图,在
item装饰#onDraw()中以及
item装饰#onDrawOver(Canvas,RecyclerView,RecyclerView.State)中的项目之后


您应该重写onDrawOver方法。

好的,我自己找到了答案

如果您在Activity(非Fragment)中使用RecyclerView,那么您的
DividerItemDecoration
上下文应该是
,而不是
getApplicationContext()
-您在匿名类中使用它-在Fragment类中应该使用
getActivity()


哦,最好在
LayoutManager
之后设置装饰,然后再设置适配器,即使列表是空的。我想它就是这样工作的。

我试过了,效果是一样的,但我覆盖了
public void onDrawOver(Canvas c,RecyclerView parent)
我刚在这里试过,效果很好。你确定你做的每件事都正确吗?也许会重建这个项目,或者考虑更新你在V7:22.2.0上建立的ReaveReVIEW图书馆的版本——这可能是原因吗?我看到你的答案了。我显然忽略了
getApplicationContext
部分。是的,当您使用任何UI组件时,您应该始终使用UI上下文。