본문 바로가기

학교 & 학원 이론 수업/FPT 회사에서 배운 내용

Android - RecycleView

스크롤을 만들어보자 (RecycleView)

 

 

layout>activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:background="@color/black"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- This is Tab bar-->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@color/gray_bg">

        <ImageView
            android:id="@+id/left_arrow"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_margin="15dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:src="@drawable/left_arrow_icon"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="13dp"
            android:text="Mark Zuckerberg"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_margin="15dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:src="@drawable/search_icon"
            tools:ignore="MissingConstraints" />

    </androidx.constraintlayout.widget.ConstraintLayout>
    <!-- This is face image and avatar area-->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="370dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp">

            <ImageView
                android:id="@+id/face_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@drawable/face_image"
                tools:ignore="MissingConstraints" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/gray_bg"
            android:paddingLeft="20dp"
            android:paddingTop="40dp"
            app:layout_constraintTop_toBottomOf="@id/face_image">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif"
                    android:text="Mark Zuckerberg"
                    android:textColor="@color/white"
                    android:textSize="25sp"
                    android:textStyle="bold"
                    tools:ignore="MissingConstraints" />

                <ImageView
                    android:layout_width="28dp"
                    android:layout_height="28dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/verified_icon"
                    app:layout_constraintStart_toEndOf="@+id/name"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:ignore="MissingConstraints" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Bringing the world closer together."
                android:fontFamily="sans-serif"
                android:textColor="@color/white"
                android:textSize="15dp"
                android:layout_marginTop="5dp"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                >
                <Button
                    android:id="@+id/following"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:text="Following"
                    android:drawableLeft="@drawable/follow_icon"
                    app:backgroundTint="@color/following_btn"
                    android:layout_marginRight="10dp"/>


                <Button
                    android:id="@+id/message"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginRight="10dp"
                    android:layout_toRightOf="@id/following"
                    android:background="@drawable/message_button"
                    app:backgroundTint="@null"
                    android:text="Message"
                    android:drawableLeft="@drawable/message_icon"/>

                <ImageButton
                    android:id="@+id/more"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/more_icon"
                    android:background="@drawable/more_button"
                    app:backgroundTint="@null"
                    android:layout_toRightOf="@id/message"/>

            </RelativeLayout>
        </LinearLayout>

        <!--Avatar-->
        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="100dp"
            app:cardCornerRadius="70dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints">

            <ImageView
                android:layout_width="350px"
                android:layout_height="350px"
                android:src="@drawable/avatar_background" />

        </androidx.cardview.widget.CardView>



    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- This is info details area-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@color/gray_bg">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <Button
                android:id="@+id/post"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/rounded_bg"
                android:text="Posts"
                android:textColor="#03A9F4"
                android:textSize="18sp"
                android:textStyle="bold"
                app:backgroundTint="@null"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <Button
                android:id="@+id/photo"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginStart="3dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_bg"
                android:text="Photos"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="bold"
                app:backgroundTint="@null"
                app:layout_constraintStart_toEndOf="@+id/post"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />
            <Button
                android:id="@+id/reel"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginStart="3dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_bg"
                android:text="Reels"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="bold"
                app:backgroundTint="@null"
                app:layout_constraintStart_toEndOf="@+id/photo"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Details"
            android:textSize="16sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:layout_marginBottom="10dp"/>

<!--        &lt;!&ndash; Founder&ndash;&gt;-->
<!--        <LinearLayout-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:orientation="horizontal"-->
<!--            android:layout_margin="10dp">-->
<!--            <ImageView-->
<!--                android:layout_width="25dp"-->
<!--                android:layout_height="25dp"-->
<!--                android:src="@drawable/work_icon"-->
<!--                android:layout_marginRight="15dp"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Founder and CEO at "-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Meta"-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textStyle="bold"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--        </LinearLayout>-->
<!--        &lt;!&ndash; Work&ndash;&gt;-->
<!--        <LinearLayout-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:orientation="horizontal"-->
<!--            android:layout_margin="10dp">-->
<!--            <ImageView-->
<!--                android:layout_width="25dp"-->
<!--                android:layout_height="25dp"-->
<!--                android:src="@drawable/work_icon"-->
<!--                android:layout_marginRight="15dp"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Work at "-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Chan Zuckerberg Initiative"-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textStyle="bold"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--        </LinearLayout>-->
<!--        &lt;!&ndash; Studied&ndash;&gt;-->
<!--        <LinearLayout-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:orientation="horizontal"-->
<!--            android:layout_margin="8dp">-->
<!--            <ImageView-->
<!--                android:layout_width="25dp"-->
<!--                android:layout_height="25dp"-->
<!--                android:src="@drawable/university_icon"-->
<!--                android:layout_marginRight="15dp"-->
<!--                />-->

<!--            <LinearLayout-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:orientation="vertical">-->

<!--                <TextView-->
<!--                    android:layout_width="wrap_content"-->
<!--                    android:layout_height="wrap_content"-->
<!--                    android:fontFamily="sans-serif"-->
<!--                    android:text="Studied Computer Science and Psychology at"-->
<!--                    android:textAlignment="center"-->
<!--                    android:textColor="@color/white"-->
<!--                    android:textSize="15sp" />-->

