2016-05-11 5 views
0

Я создал представление recyclerview, как вы можете видеть ниже, и я хочу добавить несколько объектов в строку. Кто-нибудь может мне помочь?? row_layout:Более одного объекта в строке recyclerview

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/cardView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginBottom="@dimen/activity_vertical_margin" 
app:cardCornerRadius="@dimen/activity_vertical_margin" 
app:cardElevation="@dimen/activity_vertical_margin"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="16dp"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="16dp"/> 
</RelativeLayout> 

основной макет:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/collapsing_toolbar" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:expandedTitleMarginStart="60dp"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="255dp" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context=".MainActivity" 
     android:id="@+id/collapsing_toolbar_layout" 
     android:background="@drawable/header1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:fitsSystemWindows="true" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleMarginEnd="64dp"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:background="#00000000" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:layout_collapseMode="pin" 
      android:transitionGroup="false"> 

     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
</android.support.v7.widget.RecyclerView> 

выглядит следующим образом: https://drive.google.com/open?id=0ByeD1eD5C-v_aXkwQUlKS2w1dUU

+0

http://developer.android.com/training/material/lists-cards.html – Pehlaj

ответ

0
mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2)); 

добавить itemDecoration к вашему RecyclerView, если вы хотите некоторое пространство между ним:

Создать новый класс itemDecoration:

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { 
     private int space; 

     public SpacesItemDecoration(int space) { 
     this.space = space; 
     } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, 
     RecyclerView parent, RecyclerView.State state) { 
    outRect.left = space; 
    outRect.right = space; 
    outRect.bottom = space; 

    // Add top margin only for the first item to avoid double space between items 
    if (parent.getChildLayoutPosition(view) == 0) { 
     outRect.top = space; 
    } else { 
     outRect.top = 0; 
    } 
    } 
} 

затем добавить к вашей основной:

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view); 
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing); 
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels)); 

надеюсь, что это может помочь вам :)

0

Вы можете установить менеджер компоновки вашего переработчика с к GridLayoutManager. Вы можете определить, сколько элементов в строке вы хотите. Смотрите ниже:

recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); // 2 items per row 

Вот некоторые примеры:

http://www.sitepoint.com/mastering-complex-lists-with-the-android-recyclerview/ http://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/

