Skip to content

Commit 63882bd

Browse files
author
Appyown
committed
Add Folding Cell Widgets
1 parent 11169e9 commit 63882bd

29 files changed

+1881
-3
lines changed

app/src/main/java/com/dvinfosys/WidgetsExample/Activity/HomeActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.dvinfosys.WidgetsExample.Fragments.ColorPickerFragment;
1919
import com.dvinfosys.WidgetsExample.Fragments.CountdownViewFragment;
2020
import com.dvinfosys.WidgetsExample.Fragments.EditTextFragment;
21+
import com.dvinfosys.WidgetsExample.Fragments.FoldingCellFragment;
2122
import com.dvinfosys.WidgetsExample.Fragments.ImageviewFragment;
2223
import com.dvinfosys.WidgetsExample.Fragments.NumberCounterFragment;
2324
import com.dvinfosys.WidgetsExample.Fragments.ProgressViewFragment;
@@ -119,6 +120,8 @@ public boolean onNavigationItemSelected(MenuItem item) {
119120
fragment = new SwitchFragment();
120121
} else if (id == R.id.nav_togglebutton) {
121122
fragment = new ToggleButtonFragment();
123+
} else if (id == R.id.nav_folding_cell) {
124+
fragment = new FoldingCellFragment();
122125
} else if (id == R.id.nav_share) {
123126

124127
} else if (id == R.id.nav_send) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.dvinfosys.WidgetsExample.Adapter;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ArrayAdapter;
9+
import android.widget.TextView;
10+
11+
import java.util.HashSet;
12+
import java.util.List;
13+
14+
import com.dvinfosys.WidgetsExample.Model.Item;
15+
import com.dvinfosys.WidgetsExample.R;
16+
import com.dvinfosys.widgets.foldingcell.FoldingCell;
17+
18+
public class FoldingCellListAdapter extends ArrayAdapter<Item> {
19+
20+
private HashSet<Integer> unfoldedIndexes = new HashSet<>();
21+
private View.OnClickListener defaultRequestBtnClickListener;
22+
23+
public FoldingCellListAdapter(Context context, List<Item> objects) {
24+
super(context, 0, objects);
25+
}
26+
27+
@NonNull
28+
@Override
29+
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
30+
// get item for selected view
31+
Item item = getItem(position);
32+
// if cell is exists - reuse it, if not - create the new one from resource
33+
FoldingCell cell = (FoldingCell) convertView;
34+
ViewHolder viewHolder;
35+
if (cell == null) {
36+
viewHolder = new ViewHolder();
37+
LayoutInflater vi = LayoutInflater.from(getContext());
38+
cell = (FoldingCell) vi.inflate(R.layout.cell, parent, false);
39+
// binding view parts to view holder
40+
viewHolder.price = cell.findViewById(R.id.title_price);
41+
viewHolder.time = cell.findViewById(R.id.title_time_label);
42+
viewHolder.date = cell.findViewById(R.id.title_date_label);
43+
viewHolder.fromAddress = cell.findViewById(R.id.title_from_address);
44+
viewHolder.toAddress = cell.findViewById(R.id.title_to_address);
45+
viewHolder.requestsCount = cell.findViewById(R.id.title_requests_count);
46+
viewHolder.pledgePrice = cell.findViewById(R.id.title_pledge);
47+
viewHolder.contentRequestBtn = cell.findViewById(R.id.content_request_btn);
48+
cell.setTag(viewHolder);
49+
} else {
50+
// for existing cell set valid valid state(without animation)
51+
if (unfoldedIndexes.contains(position)) {
52+
cell.unfold(true);
53+
} else {
54+
cell.fold(true);
55+
}
56+
viewHolder = (ViewHolder) cell.getTag();
57+
}
58+
59+
if (null == item)
60+
return cell;
61+
62+
// bind data from selected element to view through view holder
63+
viewHolder.price.setText(item.getPrice());
64+
viewHolder.time.setText(item.getTime());
65+
viewHolder.date.setText(item.getDate());
66+
viewHolder.fromAddress.setText(item.getFromAddress());
67+
viewHolder.toAddress.setText(item.getToAddress());
68+
viewHolder.requestsCount.setText(String.valueOf(item.getRequestsCount()));
69+
viewHolder.pledgePrice.setText(item.getPledgePrice());
70+
71+
// set custom btn handler for list item from that item
72+
if (item.getRequestBtnClickListener() != null) {
73+
viewHolder.contentRequestBtn.setOnClickListener(item.getRequestBtnClickListener());
74+
} else {
75+
// (optionally) add "default" handler if no handler found in item
76+
viewHolder.contentRequestBtn.setOnClickListener(defaultRequestBtnClickListener);
77+
}
78+
79+
return cell;
80+
}
81+
82+
// simple methods for register cell state changes
83+
public void registerToggle(int position) {
84+
if (unfoldedIndexes.contains(position))
85+
registerFold(position);
86+
else
87+
registerUnfold(position);
88+
}
89+
90+
public void registerFold(int position) {
91+
unfoldedIndexes.remove(position);
92+
}
93+
94+
public void registerUnfold(int position) {
95+
unfoldedIndexes.add(position);
96+
}
97+
98+
public View.OnClickListener getDefaultRequestBtnClickListener() {
99+
return defaultRequestBtnClickListener;
100+
}
101+
102+
public void setDefaultRequestBtnClickListener(View.OnClickListener defaultRequestBtnClickListener) {
103+
this.defaultRequestBtnClickListener = defaultRequestBtnClickListener;
104+
}
105+
106+
// View lookup cache
107+
private static class ViewHolder {
108+
TextView price;
109+
TextView contentRequestBtn;
110+
TextView pledgePrice;
111+
TextView fromAddress;
112+
TextView toAddress;
113+
TextView requestsCount;
114+
TextView date;
115+
TextView time;
116+
}
117+
}

