简体   繁体   English

recyclerview行中有多个对象

[英]More than one object in recyclerview line

I have created a recyclerview view as you can see below and i want to add more than one object in a line. 我创建了一个recyclerview视图,如下图所示,我想在一行中添加多个对象。 Can someone help me?? 有人能帮我吗?? row_layout: row_layout:

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

main layout: 主要布局:

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

looks like: https://drive.google.com/open?id=0ByeD1eD5C-v_aXkwQUlKS2w1dUU 看起来像: https : //drive.google.com/open?id=0ByeD1eD5C-v_aXkwQUlKS2w1dUU

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

add a itemDecoration to your RecyclerView if you want some space between it: 如果需要在itemDecoration之间添加一些空间,请在其之间添加一些空间:

Create a new class called itemDecoration: 创建一个名为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;
    }
  }
}

then add this to your main: 然后将此添加到您的主要:

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

hope this could help you :) 希望这可以对您有所帮助:)

You can set the layout manager of your recycler with to a GridLayoutManager . 您可以将回收站的布局管理器设置为GridLayoutManager You can define how many items per row you want. 您可以定义每行要多少个项目。 See below : 见下文 :

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

Here is some examples : 这是一些例子:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向RecyclerView Adapter添加多个对象 - Add more than one object to RecyclerView Adapter 有没有办法用多个ViewHolder对象填充RecyclerView行? 适配器应该实现什么? - Is there a way to populate a RecyclerView row with more than one ViewHolder object? What should the adapter implement? 在viewpager的一页中集成了多个recyclerview - more than one recyclerview integrated in one page of a viewpager RecyclerView - 我不能添加超过 1 行 - RecyclerView - I can't add more than 1 line 超过 1 个回收站的 SearchView - SearchView for more than 1 recyclerview 多个元素时共享元素事务无法与recyclerview一起使用 - shared element transaction not working with recyclerview when more than one element RecyclerView可以为每个项目实例化多个“视图”吗? - Can a RecyclerView instantiate more than one 'view' per item? 如何在RecyclerView中使用光标适配器制作一张以上的卡? - How to make more than one card in RecyclerView that uses cursor adapter? 在Android中向下滚动时,在recyclerview中选择了多个项目 - More than one item in recyclerview is selected when scrolling down in Android 具有多个视图的 Recyclerview,删除 addTextChangedListener() 不起作用 - Recyclerview with more than one views, Removing of addTextChangedListener() not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM