2017-05-03 30 views
0

我从mySQL数据库获取足球队的阵容数据。多行与recyclerview

我想在守门员,后卫,中场球员的每一行上显示回收者的球员数据。

我应该怎么做?:

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

回答

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(); 
    } 
} 
+0

我应该为每个守门员,后卫和其他人创建自定义布局吗? 或所有在相同的自定义布局 –

+0

这取决于。如果更改较小,则可以准备一个代码来隐藏/显示此视图中的某些元素,但需要仔细进行编写,以免在滚动过程中失去性能。但是这个解决方案应该处理视图中的变化,并且viewType的解决方案不是必需的。 如果更改更大,则可以简单地为其他行类型准备另一个布局。 –

+0

我必须像第一个cardview一样做,但是将名称改为Defenders并根据Defenders标题添加球员(名字,图片)。(相同于中场球员) –