Android 앱개발 공부/TIL(Today I Learned)

[Android] TIL 37일차

bunny code 2024. 7. 16. 21:15

사과마켓 앱 제작하기 - 1


 

* 재활용에 사용할 xml 코드

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="412dp"
    android:layout_height="180dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

        <androidx.cardview.widget.CardView
            android:id="@+id/cv_fan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardCornerRadius="12dp"
            android:layout_marginStart="16dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent">
        <ImageView
            android:id="@+id/iv_fan"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:src="@drawable/sample1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/tv_fan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="산지 한달된 선풍기 팝니다"
            android:textSize="18dp"
            android:textStyle="bold"
            android:maxLines="2"
            android:ellipsize="end"
            android:layout_marginStart="14dp"
            android:layout_marginTop="4dp"
            app:layout_constraintTop_toTopOf="@+id/cv_fan"
            app:layout_constraintStart_toEndOf="@id/cv_fan"/>

        <TextView
            android:id="@+id/tv_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="서울 서대문구 창천동"
            android:textSize="14dp"
            android:layout_marginStart="14dp"
            android:layout_marginTop="4dp"
            app:layout_constraintTop_toBottomOf="@+id/tv_fan"
            app:layout_constraintStart_toEndOf="@id/cv_fan"/>

        <TextView
            android:id="@+id/tv_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1000"
            android:textSize="20dp"
            android:textStyle="bold"
            android:inputType="number"
            android:layout_marginStart="14dp"
            android:layout_marginTop="4dp"
            app:layout_constraintTop_toBottomOf="@+id/tv_location"
            app:layout_constraintStart_toEndOf="@id/cv_fan"/>

        <ImageView
            android:id="@+id/iv_chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/chat"
            android:layout_marginEnd="70dp"
            android:layout_marginBottom="18dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
        <TextView
            android:id="@+id/tv_chat_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="25"
            android:inputType="number"
            app:layout_constraintTop_toTopOf="@+id/iv_chat"
            app:layout_constraintStart_toEndOf="@+id/iv_chat"
            app:layout_constraintBottom_toBottomOf="@+id/iv_chat"/>
        <ImageView
            android:id="@+id/iv_heart"
            android:layout_width="22dp"
            android:layout_height="wrap_content"
            android:src="@drawable/heart"
            android:layout_marginStart="6dp"
            app:layout_constraintTop_toTopOf="@+id/tv_chat_count"
            app:layout_constraintStart_toEndOf="@+id/tv_chat_count"
            app:layout_constraintBottom_toBottomOf="@+id/tv_chat_count"/>
        <TextView
            android:id="@+id/tv_heart_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="13"
            android:inputType="number"
            app:layout_constraintTop_toTopOf="@+id/iv_heart"
            app:layout_constraintStart_toEndOf="@+id/iv_heart"
            app:layout_constraintBottom_toBottomOf="@+id/iv_heart"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

* data 클래스

data class MyItem(val image:Int,val name:String, val location:String, val money:Int, val chat:Int, val heart:Int ){
}

 

* adapter

package com.android.applemarket

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.android.applemarket.databinding.ItemRecyclerviewBinding

class MyAdapter(val Items:MutableList<MyItem>) : RecyclerView.Adapter<MyAdapter.Holder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.Holder {
        val binding = ItemRecyclerviewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return Holder(binding)
    }

    override fun onBindViewHolder(holder: MyAdapter.Holder, position: Int) {
        holder.mImageView.setImageResource(Items[position].image)
        holder.mName.text = Items[position].name
        holder.mLocation.text = Items[position].location
        holder.mMoney.text = Items[position].money.toString()
        holder.mChat.text = Items[position].chat.toString()
        holder.mHeart.text = Items[position].heart.toString()
    }

    override fun getItemCount(): Int {
        return Items.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    inner class Holder(val binding: ItemRecyclerviewBinding) : RecyclerView.ViewHolder(binding.root){
        val mImageView = binding.ivFan
        val mName = binding.tvFan
        val mLocation = binding.tvLocation
        val mMoney = binding.tvMoney
        val mChat = binding.tvChatCount
        val mHeart = binding.tvHeartCount
    }
}

 

* main Activity

package com.android.applemarket

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.applemarket.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding : ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val dataList = mutableListOf<MyItem>()
        dataList.add(MyItem(R.drawable.sample1,"산지 한달된 선풍기 팝니다","서울 서대문구 창천동",1000,25,13))
        dataList.add(MyItem(R.drawable.sample2,"김치냉장고","인천 계양구 귤현동",25000,28,8))
        dataList.add(MyItem(R.drawable.sample3,"샤넬 카드지갑","수성구 범어동",10000,5,23))
        dataList.add(MyItem(R.drawable.sample4,"금고","해운대구 우제2동",10000,17,14))
        dataList.add(MyItem(R.drawable.sample5,"갤럭시Z플립3 팝니다","연제구 연산제8동",150000,9,22))
        dataList.add(MyItem(R.drawable.sample6,"프라다 복조리백","수원시 영통구 원천동",50000,16,25))
        dataList.add(MyItem(R.drawable.sample7,"울산 동해오션뷰 60평 복층 펜트하우스 1일 숙박권 펜션 힐링 숙소 별장","남구 옥동",150000,54,142))
        dataList.add(MyItem(R.drawable.sample8,"샤넬 탑핸들 가방","동래구 온천제2동",180000,7,31))
        dataList.add(MyItem(R.drawable.sample9,"4행정 엔진분무기 판매합니다.","원주시 명륜2동",30000,28,7))
        dataList.add(MyItem(R.drawable.sample10,"셀린느 버킷 가방","중구 동화동",190000,6,40))

        val adapter = MyAdapter(dataList)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
    }
}

 

* main 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="내배캠동"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginStart="16dp"
        android:layout_marginTop="18dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <ImageView
        android:id="@+id/iv_down"
        android:layout_width="14dp"
        android:layout_height="18dp"
        android:src="@drawable/down"
        android:layout_marginStart="6dp"
        app:layout_constraintTop_toTopOf="@+id/tv_my"
        app:layout_constraintStart_toEndOf="@id/tv_my"
        app:layout_constraintBottom_toBottomOf="@id/tv_my"/>

    <ImageView
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/bell"
        android:layout_marginEnd="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/iv_down"
        app:layout_constraintBottom_toBottomOf="@id/iv_down"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@id/iv_down">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

* 출력 결과

품목들이 차례대로 나타남

 

'Android 앱개발 공부 > TIL(Today I Learned)' 카테고리의 다른 글

[Android] TIL 40일차  (3) 2024.07.22
[Android] TIL 38일차  (1) 2024.07.18
[Android] TIL 36일차  (0) 2024.07.15
[Android] TIL 35일차  (0) 2024.07.12
[Android] TIL 34일차  (0) 2024.07.11