app/src/main/java/com/dvinfosys/WidgetsExample/Fragments/EditTextFragment.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,37 @@
1111
import android.view.inputmethod.InputMethodManager;
1212

1313
import com.dvinfosys.WidgetsExample.R;
14+
import com.dvinfosys.widgets.EditText.EditTextSpinner;
1415
import com.dvinfosys.widgets.EditText.ZoomEditTextView;
1516

17+
import java.util.ArrayList;
18+
1619
import static android.content.Context.INPUT_METHOD_SERVICE;
1720

1821
public class EditTextFragment extends Fragment {
1922

2023
private ZoomEditTextView edtZoom;
24+
private EditTextSpinner edtSpinner;
2125

2226
@Override
2327
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2428
View view = inflater.inflate(R.layout.fragment_edit_text, container, false);
2529
edtZoom = view.findViewById(R.id.edt_zoom_edittext);
30+
edtSpinner=view.findViewById(R.id.edt_editSpinner);
31+
2632
edtZoom.setOnFocusChangeListener(new MyFocusChangeListener());
2733
edtZoom.setFocusableInTouchMode(true);
2834
edtZoom.setTextIsSelectable(true);
2935
edtZoom.setRawInputType(InputType.TYPE_CLASS_TEXT);
36+
37+
ArrayList<String> options = new ArrayList<>();
38+
options.add("Option 1");
39+
options.add("Option 2");
40+
options.add("Option 3");
41+
options.add("Option 4");
42+
43+
edtSpinner.setOptions(options);
44+
3045
return view;
3146
}
3247

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.dvinfosys.WidgetsExample.Fragments;
2+
3+
import android.os.Bundle;
4+
import android.support.v4.app.Fragment;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.AdapterView;
9+
import android.widget.ListView;
10+
import android.widget.Toast;
11+
12+
import com.dvinfosys.WidgetsExample.Adapter.FoldingCellListAdapter;
13+
import com.dvinfosys.WidgetsExample.Model.Item;
14+
import com.dvinfosys.WidgetsExample.R;
15+
import com.dvinfosys.widgets.foldingcell.FoldingCell;
16+
17+
import java.util.ArrayList;
18+
19+
public class FoldingCellFragment extends Fragment {
20+
21+
public FoldingCellFragment() {
22+
}
23+
24+
@Override
25+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
26+
View v = inflater.inflate(R.layout.fragment_folding_cell, container, false);
27+
ListView theListView = v.findViewById(R.id.mainListView);
28+
final ArrayList<Item> items = Item.getTestingList();
29+
items.get(0).setRequestBtnClickListener(new View.OnClickListener() {
30+
@Override
31+
public void onClick(View v) {
32+
Toast.makeText(getContext(), "CUSTOM HANDLER FOR FIRST BUTTON", Toast.LENGTH_SHORT).show();
33+
}
34+
});
35+
36+
final FoldingCellListAdapter adapter = new FoldingCellListAdapter(getContext(), items);
37+
38+
adapter.setDefaultRequestBtnClickListener(new View.OnClickListener() {
39+
@Override
40+
public void onClick(View v) {
41+
Toast.makeText(getContext(), "DEFAULT HANDLER FOR ALL BUTTONS", Toast.LENGTH_SHORT).show();
42+
}
43+
});
44+
theListView.setAdapter(adapter);
45+
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
46+
@Override
47+
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
48+
49+
((FoldingCell) view).toggle(false);
50+
51+
adapter.registerToggle(pos);
52+
}
53+
});
54+
55+
return v;
56+
}
57+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.dvinfosys.WidgetsExample.Model;
2+
3+
import android.view.View;
4+
5+
import java.util.ArrayList;
6+
7+
public class Item {
8+
private String price;
9+
private String pledgePrice;
10+
private String fromAddress;
11+
private String toAddress;
12+
private int requestsCount;
13+
private String date;
14+
private String time;
15+
16+
private View.OnClickListener requestBtnClickListener;
17+
18+
public Item() {
19+
}
20+
21+
public Item(String price, String pledgePrice, String fromAddress, String toAddress, int requestsCount, String date, String time) {
22+
this.price = price;
23+
this.pledgePrice = pledgePrice;
24+
this.fromAddress = fromAddress;
25+
this.toAddress = toAddress;
26+
this.requestsCount = requestsCount;
27+
this.date = date;
28+
this.time = time;
29+
}
30+
31+
public String getPrice() {
32+
return price;
33+
}
34+
35+
public void setPrice(String price) {
36+
this.price = price;
37+
}
38+
39+
public String getPledgePrice() {
40+
return pledgePrice;
41+
}
42+
43+
public void setPledgePrice(String pledgePrice) {
44+
this.pledgePrice = pledgePrice;
45+
}
46+
47+
public String getFromAddress() {
48+
return fromAddress;
49+
}
50+
51+
public void setFromAddress(String fromAddress) {
52+
this.fromAddress = fromAddress;
53+
}
54+
55+
public String getToAddress() {
56+
return toAddress;
57+
}
58+
59+
public void setToAddress(String toAddress) {
60+
this.toAddress = toAddress;
61+
}
62+
63+
public int getRequestsCount() {
64+
return requestsCount;
65+
}
66+
67+
public void setRequestsCount(int requestsCount) {
68+
this.requestsCount = requestsCount;
69+
}
70+
71+
public String getDate() {
72+
return date;
73+
}
74+
75+
public void setDate(String date) {
76+
this.date = date;
77+
}
78+
79+
public String getTime() {
80+
return time;
81+
}
82+
83+
public void setTime(String time) {
84+
this.time = time;
85+
}
86+
87+
public View.OnClickListener getRequestBtnClickListener() {
88+
return requestBtnClickListener;
89+
}
90+
91+
public void setRequestBtnClickListener(View.OnClickListener requestBtnClickListener) {
92+
this.requestBtnClickListener = requestBtnClickListener;
93+
}
94+
95+
@Override
96+
public boolean equals(Object o) {
97+
if (this == o) return true;
98+
if (o == null || getClass() != o.getClass()) return false;
99+
100+
Item item = (Item) o;
101+
102+
if (requestsCount != item.requestsCount) return false;
103+
if (price != null ? !price.equals(item.price) : item.price != null) return false;
104+
if (pledgePrice != null ? !pledgePrice.equals(item.pledgePrice) : item.pledgePrice != null)
105+
return false;
106+
if (fromAddress != null ? !fromAddress.equals(item.fromAddress) : item.fromAddress != null)
107+
return false;
108+
if (toAddress != null ? !toAddress.equals(item.toAddress) : item.toAddress != null)
109+
return false;
110+
if (date != null ? !date.equals(item.date) : item.date != null) return false;
111+
return !(time != null ? !time.equals(item.time) : item.time != null);
112+
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
int result = price != null ? price.hashCode() : 0;
118+
result = 31 * result + (pledgePrice != null ? pledgePrice.hashCode() : 0);
119+
result = 31 * result + (fromAddress != null ? fromAddress.hashCode() : 0);
120+
result = 31 * result + (toAddress != null ? toAddress.hashCode() : 0);
121+
result = 31 * result + requestsCount;
122+
result = 31 * result + (date != null ? date.hashCode() : 0);
123+
result = 31 * result + (time != null ? time.hashCode() : 0);
124+
return result;
125+
}
126+
127+
/**
128+
* @return List of elements prepared for tests
129+
*/
130+
public static ArrayList<Item> getTestingList() {
131+
ArrayList<Item> items = new ArrayList<>();
132+
items.add(new Item("$14", "$270", "W 79th St, NY, 10024", "W 139th St, NY, 10030", 3, "TODAY", "05:10 PM"));
133+
items.add(new Item("$23", "$116", "W 36th St, NY, 10015", "W 114th St, NY, 10037", 10, "TODAY", "11:10 AM"));
134+
items.add(new Item("$63", "$350", "W 36th St, NY, 10029", "56th Ave, NY, 10041", 0, "TODAY", "07:11 PM"));
135+
items.add(new Item("$19", "$150", "12th Ave, NY, 10012", "W 57th St, NY, 10048", 8, "TODAY", "4:15 AM"));
136+
items.add(new Item("$5", "$300", "56th Ave, NY, 10041", "W 36th St, NY, 10029", 0, "TODAY", "06:15 PM"));
137+
return items;
138+
139+
}
140+
}
368 Bytes
Loading
15.3 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:shape="rectangle">
3+
<gradient
4+
android:angle="90"
5+
android:centerColor="#292929"
6+
android:endColor="#292929"
7+
android:startColor="#292929"
8+
android:type="linear" />
9+
</shape>
651 Bytes
Loading
257 KB
Loading

0 commit comments

Comments
 (0)