MainActivity

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

import com.serifgungor.recyclerview_horizontalscroll.Adapter.RecyclerViewAdapterApp;
import com.serifgungor.recyclerview_horizontalscroll.Model.App;
import com.serifgungor.recyclerview_horizontalscroll.R;

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

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    List<App> apps;
    RecyclerViewAdapterApp adapter;

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

        apps = new ArrayList<>();

        apps.add(new App(
                1,
                "WhatsApp",
                "https://lh3.ggpht.com/mp86vbELnqLi2FzvhiKdPX31_oiTRLNyeK8x4IIrbF5eD1D5RdnVwjQP0hwMNR_JdA=s180",
                "WhatsApp Inc.",
                4.3
        ));

        apps.add(new App(
                2,
                "Facebook",
                "https://lh3.googleusercontent.com/ZZPdzvlpK9r_Df9C3M7j1rNRi7hhHRvPhlklJ3lfi5jk86Jd1s0Y5wcQ1QgbVaAP5Q=s180",
                "Facebook",
                4.3
        ));

        apps.add(new App(
                3,
                "Instagram",
                "https://lh3.googleusercontent.com/aYbdIM1abwyVSUZLDKoE0CDZGRhlkpsaPOg9tNnBktUQYsXflwknnOn2Ge1Yr7rImGk=s180",
                "Instagram",
                4.3
        ));

        apps.add(new App(
                3,
                "YouTube",
                "https://lh3.googleusercontent.com/Ned_Tu_ge6GgJZ_lIO_5mieIEmjDpq9kfgD05wapmvzcInvT4qQMxhxq_hEazf8ZsqA=s180",
                "Google LLC",
                4.3
        ));

        adapter = new RecyclerViewAdapterApp(apps);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setAdapter(adapter);
    }
}

RecyclerViewAdapterApp

import android.content.Intent;
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_horizontalscroll.Activity.MainActivity;
import com.serifgungor.recyclerview_horizontalscroll.Holder.ViewHolderApp;
import com.serifgungor.recyclerview_horizontalscroll.Model.App;
import com.serifgungor.recyclerview_horizontalscroll.R;

import java.util.List;

public class RecyclerViewAdapterApp extends RecyclerView.Adapter<ViewHolderApp> {

    private List<App> appList;

    public RecyclerViewAdapterApp(){}

    public RecyclerViewAdapterApp(List<App> apps){
        this.appList = apps;
    }

    @NonNull
    @Override
    public ViewHolderApp onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.app_view,null);
        return new ViewHolderApp(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolderApp holder, final int position) {
        holder.tvTitle.setText(appList.get(position).getName());
        holder.tvDeveloper.setText(appList.get(position).getDeveloper());
        holder.tvPoint.setText(""+appList.get(position).getPoint());
        Glide
                .with(holder.itemView.getContext())
                .load(appList.get(position).getPhotourl())
                .into(holder.ivLogo);

        //RecyclerView satıra tıklama olayı
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),MainActivity.class);
                intent.putExtra("appid",appList.get(position).getId());
                v.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return appList.size();
    }
}

ViewHolderApp

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

import com.serifgungor.recyclerview_horizontalscroll.R;

public class ViewHolderApp extends RecyclerView.ViewHolder {

    public ImageView ivLogo;
    public TextView tvTitle,tvDeveloper,tvPoint;

    public ViewHolderApp(View itemView) {
        super(itemView);
        ivLogo = itemView.findViewById(R.id.ivAppLogo);
        tvTitle = itemView.findViewById(R.id.tvAppTitle);
        tvDeveloper = itemView.findViewById(R.id.tvDeveloper);
        tvPoint = itemView.findViewById(R.id.tvPoint);
    }
}

App

public class App {
    private int id;
    private String name;
    private String photourl;
    private String developer;
    private double point;

    public App() {
    }

    public App(int id, String name, String photourl, String developer, double point) {
        this.id = id;
        this.name = name;
        this.photourl = photourl;
        this.developer = developer;
        this.point = point;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotourl() {
        return photourl;
    }

    public void setPhotourl(String photourl) {
        this.photourl = photourl;
    }

    public String getDeveloper() {
        return developer;
    }

    public void setDeveloper(String developer) {
        this.developer = developer;
    }

    public double getPoint() {
        return point;
    }

    public void setPoint(double point) {
        this.point = point;
    }
}

app_view.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="wrap_content"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="240dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_margin="3dp">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/ivAppLogo"
                android:layout_width="150dp"
                android:layout_height="0dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="7dp"
                app:layout_constraintBottom_toTopOf="@+id/tvAppTitle"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="spread_inside"
                tools:srcCompat="@tools:sample/avatars" />

            <TextView
                android:id="@+id/tvPoint"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="4dp"
                android:layout_marginBottom="20dp"
                android:text="TextView"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvDeveloper" />

            <TextView
                android:id="@+id/tvDeveloper"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="4dp"
                android:text="TextView"
                android:textSize="12sp"
                app:layout_constraintBottom_toTopOf="@+id/tvPoint"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvAppTitle" />

            <TextView
                android:id="@+id/tvAppTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="4dp"
                android:text="TextView"
                app:layout_constraintBottom_toTopOf="@+id/tvDeveloper"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ivAppLogo" />

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

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"
    tools:context=".Activity.MainActivity">


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

Afiyet olsun...

Bu makaleyi paylaşın:

3 Comments

Bir yorum bırakın