Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit a45e965

Browse files
author
evgenii
committed
Changed a look of ExpListView
1 parent bcce39c commit a45e965

File tree

9 files changed

+216
-32
lines changed

9 files changed

+216
-32
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/ReportActivity.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
import android.app.Activity;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.util.Log;
76
import android.util.Pair;
87
import android.view.View;
8+
import android.view.ViewGroup;
99
import android.view.ViewTreeObserver;
1010
import android.view.Window;
11-
import android.widget.Button;
1211
import android.widget.ExpandableListView;
1312
import android.widget.ListView;
14-
import android.widget.SimpleExpandableListAdapter;
13+
import android.widget.TextView;
1514

1615
import com.blogspot.e_kanivets.moneytracker.R;
16+
import com.blogspot.e_kanivets.moneytracker.adapter.ExpandableListReportAdapter;
1717
import com.blogspot.e_kanivets.moneytracker.adapter.ReportItemAdapter;
1818
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
1919
import com.blogspot.e_kanivets.moneytracker.model.Record;
2020
import com.blogspot.e_kanivets.moneytracker.model.Report;
21+
import com.blogspot.e_kanivets.moneytracker.util.Constants;
2122

2223
import java.util.ArrayList;
2324
import java.util.HashMap;
@@ -30,7 +31,6 @@
3031
public class ReportActivity extends Activity {
3132

3233
private static final String TAG = ReportActivity.class.getSimpleName();
33-
private static final String TITLE_PARAM_NAME = "title";
3434

3535
private Activity activity;
3636
private Report report;
@@ -102,15 +102,16 @@ private void initExpandableListView() {
102102
for (Pair<String, Integer> item : report.getReportList()) {
103103
/* Fill up attribute names for each group */
104104
m = new HashMap<String, String>();
105-
m.put(TITLE_PARAM_NAME, item.first);
105+
m.put(Constants.TITLE_PARAM_NAME, item.first);
106+
m.put(Constants.PRICE_PARAM_NAME, Integer.toString(item.second));
106107

107108
groupData.add(m);
108109
}
109110

110111
/* List of attributes of groups for reading */
111-
String groupFrom[] = new String[]{TITLE_PARAM_NAME};
112+
String groupFrom[] = new String[]{Constants.TITLE_PARAM_NAME, Constants.PRICE_PARAM_NAME};
112113
/* List of view IDs for information insertion */
113-
int groupTo[] = new int[]{android.R.id.text1};
114+
int groupTo[] = new int[]{R.id.tv_category, R.id.tv_total};
114115

115116
/* Create list for childDataItems */
116117
childData = new ArrayList<List<Map<String, String>>>();
@@ -119,10 +120,16 @@ private void initExpandableListView() {
119120
childDataItem = new ArrayList<Map<String, String>>();
120121
/* Fill up attribute names for each child item */
121122
for (Record record : MTHelper.getInstance().getRecords()) {
122-
if (record.getCategory().equals(group.get(TITLE_PARAM_NAME))) {
123-
Log.d(TAG, record.getCategory());
123+
if (record.getCategory().equals(group.get(Constants.TITLE_PARAM_NAME))) {
124+
int price = record.getPrice();
125+
if (!record.isIncome()) {
126+
price *= -1;
127+
}
128+
124129
m = new HashMap<String, String>();
125-
m.put(TITLE_PARAM_NAME, record.getTitle());
130+
m.put(Constants.TITLE_PARAM_NAME, record.getTitle());
131+
m.put(Constants.PRICE_PARAM_NAME, Integer.toString(price));
132+
126133
childDataItem.add(m);
127134
}
128135
}
@@ -132,19 +139,33 @@ private void initExpandableListView() {
132139
}
133140

134141
/* List of attributes of childItems for reading */
135-
String childFrom[] = new String[]{TITLE_PARAM_NAME};
142+
String childFrom[] = new String[]{Constants.TITLE_PARAM_NAME, Constants.PRICE_PARAM_NAME};
136143
/* List of view IDs for information insertion */
137-
int childTo[] = new int[]{android.R.id.text1};
144+
int childTo[] = new int[]{R.id.tv_category, R.id.tv_total};
138145

139-
expandableListView.setAdapter(new SimpleExpandableListAdapter(
146+
expandableListView.addFooterView(getSummaryReportView(report.getSummaryReportList()));
147+
expandableListView.setAdapter(new ExpandableListReportAdapter(
140148
activity,
141149
groupData,
142-
android.R.layout.simple_expandable_list_item_1,
150+
R.layout.view_report_item_exp,
143151
groupFrom,
144152
groupTo,
145153
childData,
146-
android.R.layout.simple_list_item_1,
154+
R.layout.view_report_item,
147155
childFrom,
148-
childTo));
156+
childTo) {
157+
});
158+
}
159+
160+
private View getSummaryReportView(List<Pair<String, Integer>> summaryReportList) {
161+
ViewGroup viewGroup = (ViewGroup) getLayoutInflater().inflate(R.layout.view_summary_report, null);
162+
163+
ReportItemAdapter adapter = new ReportItemAdapter(activity, summaryReportList);
164+
165+
for (int i = 0; i < adapter.getCount(); i++) {
166+
viewGroup.addView(adapter.getView(i, null, null));
167+
}
168+
169+
return viewGroup;
149170
}
150-
}
171+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/ExpandableListReportAdapter.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.blogspot.e_kanivets.moneytracker.adapter;
22

33
import android.content.Context;
4+
import android.view.View;
5+
import android.view.ViewGroup;
46
import android.widget.SimpleExpandableListAdapter;
7+
import android.widget.TextView;
8+
9+
import com.blogspot.e_kanivets.moneytracker.R;
10+
import com.blogspot.e_kanivets.moneytracker.util.Constants;
511

612
import java.util.List;
713
import java.util.Map;
@@ -11,12 +17,59 @@
1117
*/
1218
public class ExpandableListReportAdapter extends SimpleExpandableListAdapter {
1319

20+
private static final String TAG = ExpandableListReportAdapter.class.getSimpleName();
21+
22+
private Context context;
23+
private List<? extends Map<String, ?>> groupData;
24+
private List<? extends List<? extends Map<String, ?>>> childData;
25+
1426
public ExpandableListReportAdapter(Context context, List<? extends Map<String, ?>> groupData,
1527
int groupLayout, String[] groupFrom, int[] groupTo,
1628
List<? extends List<? extends Map<String, ?>>> childData,
1729
int childLayout, String[] childFrom, int[] childTo) {
1830
super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
31+
32+
this.context = context;
33+
this.groupData = groupData;
34+
this.childData = childData;
35+
}
36+
37+
@Override
38+
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
39+
View view = super.getGroupView(groupPosition, isExpanded, convertView, parent);
40+
41+
customizeView(view, (Map<String, String>) groupData.get(groupPosition), true);
42+
43+
return view;
44+
}
45+
46+
@Override
47+
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
48+
View view = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
49+
customizeView(view, (Map<String, String>) childData.get(groupPosition).get(childPosition), false);
50+
return view;
1951
}
2052

53+
private void customizeView(View view, Map<String, String> values, boolean groupView) {
54+
/* Customize view to fit to model and UI */
55+
Integer price = Integer.parseInt(values.get(Constants.PRICE_PARAM_NAME));
56+
57+
if (groupView) {
58+
view.setBackgroundColor(price < 0 ? context.getResources().getColor(R.color.white_red) :
59+
context.getResources().getColor(R.color.white_green));
60+
} else {
61+
view.setBackgroundColor(context.getResources().getColor(R.color.white));
62+
}
2163

64+
TextView tvCategory = (TextView) view.findViewById(R.id.tv_category);
65+
TextView tvTotal = (TextView) view.findViewById(R.id.tv_total);
66+
67+
//Set color of total
68+
tvTotal.setTextColor(price >= 0 ?
69+
context.getResources().getColor(R.color.green) :
70+
context.getResources().getColor(R.color.red));
71+
72+
tvCategory.setText(values.get(Constants.TITLE_PARAM_NAME));
73+
tvTotal.setText((price >= 0 ? "+ " : "- ") + Math.abs(price));
74+
}
2275
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/ReportItemAdapter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010

1111
import com.blogspot.e_kanivets.moneytracker.R;
1212

13-
import org.w3c.dom.Text;
14-
1513
import java.util.List;
1614

1715
/**
1816
* Created by eugene on 11/09/14.
1917
*/
2018
public class ReportItemAdapter extends BaseAdapter {
2119

22-
Context context;
23-
List<Pair<String, Integer>> records;
20+
private Context context;
21+
private List<Pair<String, Integer>> records;
2422

2523
public ReportItemAdapter(Context context, List<Pair<String, Integer>> records) {
2624
this.context = context;
@@ -45,13 +43,13 @@ public long getItemId(int i) {
4543
@Override
4644
public View getView(int i, View view, ViewGroup viewGroup) {
4745
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
48-
view = layoutInflater.inflate(R.layout.view_report_item, null);
46+
view = layoutInflater.inflate(R.layout.view_summary_report_item, null);
4947

5048
//Customize view to fit to model and UI
51-
if(i == getCount()-1) {
49+
if (i == getCount() - 1) {
5250
view.findViewById(R.id.line).setVisibility(View.VISIBLE);
5351
}
54-
if(i == getCount()-3) {
52+
if (i == getCount() - 3) {
5553
view.findViewById(R.id.line).setVisibility(View.VISIBLE);
5654
}
5755
view.setBackgroundColor(records.get(i).second < 0 ? context.getResources().getColor(R.color.white_red) :

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Report.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Report {
1818

1919
private List<Record> records;
2020
private List<Pair<String, Integer>> reportList;
21+
private List<Pair<String, Integer>> summaryReportList;
2122

2223
public Report(List<Record> records) {
2324
this.records = records;
@@ -28,6 +29,10 @@ public List<Pair<String, Integer>> getReportList() {
2829
return reportList;
2930
}
3031

32+
public List<Pair<String, Integer>> getSummaryReportList() {
33+
return summaryReportList;
34+
}
35+
3136
private void makeReport() {
3237
HashMap<String, Integer> map = new HashMap<String, Integer>();
3338

@@ -73,11 +78,12 @@ private void makeReport() {
7378
reportList.addAll(reportExpenses);
7479

7580
//Add summary row to list
76-
reportList.add(new Pair<String, Integer>(
81+
summaryReportList = new ArrayList<Pair<String, Integer>>();
82+
summaryReportList.add(new Pair<String, Integer>(
7783
MTApp.get().getResources().getString(R.string.total_incomes) + " :", totalIncome));
78-
reportList.add(new Pair<String, Integer>(
84+
summaryReportList.add(new Pair<String, Integer>(
7985
MTApp.get().getResources().getString(R.string.total_expenses) + " :", totalExpense));
80-
reportList.add(new Pair<String, Integer>(
86+
summaryReportList.add(new Pair<String, Integer>(
8187
MTApp.get().getResources().getString(R.string.total) + " :", totalExpense + totalIncome));
8288
}
8389

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ public class Constants {
1818
public static final String LAUNCH_COUNT = "launch_count";
1919
public static final int RATE_PERIOD = 5;
2020
public static final String CONTRIBUTION = "contribution";
21+
22+
public static final String TITLE_PARAM_NAME = "title";
23+
public static final String PRICE_PARAM_NAME = "price";
2124
}

app/src/main/res/layout/view_report_item.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@
2424
android:layout_height="match_parent"
2525
android:text="Category"
2626
android:id="@+id/tv_category"
27-
android:textSize="18sp"
27+
android:textSize="16sp"
2828
android:layout_gravity="center"
2929
android:minWidth="5dp"
30-
android:layout_marginLeft="10dp"
30+
android:paddingLeft="10dp"
3131
android:background="@android:color/transparent"
3232
android:layout_weight="1"
3333
android:layout_margin="10dp"
3434
android:gravity="left" />
3535

3636
<TextView
37-
android:layout_width="match_parent"
37+
android:layout_width="wrap_content"
3838
android:layout_height="wrap_content"
3939
android:text="New Text"
4040
android:id="@+id/tv_total"
41-
android:textSize="18sp"
41+
android:textSize="16sp"
4242
android:layout_gravity="center_vertical"
4343
android:background="@android:color/transparent"
44-
android:layout_weight="1"
4544
android:layout_margin="10dp"
4645
android:gravity="right" />
4746

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="wrap_content"
7+
android:background="@android:color/transparent">
8+
9+
<View
10+
android:id="@+id/line"
11+
android:layout_width="match_parent"
12+
android:layout_height="2dp"
13+
android:background="@color/black"
14+
android:visibility="gone" />
15+
16+
<LinearLayout
17+
android:orientation="horizontal"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:background="@android:color/transparent">
21+
22+
<TextView
23+
android:layout_width="match_parent"
24+
android:layout_height="match_parent"
25+
android:text="Category"
26+
android:id="@+id/tv_category"
27+
android:textSize="18sp"
28+
android:layout_gravity="center"
29+
android:minWidth="5dp"
30+
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
31+
android:background="@android:color/transparent"
32+
android:layout_weight="1"
33+
android:layout_margin="10dp"
34+
android:gravity="left" />
35+
36+
<TextView
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:text="New Text"
40+
android:id="@+id/tv_total"
41+
android:textSize="18sp"
42+
android:layout_gravity="center_vertical"
43+
android:background="@android:color/transparent"
44+
android:layout_margin="10dp"
45+
android:gravity="right" />
46+
47+
</LinearLayout>
48+
49+
</LinearLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
</LinearLayout>

0 commit comments

Comments
 (0)