how do i use both scroll view and RecyclerView in

2019-05-21 02:53发布

问题:

In my application i am using recycler view and scrollview in one layout. it is not working when i am using both these views. If i remove scroll view only the recycler view is working. Plese any one help me hoe to fix this issue.

my code:

 <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>

回答1:

you must know that ,your scrollview property is set as MatchParent ,which means it will fill the content view, so i suggest to set Height a real value such as 100dp, and it will work



回答2:

You need to dynamically change the height of the recyclerview, depending on how many items you have.

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

The scroll view blocks the recyclerview from changing its height set in the xml, that's why you need to do it programically.