如何在一个xml布局android中同时使用滚动视图和RecyclerView? [英] how do i use both scroll view and RecyclerView in one xml layout android?

查看:83
本文介绍了如何在一个xml布局android中同时使用滚动视图和RecyclerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的代码:

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

推荐答案

您需要根据您拥有多少物品来动态更改recyclerview的高度.

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;

滚动视图会阻止recyclerview更改其在xml中设置的高度,这就是为什么您需要以编程方式进行操作的原因.

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

这篇关于如何在一个xml布局android中同时使用滚动视图和RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