Android: documents listing: tags and date

This commit is contained in:
jendib 2014-11-21 03:18:20 +01:00
parent 76d195a344
commit 2ce5749226
9 changed files with 85 additions and 24 deletions

View File

@ -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<DocListAdapter.ViewHolder> {
/**
* 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<DocListAdapter.ViewHold
}
return documents.length();
}
/**
* Update the displayed documents
* @param documents Documents
*/
public void setDocuments(JSONArray documents) {
this.documents = documents;
notifyDataSetChanged();
}
}

View File

@ -22,6 +22,9 @@ public class DocumentResource extends BaseResource {
init(context);
RequestParams params = new RequestParams();
params.put("limit", 50);
params.put("sort_column", 3);
params.put("asc", false);
client.get(getApiUrl(context) + "/document/list", params, responseHandler);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 970 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 969 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 751 B

View File

@ -1,26 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp">
android:padding="12dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="12dp"
android:id="@+id/folderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_folder_open_grey600_48dp"/>
<TextView
android:id="@+id/titleTextView"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/folderImageView"
android:layout_toLeftOf="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textColor="#212121"
android:textSize="16sp"/>
android:text="Test"
android:textSize="16sp"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="@+id/subtitleTextView"
android:layout_below="@+id/titleTextView"
android:layout_toRightOf="@+id/folderImageView"
android:layout_marginTop="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textColor="#777777"
android:textSize="16sp"/>
android:text="test2"
android:textSize="16sp"
android:maxLines="1"
android:ellipsize="end"/>
</LinearLayout>
<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2014-12-02"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textColor="#777777"
android:fontFamily="sans-serif-light"/>
</RelativeLayout>

View File

@ -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">