diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java index 83ed1bf7..3f2101ae 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java @@ -1,6 +1,12 @@ package com.sismics.docs.adapter; +import android.graphics.Color; import android.support.v7.widget.RecyclerView; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.format.DateFormat; +import android.text.style.BackgroundColorSpan; +import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -11,59 +17,68 @@ import com.sismics.docs.R; import org.json.JSONArray; import org.json.JSONObject; +import java.util.Date; + /** * Adapter of documents. * * @author bgamard */ public class DocListAdapter extends RecyclerView.Adapter { + /** + * Displayed documents. + */ private JSONArray documents; - // Provide a reference to the views for each data item - // Complex data items may need more than one view per item, and - // you provide access to all the views for a data item in a view holder + /** + * ViewHolder. + */ public static class ViewHolder extends RecyclerView.ViewHolder { - // each data item is just a string in this case public TextView titleTextView; public TextView subtitleTextView; + public TextView dateTextView; + public ViewHolder(View v) { super(v); titleTextView = (TextView) v.findViewById(R.id.titleTextView); subtitleTextView = (TextView) v.findViewById(R.id.subtitleTextView); + dateTextView = (TextView) v.findViewById(R.id.dateTextView); } } - // Provide a suitable constructor (depends on the kind of dataset) public DocListAdapter() { } - public void setDocuments(JSONArray documents) { - this.documents = documents; - notifyDataSetChanged(); - } - - // Create new views (invoked by the layout manager) @Override public DocListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - // create a new view View v = LayoutInflater.from(parent.getContext()). inflate(R.layout.doc_list_item, parent, false); - // set the view's size, margins, paddings and layout parameters return new ViewHolder(v); } - // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { - // - get element from your dataset at this position - // - replace the contents of the view with that element JSONObject document = documents.optJSONObject(position); + holder.titleTextView.setText(document.optString("title")); - holder.subtitleTextView.setText(document.optString("description")); + + JSONArray tags = document.optJSONArray("tags"); + SpannableStringBuilder builder = new SpannableStringBuilder(); + for (int i = 0; i < tags.length(); i++) { + JSONObject tag = tags.optJSONObject(i); + int start = builder.length(); + builder.append(" ").append(tag.optString("name")).append(" "); + builder.setSpan(new ForegroundColorSpan(Color.WHITE), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + builder.setSpan(new BackgroundColorSpan(Color.parseColor(tag.optString("color"))), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + builder.append(" "); + } + holder.subtitleTextView.setText(builder); + + String date = DateFormat.getDateFormat(holder.dateTextView.getContext()).format(new Date(document.optLong("create_date"))); + holder.dateTextView.setText(date); } - // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { if (documents == null) { @@ -71,4 +86,13 @@ public class DocListAdapter extends RecyclerView.Adapter - + android:padding="12dp" + android:clickable="true" + android:focusable="true" + android:background="?android:attr/selectableItemBackground"> + + + android:text="Test" + android:textSize="16sp" + android:ellipsize="end" + android:maxLines="1"/> + android:text="test2" + android:textSize="16sp" + android:maxLines="1" + android:ellipsize="end"/> + + - \ No newline at end of file + \ No newline at end of file diff --git a/docs-android/app/src/main/res/layout/main_activity.xml b/docs-android/app/src/main/res/layout/main_activity.xml index 2600e19a..c38cd8af 100644 --- a/docs-android/app/src/main/res/layout/main_activity.xml +++ b/docs-android/app/src/main/res/layout/main_activity.xml @@ -17,7 +17,6 @@ android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" - android:background="@drawable/list_background_holo" android:gravity="center" android:orientation="vertical">