【问题标题】:how do i use both scroll view and RecyclerView in one xml layout android?如何在一个 xml 布局 android 中同时使用滚动视图和 RecyclerView?
【发布时间】:2015-06-06 05:58:18
【问题描述】:

在我的应用程序中,我在一个布局中使用回收器视图和滚动视图。当我同时使用这两个视图时它不起作用。如果我删除滚动视图,则只有回收器视图正在工作。请任何人帮我解决这个问题。

我的代码:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/r1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        >
       <ScrollView
            android:id="@+id/sc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#1e356a">

                    <TextView
                        android:id="@+id/placce_head"
                        android:layout_width="wrap_content"
                        android:layout_height="25dp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        android:text="Hyderabad to banglore"
                        android:textColor="#ffffff"
                        android:textSize="20dp" />



                </RelativeLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#78869c"
                    android:weightSum="2"

                    android:orientation="horizontal">


                    <TextView
                        android:id="@+id/seats"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="10dp"
                        android:text="Selected Seats"
                        android:textColor="#ffffff"
                        android:textSize="16dp" />
                    <TextView
                        android:id="@+id/totalamount"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="10dp"
                        android:text="Total Amount"
                        android:textColor="#ffffff"
                        android:textSize="16dp" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#78869c"
                    android:weightSum="2"
                    android:orientation="horizontal">


                    <TextView
                        android:id="@+id/seat_num"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="20dp"

                        android:textColor="#ffffff"
                        android:textSize="16dp" />
                    <TextView
                        android:id="@+id/total_amount"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_marginLeft="10dp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"


                        android:textColor="#ffffff"
                        android:textSize="16dp" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <EditText
                        android:id="@+id/boardingpoint_edttxt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusableInTouchMode="false"
                        android:layout_marginLeft="30dp"
                        android:layout_marginTop="22dp"
                        android:background="@null"
                        android:hint=" Select Boarding Point"
                        android:textColorHint="#1e365a"
                        android:textSize="15dp" />

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="0.8dp"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="25dp"
                        android:layout_marginTop="7dp"
                        android:background="#1e365a" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#78869c"
                    android:layout_marginTop="20dp">

                    <TextView
                        android:id="@+id/contact_details"
                        android:layout_width="wrap_content"
                        android:layout_height="25dp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        android:text="Contact Details"
                        android:textColor="#ffffff"
                        android:textSize="20dp" />



                </RelativeLayout>
                    <EditText
                        android:id="@+id/contactname_edt_txt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginLeft="30dp"
                        android:layout_marginTop="22dp"
                        android:background="@null"
                        android:hint="Contact Name"
                        android:textColorHint="#1e365a"
                        android:textSize="15dp" />

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="0.8dp"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="25dp"
                        android:layout_marginTop="7dp"
                        android:background="#1e365a" />
                    <EditText
                        android:id="@+id/email_edt_txt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginLeft="30dp"
                        android:layout_marginTop="22dp"
                        android:background="@null"
                        android:hint="Email Address"
                        android:textColorHint="#1e365a"
                        android:textSize="15dp" />

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="0.8dp"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="25dp"
                        android:layout_marginTop="7dp"
                        android:background="#1e365a" />
                    <EditText
                        android:id="@+id/contactnum_edt_txt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginLeft="30dp"
                        android:layout_marginTop="22dp"
                        android:background="@null"
                        android:hint="Contact Number"
                        android:textColorHint="#1e365a"
                        android:textSize="15dp" />

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="0.8dp"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="25dp"
                        android:layout_marginTop="7dp"
                        android:background="#1e365a" />
                    <EditText
                        android:id="@+id/emergency_edt_txt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginLeft="30dp"
                        android:layout_marginTop="22dp"
                        android:background="@null"
                        android:hint="Emergency Contact Number"
                        android:textColorHint="#1e365a"
                        android:textSize="15dp" />

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="0.8dp"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="25dp"
                        android:layout_marginTop="7dp"
                        android:background="#1e365a" />
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#78869c"
                        android:layout_marginTop="20dp">

                        <TextView
                            android:id="@+id/passenger_details"
                            android:layout_width="wrap_content"
                            android:layout_height="25dp"
                            android:layout_centerHorizontal="true"
                            android:layout_centerVertical="true"
                            android:layout_marginTop="10dp"
                            android:layout_marginBottom="10dp"
                            android:text="Passenger Details"
                            android:textColor="#ffffff"
                            android:textSize="20dp" />



                    </RelativeLayout>

                </LinearLayout>
 <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/view"
            android:layout_below="@+id/sc"


            android:layout_alignParentBottom="true"

            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />



            <LinearLayout
                    android:id="@+id/coupon_lay"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="12dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:weightSum="100" >

                    <EditText
                        android:id="@+id/entercouponcode_edt"


                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:layout_weight="50"
                        android:hint="Enter coupn code"
                        android:inputType="text" >

                        <requestFocus />
                    </EditText>

                    <Button
                        android:id="@+id/bt_apply"
                        style="@style/payment_button_style"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:layout_marginLeft="-5dp"
                        android:layout_weight="40"
                        android:gravity="center"
                        android:text="Apply" />
                </LinearLayout>

                <Button
                    android:id="@+id/pay_btn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="28dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="20dp"
                    android:background="#F93249"
                    android:duplicateParentState="true"
                    android:gravity="center"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:text="Proceed to Pay"
                    android:textColor="#ffffff"
                    android:textSize="20dp"/>







            </LinearLayout>
        </ScrollView>





    </RelativeLayout>

【问题讨论】:

  • 不建议嵌套滚动视图和列表视图/回收器视图,你想做什么
  • 我在滚动视图中有很多小部件。在回收站视图中,我动态添加布局。
  • 回收站视图会为你创建滚动条,你不需要滚动视图
  • @r7v ...我的要求是在滚动视图中我需要添加一些小部件,然后我需要添加到回收器视图中,,,,在那个回收器视图中......我会添加更多动态布局......所以回收器视图不是父级......它需要进入中间......用于动态添加布局
  • 任何人帮助我....请

标签: android android-xml android-scrollview android-recyclerview


【解决方案1】:

你必须知道,你的scrollview属性设置为MatchParent,这意味着它将填充内容视图,所以我建议将Height设置为一个真实的值,例如100dp,它会起作用

【讨论】:

  • 如果我将高度设置为 100,则 scoolview 中缺少小部件
  • 在scrollview中,如果它包含任何childview,它可以滚动,你可以滚动找到它的孩子
  • 对不起,我刚才没看清楚你的代码,你的recyleview在scrollview中,你的touch会被scrollview捕捉到,你的recyleview无法响应事件,
  • 也许你需要重写一个扩展视图组的控件,或者搜索一些可以替换滚动视图的控件
【解决方案2】:

您需要根据您拥有的项目数量动态更改回收站视图的高度。

int recyclerheight = height_per_item * adapterData.size();
recyclerView.getLayoutParams().height = recyclerheight;

滚动视图阻止 recyclerview 更改其在 xml 中设置的高度,这就是您需要以编程方式执行此操作的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-03
    • 2018-05-12
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多