Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.10 KB | None | 0 0
  1. public class Activity {
  2. private int groupToExpand = 4, childToExpand = 3;
  3. protected void onCreate(Bundle savedInstance) {
  4. final ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv);
  5. if (arrayList!= null && !arrayList.isEmpty()) {
  6. elv.setAdapter(new RootAdapter(this, arrayList);
  7. // this selects the correct group, but doesn't expand the child.
  8. elv.setSelectedChild(groupToExpand, childToExpand, true);
  9. elv.expandGroup(groupToExpand); // this works.
  10. }
  11. }
  12. }
  13.  
  14. public class RootAdapter extends BaseExpandableListAdapter {
  15.  
  16. private List<Objects> arrayList;
  17. private Context context;
  18. private LayoutInflater inflater;
  19.  
  20. public RootAdapter(Context context, List<Objects> arrayList) {
  21. this.context = context;
  22. this.arrayList = arrayList;
  23. this.inflater = LayoutInflater.from(context);
  24. }
  25.  
  26. @Override
  27. public Object getChild(int groupPosition, int childPosition) {
  28. final Objects parent = (Objects) getGroup(groupPosition);
  29. return parent.arrayList.get(childPosition);
  30. }
  31.  
  32. @Override
  33. public long getChildId(int groupPosition, int childPosition) {
  34. return childPosition;
  35. }
  36.  
  37. @Override
  38. public View getChildView(int groupPosition, int childPosition,
  39. boolean isLastChild, View convertView, ViewGroup parent) {
  40. final Objects o = (Objects) getChild(groupPosition, childPosition);
  41.  
  42. CustomExpandableListView elv = (CustomExpandableListView) convertView;
  43. ChildViewHolder holder;
  44.  
  45. if (elv == null) {
  46. holder = new ChildViewHolder();
  47.  
  48. elv = new CustomExpandableListView(context);
  49. elv.setGroupIndicator(null);
  50. elv.setDivider(null);
  51. elv.setCacheColorHint(Color.parseColor("#00000000"));
  52. elv.setChildDivider(null);
  53. elv.setChildIndicator(null);
  54. elv.setScrollingCacheEnabled(false);
  55. elv.setAnimationCacheEnabled(false);
  56.  
  57. holder.cListView = elv;
  58. elv.setTag(holder);
  59. } else {
  60. holder = (ChildViewHolder) elv.getTag();
  61. }
  62.  
  63. final ParentAdapter adapter = new ParentAdapter(context, o);
  64. holder.cListView.setAdapter(adapter);
  65.  
  66. return elv;
  67. }
  68.  
  69. private static class ChildViewHolder {
  70. CustomExpandableListView cListView;
  71. }
  72.  
  73. @Override
  74. public int getChildrenCount(int groupPosition) {
  75. final Objects parent = (Objects) getGroup(groupPosition);
  76. return parent.arrayList.size();
  77. }
  78.  
  79. @Override
  80. public Object getGroup(int groupPosition) {
  81. return arrayList.get(groupPosition);
  82. }
  83.  
  84. @Override
  85. public int getGroupCount() {
  86. return arrayList.size();
  87. }
  88.  
  89. @Override
  90. public long getGroupId(int groupPosition) {
  91. return groupPosition;
  92. }
  93.  
  94. @Override
  95. public View getGroupView(int groupPosition, boolean isExpanded,
  96. View convertView, ViewGroup parent) {
  97. View layout = convertView;
  98. GroupViewHolder holder;
  99. final Objects o = (Objects) getGroup(groupPosition);
  100.  
  101. if (layout == null) {
  102. layout = inflater.inflate(R.layout.item_to_inflate, parent, false);
  103. holder = new GroupViewHolder();
  104.  
  105. holder.title = (TextView) layout.findViewById(R.id.title);
  106. holder.image = (ImageView) layout.findViewById(R.id.image);
  107. layout.setTag(holder);
  108. } else {
  109. holder = (GroupViewHolder) layout.getTag();
  110. }
  111.  
  112. holder.title.setText(o.title.trim());
  113.  
  114. return layout;
  115. }
  116.  
  117. @Override
  118. public boolean hasStableIds() {
  119. return true;
  120. }
  121.  
  122. @Override
  123. public boolean isChildSelectable(int groupPosition, int childPosition) {
  124. return true;
  125. }
  126.  
  127. private static class GroupViewHolder {
  128. TextView title;
  129. ImageView image;
  130. }
  131.  
  132. public class CustomExpandableListView extends ExpandableListView {
  133.  
  134. public CustomExpandableListView(Context context) {
  135. super(context);
  136. }
  137.  
  138. @Override
  139. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  140. heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
  141. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  142. }
  143.  
  144. }
  145.  
  146. }
  147.  
  148. public class ParentAdapter extends BaseExpandableListAdapter {
  149.  
  150. private Objects child;
  151. private LayoutInflater inflater;
  152.  
  153. public ParentAdapter(Context context, Objects child) {
  154. this.child = child;
  155. this.inflater = LayoutInflater.from(context);
  156. }
  157.  
  158. @Override
  159. public Object getChild(int groupPosition, int childPosition) {
  160. return child.arrayList.get(childPosition);
  161. }
  162.  
  163. @Override
  164. public long getChildId(int groupPosition, int childPosition) {
  165. return childPosition;
  166. }
  167.  
  168. @Override
  169. public View getChildView(int groupPosition, int childPosition,
  170. boolean isLastChild, View convertView, ViewGroup parent) {
  171. View layout = convertView;
  172. final Objects o = (Objects) getChild(0, childPosition);
  173.  
  174. ChildViewHolder holder;
  175.  
  176. if (layout == null) {
  177. layout = inflater.inflate(R.layout.item_to_inflate, parent, false);
  178.  
  179. holder = new ChildViewHolder();
  180. holder.title = (TextView) layout.findViewById(R.id.title);
  181. layout.setTag(holder);
  182. } else {
  183. holder = (ChildViewHolder) layout.getTag();
  184. }
  185.  
  186. holder.title.setText(o.title.trim());
  187.  
  188. return layout;
  189. }
  190.  
  191. @Override
  192. public int getChildrenCount(int groupPosition) {
  193. return child.arrayList.size();
  194. }
  195.  
  196. @Override
  197. public Object getGroup(int groupPosition) {
  198. return child;
  199. }
  200.  
  201. @Override
  202. public int getGroupCount() {
  203. return 1;
  204. }
  205.  
  206. @Override
  207. public long getGroupId(int groupPosition) {
  208. return groupPosition;
  209. }
  210.  
  211. @Override
  212. public View getGroupView(int groupPosition, boolean isExpanded,
  213. View convertView, ViewGroup parent) {
  214. View layout = convertView;
  215. GroupViewHolder holder;
  216.  
  217. if (layout == null) {
  218. layout = inflater.inflate(R.layout.item_to_inflate, parent, false);
  219. holder = new GroupViewHolder();
  220.  
  221. holder.image = (ImageView) layout.findViewById(R.id.image);
  222. holder.title = (TextView) layout.findViewById(R.id.title);
  223. layout.setTag(holder);
  224. } else {
  225. holder = (GroupViewHolder) layout.getTag();
  226. }
  227. holder.title.setText(o.title.trim());
  228.  
  229. return layout;
  230. }
  231.  
  232. @Override
  233. public boolean hasStableIds() {
  234. return true;
  235. }
  236.  
  237. @Override
  238. public boolean isChildSelectable(int groupPosition, int childPosition) {
  239. return true;
  240. }
  241.  
  242. private static class GroupViewHolder {
  243. TextView title;
  244. ImageView image;
  245. }
  246.  
  247. private static class ChildViewHolder {
  248. TextView title;
  249. }
  250.  
  251. }
  252.  
  253. if (groupToExpand == groupPosition && childToExpand == childPosition) {
  254. elv.expandGroup(childToExpand);
  255. }
  256.  
  257. if (arrayList!= null && !arrayList.isEmpty()) {
  258. RootAdapter adapter = new RootAdapter(this, arrayList);
  259. elv.setAdapter(adapter);
  260. // this selects the correct group, but doesn't expand the child.
  261. elv.setSelectedChild(groupToExpand, childToExpand, true);
  262. elv.expandGroup(groupToExpand); // this works.
  263.  
  264. adapter.groupToExpand = groupToExpand;
  265. adapter.childToExpand = childToExpand;
  266. adapter.notifyDataSetChanged();
  267. }
  268.  
  269. public class RootAdapter extends BaseExpandableListAdapter {
  270.  
  271. private Object root;
  272.  
  273. private final LayoutInflater inflater;
  274.  
  275. public class Entry {
  276. public final CustExpListview cls;
  277. public final SecondLevelAdapter sadpt;
  278.  
  279. public Entry(CustExpListview cls, SecondLevelAdapter sadpt) {
  280. this.cls = cls;
  281. this.sadpt = sadpt;
  282. }
  283. }
  284.  
  285. public Entry[] lsfirst;
  286.  
  287. // you can change the constructor depending on which listeners you wan't to use.
  288. public RootAdapter(Context context, Object root, ExpandableListView.OnGroupClickListener grpLst,
  289. ExpandableListView.OnChildClickListener childLst, ExpandableListView.OnGroupExpandListener grpExpLst) {
  290. this.root = root;
  291. this.inflater = LayoutInflater.from(context);
  292.  
  293. lsfirst = new Entry[root.children.size()];
  294.  
  295. for (int i = 0; i < root.children.size(); i++) {
  296. final CustExpListview celv = new CustExpListview(context);
  297. SecondLevelAdapter adp = new SecondLevelAdapter(root.children.get(i));
  298. celv.setAdapter(adp);
  299. celv.setGroupIndicator(null);
  300. celv.setOnChildClickListener(childLst);
  301. celv.setOnGroupClickListener(grpLst);
  302. celv.setOnGroupExpandListener(grpExpLst);
  303.  
  304. lsfirst[i] = new Entry(celv, adp);
  305. }
  306.  
  307. }
  308.  
  309. @Override
  310. public Object getChild(int groupPosition, int childPosition) {
  311. return childPosition;
  312. }
  313.  
  314. @Override
  315. public long getChildId(int groupPosition, int childPosition) {
  316. return childPosition;
  317. }
  318.  
  319. @Override
  320. public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
  321. View convertView, ViewGroup parent) {
  322. // second level list
  323. return lsfirst[groupPosition].cls;
  324. }
  325.  
  326. @Override
  327. public int getChildrenCount(int groupPosition) {
  328. return 1;
  329. }
  330.  
  331. @Override
  332. public Object getGroup(int groupPosition) {
  333. return root.children.get(groupPosition);
  334. }
  335.  
  336. @Override
  337. public int getGroupCount() {
  338. return root.children.size();
  339. }
  340.  
  341. @Override
  342. public long getGroupId(int groupPosition) {
  343. return groupPosition;
  344. }
  345.  
  346. @Override
  347. public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
  348. ViewGroup parent) {
  349.  
  350. // first level
  351.  
  352. View layout = convertView;
  353. GroupViewHolder holder;
  354. final Object item = (Object) getGroup(groupPosition);
  355.  
  356. if (layout == null) {
  357. layout = inflater.inflate(R.layout.item_root, parent, false);
  358. holder = new GroupViewHolder();
  359. holder.title = (TextView) layout.findViewById(R.id.itemRootTitle);
  360. layout.setTag(holder);
  361. } else {
  362. holder = (GroupViewHolder) layout.getTag();
  363. }
  364.  
  365. holder.title.setText(item.title.trim());
  366.  
  367. return layout;
  368. }
  369.  
  370. private static class GroupViewHolder {
  371. TextView title;
  372. }
  373.  
  374. @Override
  375. public boolean hasStableIds() {
  376. return true;
  377. }
  378.  
  379. @Override
  380. public boolean isChildSelectable(int groupPosition, int childPosition) {
  381. return true;
  382. }
  383. }
  384.  
  385. public class CustExpListview extends ExpandableListView {
  386.  
  387. public CustExpListview(Context context) {
  388. super(context);
  389.  
  390. }
  391.  
  392. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  393. // the value (2000) should not be fixed and be calculated
  394. // as follows: cell_height x root_items_count x root_items_children_count
  395. heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
  396. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  397. }
  398.  
  399. @Override
  400. protected void onDetachedFromWindow() {
  401. try {
  402. super.onDetachedFromWindow();
  403. } catch (IllegalArgumentException e) {
  404. // TODO: Workaround for http://code.google.com/p/android/issues/detail?id=22751
  405. }
  406. }
  407. }
  408.  
  409. public class SecondLevelAdapter extends BaseExpandableListAdapter {
  410.  
  411. public Object child;
  412.  
  413. public SecondLevelAdapter(Object child) {
  414. this.child = child;
  415. }
  416.  
  417. @Override
  418. public Object getChild(int groupPosition, int childPosition) {
  419. return child.children.get(groupPosition).children.get(childPosition);
  420. }
  421.  
  422. @Override
  423. public long getChildId(int groupPosition, int childPosition) {
  424. return childPosition;
  425. }
  426.  
  427. // third level
  428. @Override
  429. public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
  430. View convertView, ViewGroup parent) {
  431. View layout = convertView;
  432. final Object item = (Object) getChild(groupPosition, childPosition);
  433.  
  434. ChildViewHolder holder;
  435.  
  436. if (layout == null) {
  437. layout = inflater.inflate(R.layout.item_child, parent, false);
  438.  
  439. holder = new ChildViewHolder();
  440. holder.title = (TextView) layout.findViewById(R.id.itemChildTitle);
  441. layout.setTag(holder);
  442. } else {
  443. holder = (ChildViewHolder) layout.getTag();
  444. }
  445.  
  446. holder.title.setText(item.title.trim());
  447.  
  448. return layout;
  449. }
  450.  
  451. @Override
  452. public int getChildrenCount(int groupPosition) {
  453. return child.children.get(groupPosition).children.size();
  454. }
  455.  
  456. @Override
  457. public Object getGroup(int groupPosition) {
  458. return child.children.get(groupPosition);
  459. }
  460.  
  461. @Override
  462. public int getGroupCount() {
  463. return child.children.size();
  464. }
  465.  
  466. @Override
  467. public long getGroupId(int groupPosition) {
  468. return groupPosition;
  469. }
  470.  
  471. // Second level
  472. @Override
  473. public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
  474. ViewGroup parent) {
  475. View layout = convertView;
  476. ViewHolder holder;
  477.  
  478. final Object item = (Object) getGroup(groupPosition);
  479.  
  480. if (layout == null) {
  481. layout = inflater.inflate(R.layout.item_parent, parent, false);
  482. holder = new ViewHolder();
  483. holder.title = (TextView) layout.findViewById(R.id.itemParentTitle);
  484. layout.setTag(holder);
  485. } else {
  486. holder = (ViewHolder) layout.getTag();
  487. }
  488.  
  489. holder.title.setText(item.title.trim());
  490.  
  491. return layout;
  492. }
  493.  
  494. @Override
  495. public void registerDataSetObserver(DataSetObserver observer) {
  496. super.registerDataSetObserver(observer);
  497. }
  498.  
  499. @Override
  500. public void unregisterDataSetObserver(DataSetObserver observer) {
  501. Log.d("SecondLevelAdapter", "Unregistering observer");
  502. if (observer != null) {
  503. super.unregisterDataSetObserver(observer);
  504. }
  505. }
  506.  
  507. @Override
  508. public boolean hasStableIds() {
  509. return true;
  510. }
  511.  
  512. @Override
  513. public boolean isChildSelectable(int groupPosition, int childPosition) {
  514. return true;
  515. }
  516.  
  517. private static class ViewHolder {
  518. TextView title;
  519. }
  520.  
  521. private static class ChildViewHolder {
  522. TextView title;
  523. }
  524.  
  525. }
  526.  
  527. @Override
  528. protected void onCreate(Bundle savedInstanceState) {
  529. super.onCreate(savedInstanceState);
  530.  
  531. final List<Object> objects = yourItems;
  532. if (!objects.isEmpty()) {
  533. final ExpandableListView elv = (ExpandableListView) findViewById(R.id.yourExpandableListView);
  534. /* Item click listeners below */
  535.  
  536. // First level items in the ExpandableListView
  537. elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
  538. @Override
  539. public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
  540. long id) {
  541. // TODO: whatever you need
  542. return false /* or true depending on what you need */;
  543. }
  544. });
  545.  
  546. // Second level items in the ExpandableListView
  547. ExpandableListView.OnGroupClickListener grpLst = new ExpandableListView.OnGroupClickListener() {
  548. @Override
  549. public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
  550. long id) {
  551. // TODO: whatever you need
  552. return false /* or true depending on what you need */;
  553. }
  554. };
  555.  
  556. // Third (and last) level items in the ExpandableListView
  557. ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
  558. @Override
  559. public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
  560. int childPosition, long id) {
  561. // TODO: whatever you need
  562. return false /* or true depending on what you need */;
  563. }
  564. };
  565.  
  566. ExpandableListView.OnGroupExpandListener grpExpLst = new ExpandableListView.OnGroupExpandListener() {
  567. @Override
  568. public void onGroupExpand(int groupPosition) {
  569. /* this one is not required of course, you can delete it from the RootAdapter Constructor
  570. * it is just an example as to how to implement Listeners on the second level items */
  571. }
  572. };
  573.  
  574. final RootAdapter adapter = new RootAdapter(this, objects, grpLst, childLst, grpExpLst);
  575. elv.setAdapter(adapter);
  576. }
  577. }
  578.  
  579. public class Object {
  580. public String title; // use getters and setters instead
  581. public List<Object> children; // same as above
  582.  
  583. public Object() {
  584. children = new ArrayList<Object>();
  585. }
  586. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement