๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Android

[Java ์ž๋ฐ”, Android ์•ˆ๋“œ๋กœ์ด๋“œ] ์ฑ„ํŒ…๋ฐฉ ๋งŒ๋“ค๊ธฐ

by Jay Din 2023. 7. 17.
728x90
๋ฐ˜์‘ํ˜•

๋‚ด๊ฐ€ ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€๋Š” ์™ผ์ชฝ์— ๋งํ’์„ ์ด ์žˆ๊ณ , ์ƒ๋Œ€๋ฐฉ ๋ฉ”์„ธ์ง€๋Š” ์˜ค๋ฅธ์ชฝ์— ๋งํ’์„ ์ด ์žˆ๋„๋ก,

์ „ํ˜•์ ์ธ ์ฑ„ํŒ…๋ฐฉ์„ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ID ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์•ฑ ์‹คํ–‰์ž์˜ ID์™€ ์ฑ„ํŒ…์˜ ID๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ๋‚ด๊ฐ€ ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€๋กœ ์นœ๋‹ค.

์‹คํ–‰ํ™”๋ฉด

๋ฐ˜์‘ํ˜•

 

Android Studio - Version

Flamingo | 2022.2.1 patch 2

 

Project Type

 

์ฝ”๋“œ

ChatData.java

public class ChatData {
    private String id;
    private String name;
    private String message;
    // ๋…„-์›”-์ผ-์‹œ-๋ถ„-์ดˆ -> long ํƒ€์ž…์ด ๋‚˜์ค‘์— ํ•ธ๋“ค๋งํ•˜๊ธฐ ์ œ์ผ ํŽธํ•จ
    private long time;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }
}

MainActivity.java

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        // ๋ทฐ ์ดˆ๊ธฐํ™”
        initView();

    }

    String id = "100";

    private void initView(){
        ListView listView = findViewById(R.id.list);
        MyAdapter adapter = new MyAdapter(this, R.layout.list_item);
        listView.setAdapter(adapter);

        EditText editText = findViewById(R.id.editText);
        Button button = findViewById(R.id.button);


        button.setOnClickListener(view -> {
            ChatData data = new ChatData();
            data.setId(id);
            data.setName("Jay Din");
            data.setMessage(editText.getText().toString());
            data.setTime(System.currentTimeMillis());

            // ์–ด๋Œ‘ํ„ฐ์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
            adapter.add(data);

            // editText ๊ณต๋ฐฑใ…ใ„ดใ…‡
            editText.setText("");

        });

    }


    // inner ํด๋ž˜์Šค๋กœ ์–ด๋Œ‘ํ„ฐ ๋งŒ๋“ค๊ธฐ
    // ArrayAdapter ๋“ค์–ด์˜ค๋Š” ์ˆœ์„œ๋Œ€๋กœ ๊ด€๋ฆฌํ•ด์คŒ.
    class MyAdapter extends ArrayAdapter<ChatData>{
        int resource;
        public MyAdapter(@NonNull Context context, int resource) {
            super(context, resource);
            this.resource = resource;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = null;
            if (convertView == null){
                // ์ตœ์ดˆ ํ˜ธ์ถœ. View ์ƒ์„ฑ
                // convertView๋Š” ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ทฐ์ด๊ธฐ ๋•Œ๋ฌธ์— null์ด๋ฉด ์ตœ์ดˆ์‹คํ–‰์ด๋‹ค.
                LayoutInflater inflater = LayoutInflater.from(getContext());
                view = inflater.inflate(resource, null);
            }else{
                // ์žฌ์‚ฌ์šฉํ•œ๋‹ค.
                view = convertView;
            }

            ChatData data = getItem(position);

            if (data.getId().equals(id)){
                // ๋‚ด๊ฐ€ ์“ด ๊ธ€
                view.findViewById(R.id.left_layer).setVisibility(View.GONE);
                view.findViewById(R.id.right_layer).setVisibility(View.VISIBLE);

                TextView name = view.findViewById(R.id.right_name);
                TextView message = view.findViewById(R.id.right_message);
                TextView time = view.findViewById(R.id.right_time);

                name.setText(data.getName());
                message.setText(data.getMessage());
                time.setText(data.getTime()+"");

            }else{
                // ๋‚จ์ด ์“ด ๊ธ€
                view.findViewById(R.id.left_layer).setVisibility(View.VISIBLE);
                view.findViewById(R.id.right_layer).setVisibility(View.GONE);

                TextView name = view.findViewById(R.id.left_name);
                TextView message = view.findViewById(R.id.left_message);
                TextView time = view.findViewById(R.id.left_time);

                name.setText(data.getName());
                message.setText(data.getMessage());
                time.setText(data.getTime()+"");
            }



            return view;
        }
    }



}
๋ฐ˜์‘ํ˜•

 

activity_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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="353dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:divider="@null"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="255dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="8dp"
        android:textSize="10sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- ์™ผ์ชฝ ์ฑ„ํŒ… -->
    <LinearLayout
        android:id="@+id/left_layer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/left_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ํ™๊ธธ๋™" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/left_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/other_message_bg"
                android:maxWidth="300dp"
                android:text="์•ˆ๋…•ํ•˜์„ธ์š”~"
                android:textSize="24sp" />

            <TextView
                android:id="@+id/left_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="10:00" />

        </LinearLayout>

    </LinearLayout>

    <!-- ์˜ค๋ฅธ์ชฝ ์ฑ„ํŒ…-->
    <LinearLayout
        android:id="@+id/right_layer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="vertical">

        <TextView
            android:id="@+id/right_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ํ™๊ธธ๋™" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/right_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="10:00" />

            <TextView
                android:id="@+id/right_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/my_message_bg"
                android:maxWidth="300dp"
                android:text="์•ˆ๋…•ํ•˜์„ธ์š”~"
                android:textSize="24sp" />

        </LinearLayout>

    </LinearLayout>


</LinearLayout>

 

728x90
๋ฐ˜์‘ํ˜•