Guest User

Untitled

a guest
Feb 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. ExpandableListAdapter listAdapter;
  4. ExpandableListView expListView;
  5. List<String> listDataHeader;
  6. HashMap<String, List<String>> listDataChild;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12.  
  13. // get the listview
  14. expListView = findViewById(R.id.lvExp);
  15.  
  16. // preparing list data
  17. prepareListData();
  18. listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
  19.  
  20. // setting list adapter
  21. expListView.setAdapter(listAdapter);
  22.  
  23. // PEGA A POSIÇÃO DO ITEM DA LISTA
  24. expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  25. @Override
  26. public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
  27. Log.i("LOG", "Posição: " + childPosition);
  28.  
  29. parent.collapseGroup(0);
  30.  
  31. return false;
  32. }
  33. });
  34.  
  35. // TROCA O ICONE DE EXPANSAO
  36. expListView.setGroupIndicator(getResources().getDrawable(R.drawable.ic_launcher_background));
  37. }
  38.  
  39. private void prepareListData() {
  40. listDataHeader = new ArrayList<>();
  41. listDataChild = new HashMap<>();
  42.  
  43. // Adding child data
  44. listDataHeader.add("Lista de itens");
  45.  
  46. // Adding child data
  47. List<String> itens = new ArrayList<String>();
  48. itens.add("Item 1");
  49. itens.add("Item 2");
  50. itens.add("Item 3");
  51. itens.add("Item 4");
  52. itens.add("Item 5");
  53.  
  54. listDataChild.put(listDataHeader.get(0), itens); // Header, Child data
  55.  
  56. }
  57. }
  58.  
  59. public class ExpandableListAdapter extends BaseExpandableListAdapter {
  60. String headerTitle;
  61. private Context ctx;
  62. private List<String> listDataHeader; // header titles
  63. // child data in format of header title, child title
  64. private HashMap<String, List<String>> listDataChild;
  65.  
  66. public ExpandableListAdapter(Context ctx, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
  67. this.ctx = ctx;
  68. this.listDataHeader = listDataHeader;
  69. this.listDataChild = listChildData;
  70. }
  71.  
  72. @Override
  73. public Object getChild(int groupPosition, int childPosititon) {
  74. return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
  75. }
  76.  
  77. @Override
  78. public long getChildId(int groupPosition, int childPosition) {
  79. return childPosition;
  80. }
  81.  
  82. @Override
  83. public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  84.  
  85. final String childText = (String) getChild(groupPosition, childPosition);
  86.  
  87. if (convertView == null) {
  88. LayoutInflater infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  89. convertView = infalInflater.inflate(R.layout.item_lista, null);
  90. }
  91.  
  92. TextView tvItemLista = convertView.findViewById(R.id.tvw_item_nome);
  93. tvItemLista.setText(childText);
  94.  
  95. return convertView;
  96. }
  97.  
  98. @Override
  99. public int getChildrenCount(int groupPosition) {
  100. return this.listDataChild.get(this.listDataHeader.get(groupPosition))
  101. .size();
  102. }
  103.  
  104. @Override
  105. public Object getGroup(int groupPosition) {
  106. return this.listDataHeader.get(groupPosition);
  107. }
  108.  
  109. @Override
  110. public int getGroupCount() {
  111. return this.listDataHeader.size();
  112. }
  113.  
  114. @Override
  115. public long getGroupId(int groupPosition) {
  116. return groupPosition;
  117. }
  118.  
  119. @Override
  120. public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  121. headerTitle = (String) getGroup(groupPosition);
  122. if (convertView == null) {
  123. LayoutInflater infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  124. convertView = infalInflater.inflate(R.layout.list_group, null);
  125. }
  126.  
  127. TextView tvTitleHeader = convertView.findViewById(R.id.tvw_title_header);
  128. tvTitleHeader.setTypeface(null, Typeface.BOLD);
  129. tvTitleHeader.setText(headerTitle);
  130.  
  131. return convertView;
  132. }
  133.  
  134. public void changeHeader(String text){
  135. headerTitle = text;
  136. }
  137.  
  138. @Override
  139. public boolean hasStableIds() {
  140. return false;
  141. }
  142.  
  143. @Override
  144. public boolean isChildSelectable(int groupPosition, int childPosition) {
  145. return true;
  146. }
  147. }
Add Comment
Please, Sign In to add comment