Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private ListView lstListView;
- //private FragmentManager fragmentManager = getSupportFragmentManager();
- private Fragment fragmentOne = new Fragment1();
- int currentFragment = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- /* if(savedInstanceState == null){
- Fragment1 fragment1 = new Fragment1();
- Fragment2 fragment2 = new Fragment2();
- Fragment3 fragment3 = new Fragment3();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.add(R.id.content_main, fragment1);
- fragmentTransaction.commit();
- }*/
- lstListView = (ListView)findViewById(R.id.lstListView);
- final String[] lista = new String[]{"Fragment 1", "Fragment 2", "Fragment 3"};
- final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);
- lstListView.setAdapter(adapter);
- lstListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- if( position == 0){
- oneFragment();
- } else if (position == 1){
- } else if (position == 2){
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private void oneFragment() {
- if (currentFragment != 1) {
- FragmentManager fragmentManager = getSupportFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.replace(R.id.lnlContent, fragmentOne).commit();
- currentFragment = 1;
- }
- }
- <LinearLayout
- android:id="@+id/lnlContent"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Testing List View With Fragments"
- android:layout_gravity="center_horizontal"
- android:textSize="20dp"/>
- <ListView
- android:id="@+id/lstListView"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </ListView>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/lnlTest"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement