added background; categorized messages
This commit is contained in:
@@ -8,6 +8,7 @@ import android.os.Bundle;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -22,20 +23,22 @@ public class MainActivity extends AppCompatActivity implements Runnable, Adapter
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
mData = new ArrayList<>();
|
mData = new ArrayList<>();
|
||||||
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));
|
||||||
mData.add(new Message("Joel", "Eine super Nachricht", new Date()));
|
mData.add(new Message("Joel", "Eine super Nachricht", new Date(), MessageType.RECIEVED_MESSAGE));
|
||||||
mData.add(new Message("Max", "Eine Nachricht", new Date()));
|
mData.add(new Message("Max", "Eine Nachricht", new Date(), MessageType.RECIEVED_MESSAGE));
|
||||||
|
|
||||||
myAdapter = new MyAdapter(mData);
|
myAdapter = new MyAdapter(mData);
|
||||||
RecyclerView mRecyclerView = findViewById(R.id.recyclerView);
|
RecyclerView mRecyclerView = findViewById(R.id.recyclerView);
|
||||||
mRecyclerView.addOnItemTouchListener(
|
mRecyclerView.addOnItemTouchListener(
|
||||||
new RecyclerItemClickListener(getApplicationContext(), mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
|
new RecyclerItemClickListener(getApplicationContext(), mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
|
||||||
@Override public void onItemClick(View view, int position) {
|
@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) {
|
@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);
|
int randNr = (int) Math.floor(Math.random() * 3);
|
||||||
switch (randNr){
|
switch (randNr){
|
||||||
case 0:
|
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;
|
break;
|
||||||
case 1:
|
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;
|
break;
|
||||||
case 2:
|
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();
|
myAdapter.notifyDataSetChanged();
|
||||||
mHandler.postDelayed(this, 1000);
|
mHandler.postDelayed(this, 1000);
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ class Message {
|
|||||||
String sender;
|
String sender;
|
||||||
String text;
|
String text;
|
||||||
Date date;
|
Date date;
|
||||||
|
MessageType mMType;
|
||||||
|
|
||||||
Message(String aSender, String aText, Date aDate) {
|
Message(String aSender, String aText, Date aDate, MessageType aMType) {
|
||||||
sender = aSender;
|
sender = aSender;
|
||||||
text = aText;
|
text = aText;
|
||||||
date = aDate;
|
date = aDate;
|
||||||
|
mMType = aMType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.example.recyclerview;
|
||||||
|
|
||||||
|
public enum MessageType {
|
||||||
|
OWN_MESSAGE,
|
||||||
|
RECIEVED_MESSAGE;
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.example.recyclerview;
|
package com.example.recyclerview;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@@ -13,15 +12,18 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
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;
|
mData = aData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemViewType(int position) {
|
public int getItemViewType(int position) {
|
||||||
return position % 2;
|
if (mData.get(position).mMType == MessageType.OWN_MESSAGE) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|||||||
5
app/src/main/res/drawable/rectangle_blue.xml
Normal file
5
app/src/main/res/drawable/rectangle_blue.xml
Normal 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>
|
||||||
5
app/src/main/res/drawable/rectangle_green.xml
Normal file
5
app/src/main/res/drawable/rectangle_green.xml
Normal 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>
|
||||||
@@ -8,8 +8,9 @@
|
|||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/recyclerView"
|
android:id="@+id/recyclerView"
|
||||||
android:layout_width="409dp"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="729dp"
|
android:layout_height="match_parent"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
@@ -4,7 +4,9 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="100dp"
|
android:layout_height="100dp"
|
||||||
android:layout_marginLeft="10dp"
|
android:layout_marginLeft="10dp"
|
||||||
|
android:layout_marginTop="10dp"
|
||||||
android:layout_marginRight="150dp"
|
android:layout_marginRight="150dp"
|
||||||
|
android:background="@drawable/rectangle_green"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="100dp"
|
android:layout_height="100dp"
|
||||||
android:layout_marginLeft="150dp"
|
android:layout_marginLeft="150dp"
|
||||||
|
android:layout_marginTop="10dp"
|
||||||
android:layout_marginRight="10dp"
|
android:layout_marginRight="10dp"
|
||||||
|
android:background="@drawable/rectangle_blue"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
|||||||
Reference in New Issue
Block a user