-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficAdapter.java
153 lines (126 loc) · 4.91 KB
/
TrafficAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.therolf.optymoNext.vue.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.therolf.optymoNext.R;
import java.util.ArrayList;
@SuppressWarnings("unused")
public class TrafficAdapter extends BaseExpandableListAdapter {
private Context context;
private int titleId;
private ArrayList<TrafficInfo> data;
private Drawable upArrow;
private Drawable downArrow;
public TrafficAdapter(Context context, int titleId, ArrayList<TrafficInfo> data) {
this.context = context;
this.titleId = titleId;
this.data = data;
this.upArrow = context.getResources().getDrawable(R.drawable.ic_arrow_up_red);
this.downArrow = context.getResources().getDrawable(R.drawable.ic_arrow_down_red);
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public int getChildrenCount(int groupPosition) {
return data.size();
}
@Override
public Object getGroup(int groupPosition) {
return data;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@SuppressLint("InflateParams")
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//noinspection ConstantConditions
convertView = inflater.inflate(R.layout.expandable_title, null);
}
((TextView) convertView.findViewById(R.id.expandable_title)).setText(titleId);
ImageView arrow = convertView.findViewById(R.id.expandable_arrow);
if(isExpanded) {
arrow.setImageDrawable(upArrow);
} else {
arrow.setImageDrawable(downArrow);
}
return convertView;
}
@SuppressLint("InflateParams")
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//noinspection ConstantConditions
convertView = inflater.inflate(R.layout.expandable_item, null);
}
// update date
((TextView) convertView.findViewById(R.id.expandable_date)).setText(data.get(childPosition).date);
// update line
if (data.get(childPosition).lines.equals("")) {
convertView.findViewById(R.id.expandable_lines).setVisibility(View.GONE);
} else {
convertView.findViewById(R.id.expandable_lines).setVisibility(View.VISIBLE);
((TextView) convertView.findViewById(R.id.expandable_lines)).setText(data.get(childPosition).lines);
}
// update content
((TextView) convertView.findViewById(R.id.expandable_content)).setText(data.get(childPosition).content);
// update more infos url
TextView moreInfos = convertView.findViewById(R.id.expandable_more_infos);
if(data.get(childPosition).url != null && data.get(childPosition).url.startsWith("http")) {
CharSequence text = moreInfos.getText();
String linkText = "<a href='" + data.get(childPosition).url + "'>" + text + "</a>";
moreInfos.setMovementMethod(LinkMovementMethod.getInstance());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
moreInfos.setText(Html.fromHtml(linkText, Html.FROM_HTML_MODE_LEGACY));
} else {
moreInfos.setText(Html.fromHtml(linkText));
}
} else {
moreInfos.setVisibility(View.INVISIBLE);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public static class TrafficInfo {
String date;
String lines;
String content;
String url;
public TrafficInfo(String date, String lines, String content, String url) {
this.date = date;
this.lines = lines;
this.content = content;
this.url = url;
}
}
}