1

私はそれをラップするためにRecyclerView親に割り当てるために、の全高を取得しようとしています。Dialog

私はそれを得るためにこの方法を試みます:

private int getTotalHeightOfRecyclerView(RecyclerView recyclerView){

    int totalHeight         = 0;

    int totalCount          = recyclerView.getAdapter().getItemCount();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentview        = inflater.inflate(R.layout.recyclerview_custom_category_parent_row, null, false);
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    int height              = contentview.getMeasuredHeight();
    int dividerHeight       = Utils.dpToPx(1, mContext);

    for (int i = 0; i < totalCount; i++) {
        totalHeight += height+dividerHeight;
    }

    totalHeight             = totalHeight-dividerHeight;
    return totalHeight;
}

ただし、一部のデバイスでは機能し、他の一部のデバイスでは例外になります。

java.lang.NullPointerException at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:493) at android.view.View.measure(View.java:15630)

xml コンテンツは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/category_parent_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="@dimen/activity_vertical_half_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_half_margin"
        android:layout_marginRight="@dimen/activity_horizontal_half_margin"
        android:layout_marginTop="@dimen/activity_vertical_half_margin"
        android:contentDescription="@string/image_description" />

    <com.wisgoon.android.custom.WisgoonRegularTextView
        android:id="@+id/category_parent_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginEnd="@dimen/activity_horizontal_half_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_toLeftOf="@+id/category_parent_image"
        android:layout_toStartOf="@+id/category_parent_image"
        android:gravity="right"
        android:lines="1"
        android:textAppearance="@style/Regular"
        android:textColor="#FF757575"
        android:textSize="11sp" />

    <ImageView
        android:id="@+id/category_parent_toggle"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:src="@drawable/arrow"
        android:layout_marginBottom="@dimen/activity_vertical_half_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_half_margin"
        android:layout_marginStart="@dimen/activity_horizontal_half_margin"
        android:layout_marginTop="@dimen/activity_vertical_half_margin"
        android:contentDescription="@string/image_description" />

</RelativeLayout>

どこが間違っていますか?

4

1 に答える 1