added background; categorized messages

This commit is contained in:
2020-01-20 13:01:41 +01:00
parent 0dd831f921
commit 855491fc69
9 changed files with 43 additions and 15 deletions

View File

@@ -8,6 +8,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
@@ -22,20 +23,22 @@ public class MainActivity extends AppCompatActivity implements Runnable, Adapter
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mData = new ArrayList<>();
mData.add(new Message("Niklas", "Eine super duper Nachricht", new Date()));
mData.add(new Message("Joel", "Eine super Nachricht", new Date()));
mData.add(new Message("Max", "Eine Nachricht", new Date()));
mData.add(new Message("Niklas", "Eine super duper Nachricht", new Date(), MessageType.OWN_MESSAGE));
mData.add(new Message("Joel", "Eine super Nachricht", new Date(), MessageType.RECIEVED_MESSAGE));
mData.add(new Message("Max", "Eine Nachricht", new Date(), MessageType.RECIEVED_MESSAGE));
myAdapter = new MyAdapter(mData);
RecyclerView mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
System.out.println("clicked " + mData.get(position).sender);
Toast.makeText(getApplicationContext(), "clicked " + mData.get(position).sender, Toast.LENGTH_SHORT).show();
}
@Override public void onLongItemClick(View view, int position) {
// do whatever
Toast.makeText(getApplicationContext(), "deleted " + mData.get(position).sender, Toast.LENGTH_SHORT).show();
mData.remove(position);
myAdapter.notifyDataSetChanged();
}
})
);
@@ -51,13 +54,13 @@ public class MainActivity extends AppCompatActivity implements Runnable, Adapter
int randNr = (int) Math.floor(Math.random() * 3);
switch (randNr){
case 0:
mData.add(new Message("Niklas", "Eine super duper Nachricht", new Date()));
mData.add(new Message("Niklas", "Eine super duper Nachricht", new Date(), MessageType.OWN_MESSAGE));
break;
case 1:
mData.add(new Message("Joel", "Eine super Nachricht", new Date()));
mData.add(new Message("Joel", "Eine super Nachricht", new Date(), MessageType.RECIEVED_MESSAGE));
break;
case 2:
mData.add(new Message("Max", "Eine Nachricht", new Date()));
mData.add(new Message("Max", "Eine Nachricht", new Date(), MessageType.RECIEVED_MESSAGE));
}
myAdapter.notifyDataSetChanged();
mHandler.postDelayed(this, 1000);

View File

@@ -6,10 +6,12 @@ class Message {
String sender;
String text;
Date date;
MessageType mMType;
Message(String aSender, String aText, Date aDate) {
Message(String aSender, String aText, Date aDate, MessageType aMType) {
sender = aSender;
text = aText;
date = aDate;
mMType = aMType;
}
}

View File

@@ -0,0 +1,6 @@
package com.example.recyclerview;
public enum MessageType {
OWN_MESSAGE,
RECIEVED_MESSAGE;
}

View File

@@ -1,6 +1,5 @@
package com.example.recyclerview;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -13,15 +12,18 @@ import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<Message> mData;
private ArrayList<Message> mData;
public MyAdapter(ArrayList<Message> aData) {
MyAdapter(ArrayList<Message> aData) {
mData = aData;
}
@Override
public int getItemViewType(int position) {
return position % 2;
if (mData.get(position).mMType == MessageType.OWN_MESSAGE) {
return 1;
}
return 0;
}
@NonNull

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/holo_blue_dark" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/holo_green_dark" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

View File

@@ -8,8 +8,9 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -4,7 +4,9 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="150dp"
android:background="@drawable/rectangle_green"
android:orientation="vertical">
<TextView

View File

@@ -4,7 +4,9 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/rectangle_blue"
android:orientation="vertical">
<TextView