RecyclerView StaggeredGridView Kullanımı

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.serifgungor.recyclerview_straggeredgridview.Adapter.Photo_Adapter;
import com.serifgungor.recyclerview_straggeredgridview.Model.Photo;
import com.serifgungor.recyclerview_straggeredgridview.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    Photo_Adapter adapter;
    List<Photo> photos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        photos = new ArrayList<>();
        /*
        int id, String photoUrl, String photoTitle, String userPhotoUrl, String userName, String userDescription
         */
        photos.add(
                new Photo(1,"https://images.unsplash.com/photo-1544817286-c8934b4f3c3a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","Penguen","https://images.unsplash.com/photo-1544817286-c8934b4f3c3a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","username","açıklama")
        );
        photos.add(
                new Photo(1,"https://images.unsplash.com/photo-1544828115-333cdae6b7c3?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","Street","https://images.unsplash.com/photo-1544828115-333cdae6b7c3?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","username","açıklama")
        );
        photos.add(
                new Photo(1,"https://images.unsplash.com/photo-1544808728-35c5958efd93?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","Train","https://images.unsplash.com/photo-1544808728-35c5958efd93?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","username","açıklama")
        );
        photos.add(
                new Photo(1,"https://images.unsplash.com/photo-1544842413-05944bc01da2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","Taksi","https://images.unsplash.com/photo-1544842413-05944bc01da2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60","username","açıklama")
        );

        recyclerView = findViewById(R.id.recyclerView);
        adapter = new Photo_Adapter(photos);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
    }
}

Photo_Adapter

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.serifgungor.recyclerview_straggeredgridview.Holder.Photo_ViewHolder;
import com.serifgungor.recyclerview_straggeredgridview.Model.Photo;
import com.serifgungor.recyclerview_straggeredgridview.R;

import java.util.List;

public class Photo_Adapter extends RecyclerView.Adapter<Photo_ViewHolder> {
    List<Photo> photos;

    public Photo_Adapter(){}

    public Photo_Adapter(List<Photo> photos){
        /*
        Adapter sınıfındaki Liste değerlerini dolu constructor üzerinden çağırdık.
         */
        this.photos = photos;
    }

    @NonNull
    @Override
    public Photo_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        /*
        Adapter sınıfının layout görünümüne bağlanması işlemini yürütür.
         */
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_row,null);
        return new Photo_ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Photo_ViewHolder holder, int position) {
        /*
        Nesnelerin tanımlanması ViewHolder sınıfı içerisinde gerçekleşmişti.
        holder. diyerek, holder sınıfı içerisinde tanımlamış olduğumuz nesneleri
        değişken olarak çağırabiliyoruz.

        - Tanımlanmış nesnelerin doldurulma (gettext,settext,onclicklistener) işlemlerini
        sağlayan metot.
         */

        holder.tvTitle.setText(photos.get(position).getPhotoTitle());
        holder.tvUserName.setText(photos.get(position).getUserName());
        holder.tvDescription.setText(photos.get(position).getUserDescription());
        Glide
                .with(holder.itemView.getContext())
                .load(photos.get(position).getPhotoUrl())
                .into(holder.ivPhoto);

        Glide
                .with(holder.itemView.getContext())
                .load(photos.get(position).getUserPhotoUrl())
                .into(holder.ivUserPhoto);

    }

    @Override
    public int getItemCount() {
        // Listenin eleman sayısını döndürmesi gerekir.
        return photos.size();
    }

}

Photo_ViewHolder

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.serifgungor.recyclerview_straggeredgridview.R;

public class Photo_ViewHolder extends RecyclerView.ViewHolder {

    /*
    ViewHolder sınıfı, Layout içerisinde bulunan nesnelerin referansının
    tanımlanması için kullanılır.
     */

    public ImageView ivPhoto,ivUserPhoto;
    public TextView tvTitle,tvUserName,tvDescription;

    public Photo_ViewHolder(final View itemView) {
        super(itemView);
        ivPhoto = itemView.findViewById(R.id.ivPhoto);
        ivUserPhoto = itemView.findViewById(R.id.ivUserPhoto);
        tvTitle = itemView.findViewById(R.id.tvTitle);
        tvUserName = itemView.findViewById(R.id.tvUser);
        tvDescription = itemView.findViewById(R.id.tvDescription);
    }

}

Photo

public class Photo {
    private int id;
    private String photoUrl;
    private String photoTitle;
    private String userPhotoUrl;
    private String userName;
    private String userDescription;

    public Photo() {
    }

    public Photo(int id, String photoUrl, String photoTitle, String userPhotoUrl, String userName, String userDescription) {
        this.id = id;
        this.photoUrl = photoUrl;
        this.photoTitle = photoTitle;
        this.userPhotoUrl = userPhotoUrl;
        this.userName = userName;
        this.userDescription = userDescription;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public String getPhotoTitle() {
        return photoTitle;
    }

    public void setPhotoTitle(String photoTitle) {
        this.photoTitle = photoTitle;
    }

    public String getUserPhotoUrl() {
        return userPhotoUrl;
    }

    public void setUserPhotoUrl(String userPhotoUrl) {
        this.userPhotoUrl = userPhotoUrl;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserDescription() {
        return userDescription;
    }

    public void setUserDescription(String userDescription) {
        this.userDescription = userDescription;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

image_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp">

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

            <ImageView
                android:id="@+id/ivPhoto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                app:srcCompat="@drawable/ic_launcher_background" />

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@id/ivPhoto"
                android:layout_marginTop="5dp"
                android:background="#a6a6a6" />

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/line1"
                android:layout_alignParentStart="true"
                android:gravity="center"
                android:text="TextView"
                android:textSize="20sp" />

            <View
                android:id="@+id/line2"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@id/tvTitle"
                android:layout_marginTop="5dp"
                android:background="#a6a6a6" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/line2">

                <ImageView
                    android:id="@+id/ivUserPhoto"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    app:srcCompat="@drawable/ic_launcher_background" />

                <TextView
                    android:id="@+id/tvUser"
                    android:layout_width="286dp"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/ivUserPhoto"
                    android:layout_alignParentStart="true"
                    android:layout_marginStart="78dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp"
                    android:text="TextView"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/tvDescription"
                    android:layout_width="283dp"
                    android:layout_height="wrap_content"
                    android:layout_alignStart="@+id/tvUser"
                    android:layout_alignBottom="@+id/ivUserPhoto"
                    android:layout_marginStart="3dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="9dp"
                    android:text="TextView" />
            </RelativeLayout>


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

Bu makaleyi paylaşın:

3 Comments

Bir yorum bırakın