RecyclerView的宽度超过DeviceWidth并销毁TableLayout

时间:2017-11-15 19:24:15

标签: android android-layout android-recyclerview android-tablelayout

我在一个表(行)布局中有一个recylcerView,在NestedScrollView中,所有这些都将layout_width属性设置为match_parent。我遇到的问题是recyclelerView的滚动没有工作,但是当试图完成滚动(使用NestedScrollView)时,我发现这不是真正的问题。问题是我的水平recylcerView的宽度,扩展了设备的宽度。我只知道它,因为我的recyclerView上方的行没有显示正确的文本。

下图显示了RV和上面的textRow的行为 screenshot

左边的图像:在这里你可以看到上面文本行中的文字丢失了" h word"
标有2的图像:这是预期的输出。

我只实现了所期望的"正确的" size,如果我将recyclerview的layout_width设置为" 200dp"。但我不想要一个固定的尺寸(由于不同的设备)。

我尝试使用以下代码动态设置RV的layout_width但未成功:

LinearLayoutManager llm = new LinearLayoutManager(this.getContext(), LinearLayoutManager.HORIZONTAL, false);
    mRecyclerViewAttachments.setLayoutManager(llm);
    ViewGroup.LayoutParams params = mRecyclerViewAttachments.getLayoutParams();
    int newWidth = (int) (params.width * 0.9);
    mRecyclerViewAttachments.setLayoutParams(params);

任何人都可以帮我解决这个问题吗?下面是对话框的完整布局xml(RV最后出现):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
    android:id="@+id/detail_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:animateLayoutChanges="true">

    <TableLayout
        android:id="@+id/tableLayoutDetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">

        <!-- ROW 1 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_column="0"
                android:layout_span="2"
                android:text="@string/detail_fragment_entry_header_1"
                android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                tools:text="@string/detail_fragment_entry_header_1"
                android:background="@color/colorPrimary"/>

        </TableRow>

        <!-- ROW 2 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <ImageView
                android:src="@drawable/ic_event_black_24dp"
                android:layout_height="wrap_content"
                android:layout_width="48dp"
                android:layout_gravity="left"
                android:layout_column="0"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_column="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:textAppearance="@style/ChronlyTextViewHint"
                    android:text="@string/created_entry"
                    tools:text="@string/created_entry" />

                <TextView
                    android:id="@+id/textViewDate"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:text="@string/detail_dlg_phone_choose"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    tools:text="@string/detail_dlg_phone_choose" />
            </LinearLayout>
        </TableRow>

        <!-- ROW 3 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <ImageView
                android:id="@+id/imageViewEventType"
                android:src="@drawable/ic_contacts_black_24dp"
                android:layout_height="wrap_content"
                android:layout_width="48dp"
                android:layout_gravity="left"
                android:layout_column="0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_column="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:textAppearance="@style/ChronlyTextViewHint"
                    android:text="@string/detail_dlg_contact"
                    tools:text="@string/detail_dlg_contact" />

                <TextView
                    android:id="@+id/textViewContact"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:text="@string/detail_dlg_contact_desc"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    tools:text="@string/detail_dlg_contact_desc" />
            </LinearLayout>
        </TableRow>

        <!-- ROW 4 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorDarkGray"
                android:layout_column="0"
                android:layout_span="2">
            </LinearLayout>
        </TableRow>
        <!-- ROW 5 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="8dp"
            android:paddingTop="8dp">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_span="2">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/editTextComment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="top|left"
                    android:inputType="textMultiLine"
                    android:scrollbars="vertical|horizontal"
                    android:scrollHorizontally="false"
                    android:overScrollMode="always"
                    android:scrollbarStyle="insideInset"
                    android:hint="Enter a comment"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    tools:text="first word, second word, third word, fourth word, fifth word, sixth word, seventh word" />

            </android.support.design.widget.TextInputLayout>
        </TableRow>
        <!-- ROW 6 -->
        <TableRow
            android:id="@+id/rowDetailLabelAttachment"
            android:layout_width="match_parent"
            android:paddingRight="16dp"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_gravity="left"
                android:layout_span="2"
                android:textAppearance="@style/ChronlyTextViewHint"
                android:text="@string/detail_label_attachment"
                tools:text="Attachments"/>
        </TableRow>
        <!-- ROW 7 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <ImageView
                android:layout_width="48dp"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_attachment_black_24dp"
                android:layout_column="0"
                android:layout_gravity="left"/>
            <!-- https://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working/37338715#37338715 -->

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewAttachments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:scrollbars="horizontal"
                android:overScrollMode="always"
                android:scrollbarStyle="insideInset"
                android:nestedScrollingEnabled="false"
                android:visibility="visible"></android.support.v7.widget.RecyclerView>

        </TableRow>
    </TableLayout>
</android.support.v4.widget.NestedScrollView>

1 个答案:

答案 0 :(得分:2)

你的树应该是这样的

<NestedScrollView>

  <LinearLayout  orientation = vertical>

     <EditText>

     <TextView>

     <Recyclerview>

  </LinearLayout>

</NestedScrollView>