2017-08-09 35 views
0

我正在垂直滚动视图中加载水平recyclerView。 Horizo​​ntal recyclerView项目应该适合屏幕并可滚动。如何在android中将水平recyclerView可滚动项放入屏幕?

XMl为nestedscrollView和horizo​​ntal recyclerView项目中的垂直recyclerView添加。

代码:

void bind(final Element element, final int position) { 
      myCustomTextViewElementName.setText(element.getElementName() + "/" + element.getPriority()); 
      myCustomTextViewElementName.setTextColor(ContextCompat.getColorStateList(mContext, R.color.text_color)); 

     } 

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:customfontdemo="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/color_white" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <include layout="@layout/toolbar" /> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     android:orientation="vertical"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/main.appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/light_grey_color" 
      android:fitsSystemWindows="true"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsingToolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:fitsSystemWindows="true" 
       app:contentScrim="@color/light_grey_color" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed" 
       app:title=""> 

       <LinearLayout 
        android:id="@+id/patient_profile_layout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@color/light_grey_color" 
        android:orientation="horizontal" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="@dimen/margin_16dp" 
         android:layout_marginRight="@dimen/margin_16dp" 
         android:layout_marginTop="@dimen/margin_16dp" 
         android:orientation="horizontal"> 

         <com.mikhaellopez.circularimageview.CircularImageView 
          android:id="@+id/circularImageView" 
          android:layout_width="@dimen/image_size" 
          android:layout_height="@dimen/image_size" 
          android:src="@drawable/default_avatar" 
          app:civ_border="true" 
          app:civ_border_color="#3f51b5" 
          app:civ_border_width="1dp" 
          app:civ_shadow="true" 
          app:civ_shadow_color="#3f51b5" /> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="@dimen/margin_8dp" 
          android:orientation="vertical"> 

          <MyCustomTextView 
           android:id="@+id/text_view_patient_name_with_emr_number" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:textColor="@color/black_54_percent" 
           android:textSize="@dimen/text_size_14dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 

          <MyCustomTextView 
           android:id="@+id/text_view_patient_age" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Age - 32 | Female | Married " 
           android:textColor="@color/black_color_87_percent" 
           android:textSize="@dimen/text_size_12dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 

          <MyCustomTextView 
           android:id="@+id/text_view_patient_id" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Patient ID - " 
           android:textColor="@color/black_color_87_percent" 
           android:textSize="@dimen/text_size_12dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 

          <MyCustomTextView 
           android:id="@+id/text_view_hospital_name" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginTop="@dimen/margin_12dp" 
           android:text="Corporate- Paramount Health Services(P)" 
           android:textColor="@color/black_color_87_percent" 
           android:textSize="@dimen/text_size_12dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 

          <MyCustomTextView 
           android:id="@+id/text_view_consultant_name" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Consultant Name- Rachan" 
           android:textColor="@color/black_color_87_percent" 
           android:textSize="@dimen/text_size_12dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 

          <MyCustomTextView 
           android:id="@+id/text_view_department_name" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Department- Medical Oncology" 
           android:textColor="@color/black_color_87_percent" 
           android:textSize="@dimen/text_size_12dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 

          <MyCustomTextView 
           android:id="@+id/text_view_date_of_admission" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginBottom="@dimen/text_size_12dp" 
           android:layout_marginTop="@dimen/text_size_12dp" 
           android:text="Date of Admission- 5 May 17" 
           android:textColor="@color/black_color_87_percent" 
           android:textSize="@dimen/text_size_12dp" 
           customfontdemo:fontName="@string/font_family_roboto_regular" /> 


         </LinearLayout> 
        </LinearLayout> 
       </LinearLayout> 
      </android.support.design.widget.CollapsingToolbarLayout> 
     </android.support.design.widget.AppBarLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <MyCustomTextView 
       android:id="@+id/text_view_today_date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_gravity="center" 
       android:layout_marginTop="@dimen/margin_16dp" 
       android:background="@drawable/round_corner_btn" 
       android:gravity="center" 
       android:paddingBottom="@dimen/margin_4dp" 
       android:paddingLeft="@dimen/margin_28dp" 
       android:paddingRight="@dimen/margin_28dp" 
       android:paddingTop="@dimen/margin_4dp" 
       android:text="Today" 
       android:visibility="gone" 
       android:textColor="@color/color_white" 
       android:textSize="@dimen/text_size_12dp" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
       customfontdemo:fontName="@string/font_family_roboto_regular" /> 

      <android.support.v4.widget.NestedScrollView 
       android:id="@+id/nested_scroll_view" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/text_view_today_date" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

       <RelativeLayout 
        android:id="@+id/relative_layout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="@dimen/margin_36dp" 
        android:orientation="vertical" 
        android:visibility="gone" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

        <android.support.v7.widget.RecyclerView 
         android:id="@+id/recyclerView" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:scrollbars="vertical" /> 

        <Button 
         android:id="@+id/btn_submit" 
         android:layout_width="180dp" 
         android:layout_height="45dp" 
         android:layout_below="@+id/recyclerView" 
         android:layout_centerHorizontal="true" 
         android:layout_gravity="center" 
         android:layout_marginBottom="@dimen/margin_54dp" 
         android:layout_marginTop="@dimen/margin_16dp" 
         android:background="@color/colorPrimaryDark" 
         android:gravity="center" 
         android:text="Submit" 
         android:textAllCaps="false" 
         android:textColor="@android:color/white" /> 
       </RelativeLayout> 
      </android.support.v4.widget.NestedScrollView> 
     </RelativeLayout> 
    </android.support.design.widget.CoordinatorLayout> 
