Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. package com.example.tchatocampus;
  2.  
  3. import android.os.Bundle;
  4. import android.support.design.widget.FloatingActionButton;
  5. import android.support.design.widget.Snackbar;
  6. import android.view.View;
  7. import android.support.design.widget.NavigationView;
  8. import android.support.v4.view.GravityCompat;
  9. import android.support.v4.widget.DrawerLayout;
  10. import android.support.v7.app.ActionBarDrawerToggle;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.support.v7.widget.Toolbar;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15.  
  16. public class MainActivity extends AppCompatActivity
  17. implements NavigationView.OnNavigationItemSelectedListener {
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  24. setSupportActionBar(toolbar);
  25.  
  26. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  27. fab.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View view) {
  30. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  31. .setAction("Action", null).show();
  32. }
  33. });
  34.  
  35. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  36. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  37. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  38. drawer.addDrawerListener(toggle);
  39. toggle.syncState();
  40.  
  41. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  42. navigationView.setNavigationItemSelectedListener(this);
  43. }
  44.  
  45. @Override
  46. public void onBackPressed() {
  47. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  48. if (drawer.isDrawerOpen(GravityCompat.START)) {
  49. drawer.closeDrawer(GravityCompat.START);
  50. } else {
  51. super.onBackPressed();
  52. }
  53. }
  54.  
  55. @Override
  56. public boolean onCreateOptionsMenu(Menu menu) {
  57. // Inflate the menu; this adds items to the action bar if it is present.
  58. getMenuInflater().inflate(R.menu.main, menu);
  59. return true;
  60. }
  61.  
  62. @Override
  63. public boolean onOptionsItemSelected(MenuItem item) {
  64. // Handle action bar item clicks here. The action bar will
  65. // automatically handle clicks on the Home/Up button, so long
  66. // as you specify a parent activity in AndroidManifest.xml.
  67. int id = item.getItemId();
  68.  
  69. //noinspection SimplifiableIfStatement
  70. if (id == R.id.action_settings) {
  71. return true;
  72. }
  73.  
  74. return super.onOptionsItemSelected(item);
  75. }
  76.  
  77. @SuppressWarnings("StatementWithEmptyBody")
  78. @Override
  79. public boolean onNavigationItemSelected(MenuItem item) {
  80. // Handle navigation view item clicks here.
  81. int id = item.getItemId();
  82.  
  83. if (id == R.id.nav_aspect) {
  84. // Handle the camera action
  85.  
  86. } else if (id == R.id.nav_help) {
  87.  
  88. } else if (id == R.id.nav_cgu) {
  89.  
  90. } else if (id == R.id.nav_about) {
  91.  
  92. } else if (id == R.id.nav_version) {
  93.  
  94. }
  95.  
  96. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  97. drawer.closeDrawer(GravityCompat.START);
  98. return true;
  99. }
  100. }
  101.  
  102.  
  103. package elv.iso.com.expandablelistview;
  104.  
  105.  
  106. import android.os.Bundle;
  107. import android.view.View;
  108. import android.widget.ExpandableListAdapter;
  109. import android.widget.ExpandableListView;
  110. import android.widget.Toast;
  111.  
  112. import java.util.ArrayList;
  113. import java.util.HashMap;
  114. import java.util.List;
  115.  
  116. import androidx.appcompat.app.AppCompatActivity;
  117.  
  118. public class MainActivity extends AppCompatActivity {
  119.  
  120. private ExpandableListView expandableListView;
  121. private ExpandableListAdapter expandableListAdapter;
  122. private List<String> expandableListNombres;
  123. private HashMap<String, Contacto> listaContactos;
  124. private int lastExpandedPosition = -1;
  125.  
  126.  
  127. @Override
  128. protected void onCreate(Bundle savedInstanceState) {
  129. super.onCreate(savedInstanceState);
  130. setContentView(R.layout.activity_main);
  131.  
  132. init();
  133.  
  134. expandableListView.setAdapter(expandableListAdapter);
  135.  
  136. expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
  137. @Override
  138. public void onGroupExpand(int groupPosition) {
  139. if(lastExpandedPosition != -1 && groupPosition != lastExpandedPosition){
  140. expandableListView.collapseGroup(lastExpandedPosition);
  141. }
  142. lastExpandedPosition = groupPosition;
  143. }
  144. });
  145.  
  146. /**
  147. expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  148. @Override
  149. public void onGroupCollapse(int groupPosition) {
  150. Toast.makeText(getBaseContext(),"List Collapsed:" +
  151. expandableListNombres.get(groupPosition),Toast.LENGTH_SHORT).show();
  152. }
  153. });
  154. expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  155. @Override
  156. public boolean onChildClick(ExpandableListView parent, View v,
  157. int groupPosition, int childPosition, long id) {
  158. Toast.makeText(getBaseContext(),
  159. expandableListNombres.get(groupPosition) +
  160. " ---> " + listaContactos.get(expandableListNombres.get(groupPosition))
  161. ,Toast.LENGTH_SHORT).show();
  162. return false;
  163. }
  164. });
  165. */
  166.  
  167. }
  168.  
  169. private void init() {
  170. this.expandableListView = findViewById(R.id.expandableListView);
  171. this.listaContactos = getContactos();
  172. this.expandableListNombres = new ArrayList<>(listaContactos.keySet());
  173. this.expandableListAdapter = new CustomExpandableListAdapter(this,
  174. expandableListNombres, listaContactos);
  175.  
  176. }
  177.  
  178.  
  179. private HashMap<String, Contacto> getContactos() {
  180. HashMap<String, Contacto> listaC = new HashMap<>();
  181.  
  182. listaC.put("Juan", new Contacto("111-111-111",
  183. "juan@correo.com", "Calle 1, 11 - 4D", R.drawable.img_11));
  184. listaC.put("Sara", new Contacto("222-222-222",
  185. "sara@correo.com", "Calle 2, 1 - 1A", R.drawable.img_22));
  186. listaC.put("Raquel", new Contacto("333-333-333",
  187. "raquel@correo.com", "Calle 3, 3 - 6B", R.drawable.img_33));
  188. listaC.put("Juan 2", new Contacto("444-444-444",
  189. "juan@correo.com", "Calle 1, 11 - 4D", R.drawable.img_11));
  190. listaC.put("Sara 2", new Contacto("555-555-555",
  191. "sara@correo.com", "Calle 2, 1 - 1A", R.drawable.img_22));
  192. listaC.put("Raquel 2", new Contacto("666-666-666",
  193. "raquel@correo.com", "Calle 3, 3 - 6B", R.drawable.img_33));
  194. listaC.put("Juan 3", new Contacto("777-777-777",
  195. "juan@correo.com", "Calle 1, 11 - 4D", R.drawable.img_11));
  196. listaC.put("Sara 3", new Contacto("888-888-888",
  197. "sara@correo.com", "Calle 2, 1 - 1A", R.drawable.img_22));
  198. listaC.put("Raquel 3", new Contacto("999-999-999",
  199. "raquel@correo.com", "Calle 3, 3 - 6B", R.drawable.img_33));
  200.  
  201.  
  202. return listaC;
  203. }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement