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
๋ฐ์ํ
'๐ Development > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋๋ก์ด๋] View Binding(๋ทฐ๋ฐ์ธ๋ฉ) ์ฌ์ฉ ๋ฐฉ๋ฒ (0) | 2023.07.17 |
---|---|
[์๋๋ก์ด๋ Android] Data Binding ์ฌ์ฉ๋ฒ (0) | 2023.07.17 |
[์๋๋ก์ด๋] Jetpack์ ViewModel์ด๋? (์ฌ์ฉ๋ฒ ์์ ) (0) | 2023.05.22 |
[์๋๋ก์ด๋] JetPack์ LiveData๋? (0) | 2023.05.22 |
[์๋๋ก์ด๋] Jetpack์ Room DB์ด๋? (์ฌ์ฉ๋ฒ ์์ ) (0) | 2023.05.17 |