0

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:background="#ffffff" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffffff" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

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


      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 

       android:background="#e01e38"> 

       <TextView 
        android:id="@+id/text_username" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_centerVertical="true" 
        android:textColor="#ffffff" 
        android:textSize="@dimen/text_big" /> 

       <ImageView 
        android:id="@+id/img_setting" 
        android:layout_width="35dp" 
        android:layout_height="35dp" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="10dp" 
        android:src="@drawable/icon_seting" /> 


      </RelativeLayout> 


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

       <ImageView 
        android:id="@+id/image_cover" 
        android:layout_width="match_parent" 
        android:layout_height="700dp" 
        android:background="@drawable/profile_header" 
        android:scaleType="centerCrop" 
        app:layout_collapseMode="parallax" /> 


       <RelativeLayout 
        android:layout_width="match_parent" 

        android:layout_height="match_parent"> 

        <TextView 
         android:id="@+id/text_weblink" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentLeft="true" 
         android:layout_marginBottom="20dp" 
         android:layout_marginLeft="@dimen/Profile_margin_left" 
         android:text="Weblink" 
         android:textColor="#e01e38" 
         android:textSize="15dp" /> 
        <!--<TextView--> 
        <!--android:layout_width="wrap_content"--> 
        <!--android:layout_height="wrap_content"--> 
        <!--android:text="sjkfsdokfhsdlfs"--> 
        <!--android:layout_centerHorizontal="true"--> 
        <!--android:layout_marginTop="10dp"--> 
        <!--android:textSize="20dp"/>--> 
        <!--<TextView--> 
        <!--android:layout_width="wrap_content"--> 
        <!--android:layout_height="wrap_content"--> 
        <!--android:text="sjkfsdokfhsdlfs"--> 
        <!--android:layout_centerHorizontal="true"--> 
        <!--android:layout_marginTop="10dp"--> 
        <!--android:textSize="20dp"/>--> 
        <!--<TextView--> 
        <!--android:layout_width="wrap_content"--> 
        <!--android:layout_height="wrap_content"--> 
        <!--android:text="sjkfsdokfhsdlfs"--> 
        <!--android:layout_centerHorizontal="true"--> 
        <!--android:layout_marginTop="10dp"--> 
        <!--android:textSize="20dp"/>--> 

        <!--<TextView--> 
        <!--android:layout_width="wrap_content"--> 
        <!--android:layout_height="wrap_content"--> 
        <!--android:text="sjkfsdokfhsdlfs"--> 
        <!--android:layout_centerHorizontal="true"--> 
        <!--android:layout_marginTop="10dp"--> 
        <!--android:textSize="20dp"/>--> 

        <com.elite.watchme.commonFunctionalityController.CircularImageView 
         android:id="@+id/imageView" 
         android:layout_width="@dimen/Profile_image_width" 
         android:layout_height="@dimen/Profile_Image_hieght" 
         android:layout_alignParentBottom="true" 
         android:layout_centerHorizontal="true" 
         android:src="@drawable/img_profile" /> 


        <RelativeLayout 
         android:id="@+id/following_container" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentStart="true" 
         android:layout_alignTop="@+id/imageView" 
         android:layout_marginLeft="@dimen/Profile_margin_left"> 


         <TextView 
          android:id="@+id/textView_following_count" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#ffffff" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 


         <TextView 
          android:id="@+id/textView_left_following" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/textView_following_count" 
          android:text="Following" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#ffffff" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 


        </RelativeLayout> 


        <RelativeLayout 
         android:id="@+id/rescope_contaner" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentEnd="true" 
         android:layout_alignParentRight="true" 
         android:layout_alignTop="@+id/imageView" 
         android:layout_marginRight="@dimen/Profile_margin_right"> 


         <TextView 
          android:id="@+id/textView_Rescope_count" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#ffffff" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 


         <TextView 
          android:id="@+id/textView" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/textView_Rescope_count" 
          android:text="Rescopes" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#ffffff" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 


        </RelativeLayout> 


        <RelativeLayout 
         android:id="@+id/following_contaner" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentTop="true" 
         android:layout_centerHorizontal="true" 
         android:layout_marginTop="@dimen/Profile_margin_top"> 


         <TextView 
          android:id="@+id/textView_followers_count" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#ffffff" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 


         <TextView 
          android:id="@+id/textView_follower" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/textView_followers_count" 
          android:text="Followers" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#ffffff" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 


        </RelativeLayout> 


       </RelativeLayout> 


      </RelativeLayout> 
     </LinearLayout> 


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

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

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffe5e5e5" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/ColorWhite" 
     android:orientation="vertical"> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#d2d2d2" 
      android:padding="5dp"> 


      <TextView 
       android:id="@+id/text_bio" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:textSize="15sp" /> 

     </RelativeLayout> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="130dp" 
      android:background="#ffffff" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp"> 

      <TextView 
       android:id="@+id/text_top_five" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Top 5 Post" 
       android:textColor="#e01e38" 
       android:textSize="15sp" /> 


      <!--<com.elite.watchme.commonFunctionalityController.HorizontalListView--> 

      <!--android:layout_width="match_parent"--> 
      <!--android:layout_height="match_parent"--> 


      <!--app:dividerWidth="10dp" />--> 


      <!--<android.support.v7.widget.RecyclerView--> 
      <!--android:id="@+id/Horizontal_profile_Images"--> 
      <!--android:layout_width="match_parent"--> 
      <!--android:layout_height="match_parent"--> 
      <!--android:layout_below="@+id/text_top_five"--> 
      <!--android:orientation="horizontal"--> 
      <!--android:paddingTop="5dp"--> 
      <!--app:layoutManager="android.support.v7.widget.LinearLayoutManager" />--> 


      <android.support.v7.widget.RecyclerView 
       android:id="@+id/Horizontal_profile_Images" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/text_top_five" 
       android:paddingTop="5dp" 
       android:scrollbars="vertical"></android.support.v7.widget.RecyclerView> 


     </RelativeLayout> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="600dp" 
      android:background="@color/ColorWhite" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp"> 


      <TextView 
       android:id="@+id/text_recent" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Recent" 
       android:textColor="#e01e38" 
       android:textSize="15sp" /> 

      <!--<GridView--> 
      <!--android:id="@+id/profile_grid_images"--> 
      <!--android:layout_width="match_parent"--> 
      <!--android:layout_height="match_parent"--> 
      <!--android:layout_below="@+id/text_recent"--> 
      <!--android:clickable="true"--> 
      <!--android:columnWidth="100dp"--> 
      <!--android:drawSelectorOnTop="true"--> 
      <!--android:focusable="true"--> 
      <!--android:gravity="center"--> 
      <!--android:numColumns="auto_fit"--> 
      <!--android:paddingTop="5dp"--> 
      <!--android:stretchMode="columnWidth"--> 
      <!--android:verticalSpacing="10dp">--> 


      <!--</GridView>--> 


      <android.support.v7.widget.RecyclerView 
       android:id="@+id/profile_grid_images" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/text_recent" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 


     </RelativeLayout> 
    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

Смежные вопросы