0

I am getting football team lineup data from mySQL database.

I want to show Goalkeepers,Defender,Midfielders as headers on each row with the players data on the recycler.

What should i do?:

1-Create 3 recyclerview?

2-Create 3 custom layouts?

3-Different viewholders?

What i did is this :

enter image description here

What i want to do is : Doing same thing but showing the other data as Defenders with their players and Midfielders with their players

This is my custom layout(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 H"
                    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>

Layout containing the 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>

UPDATED : I want to show as this one :

enter image description here

Mahdi H
  • 339
  • 8
  • 24

1 Answers1

2

You can just use standard RecyclerView way and define several view types. You will need to override function getItemViewType in your adapter. This function needs to return int that defines which type you should display for element. After that you just handle this in 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();
    } 
}
Patryk Jabłoński
  • 697
  • 10
  • 29
  • Should i create a custom layout for each Goalkeepers,Defenders and others ? Or all on same custom layout – Mahdi H May 03 '17 at 15:54
  • It depends. If changes are small you can prepare a code that will hide/show some elements in this view, but you need to prepare it carefully, to not lose performance during scroll. But this solution should handle changes in view and solution with viewType would not be necessary. If changes are bigger you can simply prepare another layout for other row type. – Patryk Jabłoński May 03 '17 at 15:57
  • I have to do the same as the first cardview but changing name to Defenders and adding the players(name,image) according to the Defenders title..(Same for Midfielders) – Mahdi H May 03 '17 at 15:59
  • Ok, so I assume that you need only to change title depending on player position. I think that you can just prepare one layout and set position name from your model. Your player object will have information if it is goalkeeper or midfielder, so you can just use this info. – Patryk Jabłoński May 03 '17 at 16:04
  • ok, according to the uploaded image I can tell for sure that I will suggest to prepare 2 different layouts and use the way I told about in first reply. Better description how to achieve whole headers functionality can be found in this topic: http://stackoverflow.com/questions/34848401/divide-elements-on-groups-in-recyclerview – Patryk Jabłoński May 03 '17 at 18:51
  • Thanks a lot i will try it soon – Mahdi H May 03 '17 at 18:53