带有recyclerview的多行

时间:2017-05-03 15:37:20

标签: android

我从mySQL数据库获取足球队阵容数据。

我希望将守门员,后卫,中场球员作为每一行的标题显示回收者的球员数据。

我该怎么办?:

1 - 创建3个recyclerview?

2 - 创建3个自定义布局?

3种不同的观看者?

我做的是这个:

enter image description here

我想做的是: 同样的事情,但他们的球员和中场球员与他们的球员显示其他数据作为防守者

这是我的自定义布局(recycler_lineup):

    <?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/tools"
    android:orientation="horizontal">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardUseCompatPadding="true"
            >


            <RelativeLayout
                android:layoutDirection="ltr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp">


                <TextView
                    android:id="@+id/txt_competition"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="10dp"
                    android:layout_gravity="end"
                    android:textStyle="bold"
                    android:textSize="14sp"
                    />


                <View
                    android:id="@+id/view_1"
                    android:layout_below="@+id/txt_competition"
                    android:layout_width="match_parent"
                    android:layout_height="3dp"
                    android:layout_marginTop="3dp"
                    android:background="?android:attr/listDivider" />


                <TextView
                    android:textAlignment="center"
                    android:layout_gravity="start"
                    android:id="@+id/txt_id_player"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/view_1"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="8dp"
                    android:textColor="#666666"
                    android:text="1"
                    android:textSize="16sp" />

                <ImageView
                    android:id="@+id/image_player"
                    android:layout_width="35dp"
                    android:layout_height="45dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginEnd="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_below="@+id/view_1"
                    android:layout_toStartOf="@+id/txt_id_player"
                    android:layout_toLeftOf="@+id/txt_id_player"

                    />

                <TextView
                    android:textAlignment="center"
                    android:layout_gravity="start"
                    android:id="@+id/txt_name_player"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#666666"
                    android:text="Mahdi Hraybi"
                    android:textSize="13sp"
                    android:textStyle="bold"
                    android:layout_marginRight="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_below="@+id/view_1"
                    android:layout_toLeftOf="@+id/image_player"
                    android:layout_toStartOf="@+id/image_player" />

                <ImageView
                    android:id="@+id/image_flag_player"
                    android:layout_width="20dp"
                    android:layout_height="15dp"
                    android:background="@drawable/testing"
                    android:layout_below="@+id/txt_name_player"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="5dp"
                    android:scaleType="fitXY"
                    android:layout_toLeftOf="@+id/image_player"
                    android:layout_toStartOf="@+id/image_player" />

                <TextView
                    android:textAlignment="center"
                    android:layout_gravity="center"
                    android:id="@+id/txt_nationality_player"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#666666"
                    android:text="Ahed"
                    android:textSize="14sp"
                    android:layout_alignBaseline="@+id/txt_id_player"
                    android:layout_alignBottom="@+id/txt_id_player"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />





            </RelativeLayout>

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


    </ScrollView>
</LinearLayout>

包含recyclerview的布局:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

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


            </ScrollView>

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

    </RelativeLayout>



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

更新: 我想表现为:

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用标准的RecyclerView方式并定义多种视图类型。您需要在适配器中覆盖函数getItemViewType。此函数需要返回int,用于定义应为元素显示的类型。之后你只需在onCreateViewHolder中处理它。

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == TYPE1){
       //here you prepare ViewHolder for first view type
       return new ViewHolder1();
    }else if(viewType == TYPE2){
       //here you prepare ViewHolder for another view type
       return new ViewHolder2();
    } 
}