<!--                <TextView-->
<!--                    android:layout_width="wrap_content"-->
<!--                    android:layout_height="wrap_content"-->
<!--                    android:fontFamily="sans-serif"-->
<!--                    android:text="Harvard University"-->
<!--                    android:textAlignment="center"-->
<!--                    android:textColor="@color/white"-->
<!--                    android:textSize="15sp"-->
<!--                    android:textStyle="bold" />-->
<!--            </LinearLayout>-->
<!--        </LinearLayout>-->
<!--        &lt;!&ndash; Live&ndash;&gt;-->
<!--        <LinearLayout-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:orientation="horizontal"-->
<!--            android:layout_margin="8dp">-->
<!--            <ImageView-->
<!--                android:layout_width="25dp"-->
<!--                android:layout_height="25dp"-->
<!--                android:src="@drawable/home_icon"-->
<!--                android:layout_marginRight="15dp"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Lives in "-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Palo Alto, California"-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textStyle="bold"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--        </LinearLayout>-->
<!--        &lt;!&ndash; Address&ndash;&gt;-->
<!--        <LinearLayout-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:orientation="horizontal"-->
<!--            android:layout_margin="10dp">-->
<!--            <ImageView-->
<!--                android:layout_width="25dp"-->
<!--                android:layout_height="25dp"-->
<!--                android:src="@drawable/address_icon"-->
<!--                android:layout_marginRight="15dp"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="From "-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--            <TextView-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="Dobbs Ferry, New York"-->
<!--                android:textColor="@color/white"-->
<!--                android:fontFamily="sans-serif"-->
<!--                android:textSize="15sp"-->
<!--                android:textStyle="bold"-->
<!--                android:textAlignment="center"-->
<!--                />-->
<!--        </LinearLayout>-->
    </LinearLayout>


            <androidx.recyclerview.widget.RecyclerView
                android:background="@color/gray_bg"
                android:layout_marginTop="8dp"
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/image_header" />


</androidx.appcompat.widget.LinearLayoutCompat>

 

 

layout>item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">


    <ImageView
        android:id="@+id/imageview"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginStart="1dp"
        android:layout_marginBottom="1dp"
        android:layout_marginEnd="2dp"
        android:layout_marginTop="2dp"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/description_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/description_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="5dp"
        android:text="Your description here"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@+id/imageview"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java

package com.example.homework1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.PorterDuff;
import android.os.Bundle;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //1-AdapterView
    RecyclerView recyclerView;

    //2-Data Source
    List<Item> itemList;

    // 3- Adapter
    MyAdapter myAdapter;

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

        recyclerView = findViewById(R.id.recyclerView);

        itemList = new ArrayList<>();
        Item item1 = new Item(R.drawable.work_icon, "Work", "Founder and CEO at Meta");
        Item item2 = new Item(R.drawable.work_icon, "Career", "Work at Chan Zuckerberg Initiative");
        Item item3 = new Item(R.drawable.university_icon, "University", "Studied Computer Science and Psychology at Harvard");
        Item item4 = new Item(R.drawable.home_icon, "Home", "Palo Alto, California");
        Item item5 = new Item(R.drawable.address_icon, "Hometown", "Dobbs Ferry, New York");
        Item item6 = new Item(R.drawable.heart_icon, "Marriage", "Married to Pracislla Chen");
        Item item7 = new Item(R.drawable.follower_icon, "Follower", "Followed by 119,488,936 people");


        itemList.add(item1);
        itemList.add(item2);
        itemList.add(item3);
        itemList.add(item4);
        itemList.add(item5);
        itemList.add(item6);
        itemList.add(item7);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        myAdapter = new MyAdapter(itemList);
        recyclerView.setAdapter(myAdapter);

    }
}

 

Item.java

package com.example.homework1;

public class Item {


    int itemImg;
    String itemName, itemDesc;
    public Item(int itemImg, String itemName, String itemDesc) {
        this.itemImg = itemImg;
        this.itemName = itemName;
        this.itemDesc = itemDesc;
    }

    public int getItemImg() {
        return itemImg;
    }

    public void setItemImg(int itemImg) {
        this.itemImg = itemImg;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemDesc() {
        return itemDesc;
    }

    public void setItemDesc(String itemDesc) {
        this.itemDesc = itemDesc;
    }


}

 

 

MyAdapter.java

package com.example.homework1;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    private List<Item> itemList;

    public MyAdapter(List<Item> itemList) {
        this.itemList = itemList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);


        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Item item = itemList.get(position);
        holder.description.setText(item.getItemDesc());
        holder.imageView.setImageResource(item.getItemImg());
    }

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


    public class MyViewHolder extends
            RecyclerView.ViewHolder {

        ImageView imageView;
        TextView description;

        public MyViewHolder (@NonNull View itemView) {

            super(itemView);
            imageView = itemView.findViewById(R.id.imageview);
            description = itemView.findViewById(R.id.description_text);

        }


    }
}

'학교 & 학원 이론 수업 > FPT 회사에서 배운 내용' 카테고리의 다른 글

Embedded Programming  (0) 2023.11.08
Android - intent  (0) 2023.11.02
Android(3)  (0) 2023.10.25
Android(2)- 231024  (0) 2023.10.24
Android (231023)  (0) 2023.10.23