Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private ListView lstListView;
  4.  
  5. //private FragmentManager fragmentManager = getSupportFragmentManager();
  6.  
  7. private Fragment fragmentOne = new Fragment1();
  8.  
  9. int currentFragment = 0;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  16. setSupportActionBar(toolbar);
  17.  
  18. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  19. fab.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  23. .setAction("Action", null).show();
  24. }
  25. });
  26.  
  27. /* if(savedInstanceState == null){
  28.  
  29. Fragment1 fragment1 = new Fragment1();
  30. Fragment2 fragment2 = new Fragment2();
  31. Fragment3 fragment3 = new Fragment3();
  32.  
  33. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  34. fragmentTransaction.add(R.id.content_main, fragment1);
  35. fragmentTransaction.commit();
  36.  
  37. }*/
  38.  
  39. lstListView = (ListView)findViewById(R.id.lstListView);
  40.  
  41. final String[] lista = new String[]{"Fragment 1", "Fragment 2", "Fragment 3"};
  42.  
  43. final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);
  44.  
  45. lstListView.setAdapter(adapter);
  46.  
  47. lstListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  48. @Override
  49. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  50.  
  51. if( position == 0){
  52.  
  53. oneFragment();
  54.  
  55. } else if (position == 1){
  56.  
  57.  
  58. } else if (position == 2){
  59.  
  60.  
  61. }
  62. }
  63. });
  64.  
  65.  
  66. }
  67.  
  68. @Override
  69. public boolean onCreateOptionsMenu(Menu menu) {
  70. // Inflate the menu; this adds items to the action bar if it is present.
  71. getMenuInflater().inflate(R.menu.menu_main, menu);
  72. return true;
  73. }
  74.  
  75. @Override
  76. public boolean onOptionsItemSelected(MenuItem item) {
  77. // Handle action bar item clicks here. The action bar will
  78. // automatically handle clicks on the Home/Up button, so long
  79. // as you specify a parent activity in AndroidManifest.xml.
  80. int id = item.getItemId();
  81.  
  82. //noinspection SimplifiableIfStatement
  83. if (id == R.id.action_settings) {
  84. return true;
  85. }
  86.  
  87. return super.onOptionsItemSelected(item);
  88. }
  89.  
  90.  
  91. private void oneFragment() {
  92.  
  93. if (currentFragment != 1) {
  94.  
  95. FragmentManager fragmentManager = getSupportFragmentManager();
  96. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  97. fragmentTransaction.replace(R.id.lnlContent, fragmentOne).commit();
  98. currentFragment = 1;
  99.  
  100. }
  101.  
  102. }
  103.  
  104. <LinearLayout
  105. android:id="@+id/lnlContent"
  106. android:layout_width="match_parent"
  107. android:layout_height="match_parent"
  108. android:orientation="vertical">
  109.  
  110. <TextView
  111. android:layout_width="wrap_content"
  112. android:layout_height="wrap_content"
  113. android:text="Testing List View With Fragments"
  114. android:layout_gravity="center_horizontal"
  115. android:textSize="20dp"/>
  116.  
  117. <ListView
  118. android:id="@+id/lstListView"
  119. android:layout_width="match_parent"
  120. android:layout_height="match_parent">
  121. </ListView>
  122.  
  123. </LinearLayout>
  124.  
  125. <LinearLayout
  126. android:id="@+id/lnlTest"
  127. android:layout_width="match_parent"
  128. android:layout_height="match_parent"
  129. android:orientation="vertical">
  130.  
  131. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement