mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 14:07:55 +01:00
Closes #35: Android: Tag depth shown in tags tree
This commit is contained in:
parent
af23cd4948
commit
b3e44b84d2
@ -114,9 +114,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
TagListAdapter adapter = (TagListAdapter) tagListView.getAdapter();
|
TagListAdapter adapter = (TagListAdapter) tagListView.getAdapter();
|
||||||
if (adapter == null) return;
|
if (adapter == null) return;
|
||||||
JSONObject tag = adapter.getItem(position);
|
TagListAdapter.TagItem tagItem = adapter.getItem(position);
|
||||||
if (tag == null) return;
|
if (tagItem == null) return;
|
||||||
searchQuery("tag:" + tag.optString("name"));
|
searchQuery("tag:" + tagItem.getName());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package com.sismics.docs.adapter;
|
package com.sismics.docs.adapter;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.util.TypedValue;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -17,8 +19,6 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,7 +30,7 @@ public class TagListAdapter extends BaseAdapter {
|
|||||||
/**
|
/**
|
||||||
* Tags.
|
* Tags.
|
||||||
*/
|
*/
|
||||||
private List<JSONObject> tags;
|
private List<TagItem> tagItemList = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tag list adapter.
|
* Tag list adapter.
|
||||||
@ -38,33 +38,53 @@ public class TagListAdapter extends BaseAdapter {
|
|||||||
* @param tagsArray Tags
|
* @param tagsArray Tags
|
||||||
*/
|
*/
|
||||||
public TagListAdapter(JSONArray tagsArray) {
|
public TagListAdapter(JSONArray tagsArray) {
|
||||||
this.tags = new ArrayList<>();
|
List<JSONObject> tags = new ArrayList<>();
|
||||||
for (int i = 0; i < tagsArray.length(); i++) {
|
for (int i = 0; i < tagsArray.length(); i++) {
|
||||||
tags.add(tagsArray.optJSONObject(i));
|
tags.add(tagsArray.optJSONObject(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort tags by count desc
|
// Reorder tags by parent/child relation and compute depth
|
||||||
Collections.sort(tags, new Comparator<JSONObject>() {
|
int depth = 0;
|
||||||
@Override
|
initTags(tags, JSONObject.NULL.toString(), depth);
|
||||||
public int compare(JSONObject lhs, JSONObject rhs) {
|
}
|
||||||
return lhs.optInt("count") < rhs.optInt("count") ? 1 : -1;
|
|
||||||
|
/**
|
||||||
|
* Init tags model recursively.
|
||||||
|
*
|
||||||
|
* @param tags All tags from server
|
||||||
|
* @param parentId Parent ID
|
||||||
|
* @param depth Depth
|
||||||
|
*/
|
||||||
|
private void initTags(List<JSONObject> tags, String parentId, int depth) {
|
||||||
|
// Get all tags with this parent
|
||||||
|
for (JSONObject tag : tags) {
|
||||||
|
String tagParentId = tag.optString("parent");
|
||||||
|
if (tagParentId.equals(parentId)) {
|
||||||
|
TagItem tagItem = new TagItem();
|
||||||
|
tagItem.id = tag.optString("id");
|
||||||
|
tagItem.name = tag.optString("name");
|
||||||
|
tagItem.count = tag.optInt("count");
|
||||||
|
tagItem.color = tag.optString("color");
|
||||||
|
tagItem.depth = depth;
|
||||||
|
tagItemList.add(tagItem);
|
||||||
|
initTags(tags, tagItem.id, depth + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
return tags.size();
|
return tagItemList.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject getItem(int position) {
|
public TagItem getItem(int position) {
|
||||||
return tags.get(position);
|
return tagItemList.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(int position) {
|
public long getItemId(int position) {
|
||||||
return getItem(position).optString("id").hashCode();
|
return getItem(position).id.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -75,19 +95,41 @@ public class TagListAdapter extends BaseAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fill the view
|
// Fill the view
|
||||||
JSONObject tag = getItem(position);
|
TagItem tagItem = getItem(position);
|
||||||
TextView tagTextView = (TextView) view.findViewById(R.id.tagTextView);
|
TextView tagTextView = (TextView) view.findViewById(R.id.tagTextView);
|
||||||
tagTextView.setText(tag.optString("name"));
|
tagTextView.setText(tagItem.name);
|
||||||
TextView tagCountTextView = (TextView) view.findViewById(R.id.tagCountTextView);
|
TextView tagCountTextView = (TextView) view.findViewById(R.id.tagCountTextView);
|
||||||
tagCountTextView.setText(tag.optString("count"));
|
tagCountTextView.setText(String.format("%d", tagItem.count));
|
||||||
|
|
||||||
// Label color filtering
|
// Label color filtering
|
||||||
ImageView labelImageView = (ImageView) view.findViewById(R.id.labelImageView);
|
ImageView labelImageView = (ImageView) view.findViewById(R.id.labelImageView);
|
||||||
Drawable labelDrawable = labelImageView.getDrawable().mutate();
|
Drawable labelDrawable = labelImageView.getDrawable().mutate();
|
||||||
labelDrawable.setColorFilter(Color.parseColor(tag.optString("color")), PorterDuff.Mode.MULTIPLY);
|
labelDrawable.setColorFilter(Color.parseColor(tagItem.color), PorterDuff.Mode.MULTIPLY);
|
||||||
labelImageView.setImageDrawable(labelDrawable);
|
labelImageView.setImageDrawable(labelDrawable);
|
||||||
labelImageView.invalidate();
|
labelImageView.invalidate();
|
||||||
|
|
||||||
|
// Offset according to depth
|
||||||
|
Resources resources = parent.getContext().getResources();
|
||||||
|
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) labelImageView.getLayoutParams();
|
||||||
|
layoutParams.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tagItem.depth * 12, resources.getDisplayMetrics());
|
||||||
|
labelImageView.setLayoutParams(layoutParams);
|
||||||
|
labelImageView.requestLayout();
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tag item in the tags list.
|
||||||
|
*/
|
||||||
|
public static class TagItem {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private int count;
|
||||||
|
private String color;
|
||||||
|
private int depth;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user