2017-05-03 54 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標題添加球員(名字,圖片)。(相同於中場球員) –