单击按钮后,Recyclerview适配器中的项目跳到右侧

时间:2018-12-04 12:28:48

标签: android android-recyclerview scrollview

我有一个scrollview作为片段的根布局,在该scrollview中有一个linearlayout,其内部有一个constraintlayout和一个recyclerview,并且在recyclerview中有两列。当我的片段启动时,UI很好,但是当我单击一个按钮时,一列会跳向屏幕的右侧。我没有在左边或右边添加任何marginlayout,所以我不知道为什么会这样。以前没有这个问题,在使用scrollview之后我遇到了这个问题。我已经粘贴了下面的代码。关于单击按钮后列为何更改的任何想法。

就我而言,这是线性布局的问题,因为在我使用线性布局之前就可以了。

片段xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="scoutingFragmentViewModel"
            type="com.resatech.android.navigationtest.viewModels.ScoutingFragmentViewModel">

        </variable>

    </data>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:fadeScrollbars="false"
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:focusableInTouchMode="true">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraint_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <ImageView
                    android:id="@+id/image_bg"
                    android:layout_width="385dp"
                    android:layout_height="285dp"
                    android:src="@color/cpb_grey"
                    android:visibility="visible"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="1.0" />

                <TextView
                    android:id="@+id/text_scout_timer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:text="Scouting Timer"
                    android:textColor="@color/color_text_dark"
                    android:textSize="20sp"
                    android:visibility="@{scoutingFragmentViewModel.mScoutingTimerTextVisibility}"
                    app:layout_constraintBottom_toBottomOf="@+id/image_bg"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.474"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.335" />

                <Button
                    android:id="@+id/button_ready"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="92dp"
                    android:layout_marginEnd="30dp"
                    android:background="@drawable/connect_button"
                    android:text="Ready"
                    android:visibility="@{scoutingFragmentViewModel.mReadyButtonVisibility}"
                    app:layout_constraintEnd_toEndOf="@+id/image_bg"
                    app:layout_constraintHorizontal_bias="0.484"
                    app:layout_constraintStart_toStartOf="@+id/image_bg"
                    app:layout_constraintTop_toTopOf="parent" />

                <Button
                    android:id="@+id/button_stop_scouting"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:background="@drawable/connect_button"
                    android:text="Stop Scouting"
                    android:visibility="@{scoutingFragmentViewModel.mStopScoutingButtonVisibility}"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.31" />

                <Button
                    android:id="@+id/button_connect"
                    android:layout_width="120dp"
                    android:layout_height="40dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="88dp"
                    android:layout_marginEnd="8dp"
                    android:background="@drawable/connect_button"
                    android:shadowColor="#A84F4F"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="0"
                    android:text="Connect"
                    android:textColor="#140533"
                    android:textSize="18sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.443"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </android.support.constraint.ConstraintLayout>


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

            </android.support.v7.widget.RecyclerView>


        </LinearLayout>
    </ScrollView>

</layout>

Itemlist xml:

<?xml version="1.0" encoding="utf-8"?>
    <layout>

        <data>

            <variable
                name="itemGridViewModel"
                type="com.resatech.android.navigationtest.viewModels.ItemGridViewAdapterViewModel" />
        </data>


        <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/layout_border"
            android:maxWidth="150dp"
            android:maxHeight="150dp"
            android:layout_marginTop="20dp"

            android:paddingVertical="10dp"
            app:cardBackgroundColor="#ffffff">


            <android.support.constraint.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">


                <TextView
                    android:id="@+id/big_text"
                    android:layout_width="60dp"
                    android:layout_height="69dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="21dp"
                    android:layout_marginBottom="16dp"
                    android:text="@{itemGridViewModel.values}"
                    android:textSize="50sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.51"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.526" />

                <TextView
                    android:id="@+id/small_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="21dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="1dp"
                    android:text="@{itemGridViewModel.description}"
                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.468"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/big_text" />

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="23dp"
                    android:layout_height="22dp"
                    android:layout_marginEnd="50dp"
                    android:layout_marginBottom="50dp"
                    app:imageResource="@{itemGridViewModel.imageNames}"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.05"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.077" />
            </android.support.constraint.ConstraintLayout>

        </android.support.v7.widget.CardView>

    </layout>

显示

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试将android:clickable =“ true”这行添加到ScrollView

android:clickable="true"

尝试使用Recyclerview并设置GridLayoutManger并以这种方式定义列。

        recyclerview.setLayoutManager(new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false));