</LinearLayout> 

水平RecyclerView项目

水平recyclerView每个垂直recyclerView项目

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:customfontdemo="http://schemas.android.com/apk/res-auto" 
    style="@style/AppTheme.Card.Margins" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white"> 

    <LinearLayout 
     android:id="@+id/whole_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:orientation="vertical"> 

     <MyCustomTextView 
      android:id="@+id/text_view_section_list_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/margin_8dp" 
      android:text="@string/text_cheif_complains" 
      android:textColor="@color/black_color_20_percent" 
      android:textSize="@dimen/text_size_14dp" 
      customfontdemo:fontName="@string/font_family_roboto_regular" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/element_recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="@dimen/margin_12dp" /> 

     <MyCustomTextView 
      android:id="@+id/remaining_element_count" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_marginLeft="@dimen/margin_4dp" 
      android:layout_marginRight="@dimen/margin_12dp" 
      android:layout_marginTop="@dimen/margin_5dp" 
      android:gravity="left" 
      android:textColor="@android:color/holo_red_dark" 
      android:textSize="@dimen/text_size_14dp" 
      android:visibility="gone" 
      customfontdemo:fontName="@string/font_family_roboto_regular" /> 

     <MyCustomTextView 
      android:id="@+id/text_view_add_emr" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/margin_40dp" 
      android:layout_gravity="center" 
      android:layout_marginBottom="@dimen/margin_16dp" 
      android:layout_marginLeft="@dimen/margin_8dp" 
      android:gravity="center|left" 
      android:text="@string/text_add_more" 
      android:textColor="@color/colorPrimaryDark" 
      android:textSize="@dimen/text_size_14dp" 
      customfontdemo:fontName="@string/font_family_roboto_regular" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_marginBottom="@dimen/margin_8dp" 
      android:layout_marginLeft="@dimen/margin_8dp" 
      android:layout_marginRight="@dimen/margin_8dp" 
      android:layout_marginTop="@dimen/margin_12dp" 
      android:background="@color/black_color_20_percent" 
      android:visibility="gone" /> 

    </LinearLayout> 

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

enter image description here

+0

@karolinap可以请你分享一些代码吗? –

回答

0

尝试布局重量1的每个元素和宽度= 0和POST XML代码。

+0

添加了XML请检查 –

+0

对于您的自定义文本视图,而不是使用宽度包装内容,对所有自定义文本视图使用宽度= 0和权重= 1。所以所有元素在屏幕上的大小都相同。 –

+0

不,它不工作 –