diff --git a/app/build.gradle b/app/build.gradle index fc50d19..54b1252 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,6 +48,8 @@ dependencies { compile 'com.android.support:design:25.3.1' + compile 'com.squareup.picasso:picasso:2.5.2' + compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' diff --git a/app/src/main/java/com/emarsys/mobileengage/sample/NotificationListAdapter.java b/app/src/main/java/com/emarsys/mobileengage/sample/NotificationListAdapter.java index 83c1636..24b572e 100644 --- a/app/src/main/java/com/emarsys/mobileengage/sample/NotificationListAdapter.java +++ b/app/src/main/java/com/emarsys/mobileengage/sample/NotificationListAdapter.java @@ -1,42 +1,66 @@ package com.emarsys.mobileengage.sample; +import android.content.Context; +import android.support.constraint.ConstraintLayout; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; -import android.widget.LinearLayout; +import android.widget.ImageView; import android.widget.TextView; import com.emarsys.mobileengage.inbox.model.Notification; +import com.squareup.picasso.Picasso; +import java.text.DateFormat; +import java.util.Date; import java.util.List; public class NotificationListAdapter extends RecyclerView.Adapter { public static class ViewHolder extends RecyclerView.ViewHolder { - public TextView mTextView; + public Context context; + public TextView title; + public TextView receivedAt; + public ImageView imageView; - public ViewHolder(LinearLayout root) { + public ViewHolder(ConstraintLayout root) { super(root); - mTextView = (TextView) root.findViewById(R.id.adapterNotificationTitle); + context = root.getContext(); + title = (TextView) root.findViewById(R.id.adapterNotificationTitle); + receivedAt = (TextView) root.findViewById(R.id.receivedAt); + imageView = (ImageView) root.findViewById(R.id.imageview); } } List notifications; + DateFormat dateFormat; public NotificationListAdapter(List notifications) { this.notifications = notifications; + this.dateFormat = DateFormat.getDateTimeInstance(); } @Override public NotificationListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - LinearLayout root = (LinearLayout) LayoutInflater.from(parent.getContext()) + ConstraintLayout root = (ConstraintLayout) LayoutInflater.from(parent.getContext()) .inflate(R.layout.adapter_notification_list_item, parent, false); return new ViewHolder(root); } @Override public void onBindViewHolder(NotificationListAdapter.ViewHolder holder, int position) { - holder.mTextView.setText(notifications.get(position).getTitle()); + Notification notification = notifications.get(position); + + holder.title.setText(notification.getTitle()); + String dateString = dateFormat.format(new Date(notification.getReceivedAt())); + holder.receivedAt.setText("Received at " + dateString); + + String imageUrl = notification.getCustomData().get("image"); + if (imageUrl != null) { + Picasso.with(holder.context).load(imageUrl).into(holder.imageView); + } else { + Picasso.with(holder.context).load(R.drawable.ic_image_black).into(holder.imageView); + } } @Override diff --git a/app/src/main/res/drawable/ic_image_black.png b/app/src/main/res/drawable/ic_image_black.png new file mode 100644 index 0000000..ab700a3 Binary files /dev/null and b/app/src/main/res/drawable/ic_image_black.png differ diff --git a/app/src/main/res/layout/adapter_notification_list_item.xml b/app/src/main/res/layout/adapter_notification_list_item.xml index b7c3f30..5314021 100644 --- a/app/src/main/res/layout/adapter_notification_list_item.xml +++ b/app/src/main/res/layout/adapter_notification_list_item.xml @@ -1,13 +1,45 @@ - + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" + android:layout_marginTop="8dp" + app:layout_constraintHorizontal_bias="1.0" + app:layout_constraintLeft_toRightOf="@+id/imageview" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toTopOf="parent" + tools:text="Notification title" /> + + + + - \ No newline at end of file + \ No newline at end of file