Projekt-Anfragen: Tel: 07022/9319004 info@CodeDocu.de Software Entwicklung in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

Android Example Code : Tabbed Activity with Different Fragments

 

Diese Activity hat mehrere Tabs und verschiedene untergeordnete Fragment Seiten

 

 

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">

    <
androidx.viewpager.widget.ViewPager
       
android:id="@+id/view_pager"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <
com.google.android.material.appbar.AppBarLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:theme="@style/Theme.Demo_TabbedActivity02.AppBarOverlay">

        <
TextView
           
android:id="@+id/title"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:gravity="center"
           
android:minHeight="?actionBarSize"
           
android:padding="@dimen/appbar_padding"
           
android:text="Demo with Tabs"
           
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <
com.google.android.material.tabs.TabLayout
           
android:id="@+id/tabs"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:background="@color/black"
           
android:foregroundTint="@color/white" />

    </
com.google.android.material.appbar.AppBarLayout>


</
androidx.coordinatorlayout.widget.CoordinatorLayout>

 

 

 

 

Und mainactivity.java Code

package com.example.demo_tabbedactivity02;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.demo_tabbedactivity02.ui.main.SectionsPagerAdapter;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_main);

       
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
       
ViewPager viewPager = findViewById(R.id.view_pager);

       
       
viewPager.setAdapter(sectionsPagerAdapter);
       
TabLayout tabs = findViewById(R.id.tabs);
       
tabs.setupWithViewPager(viewPager);

    }
}

 

 

 

 

Frament_page1.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".Page1">

   
<!-- TODO: Update blank fragment layout -->
   
<TextView
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:text="Page1"
       
android:textSize="22sp"
       
android:background="@color/cardview_dark_background"
       
/>

</
FrameLayout>

 

 

Die verschiedenen Fragments warden geladen durch den SectionsPagerAdapter

 

Zu beachten ist hier, dass in der GetItem() Methode jeweils das passende Fragment zurückgeben wird

 

package com.example.demo_tabbedactivity02.ui.main;

import android.content.Context;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.demo_tabbedactivity02.Page1;
import com.example.demo_tabbedactivity02.Page2;
import com.example.demo_tabbedactivity02.Page3;
import com.example.demo_tabbedactivity02.R;

/**
 * A [FragmentPagerAdapter] that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

   
@StringRes
   
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2,R.string.tab_text_3 };
   
private final Context mContext;

   
public SectionsPagerAdapter(Context context, FragmentManager fm) {
       
super(fm);
       
mContext = context;
    }

   
@Override
   
public Fragment getItem(int position) {
       
// getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
       
switch (position) {
           
case 0:
               
return  new Page1();
           
case 1:
               
return  new Page2();
           
case 2:
               
return  new Page3();
           
default:
               
return  new PlaceholderFragment();
        }

    }

   
@Nullable
    @Override
   
public CharSequence getPageTitle(int position) {
       
return mContext.getResources().getString(TAB_TITLES[position]);
    }

   
@Override
   
public int getCount() {
       
// Show 2 total pages.
       
return 3;
    }
}

 

 

Mobile
»
Android Code: Tabbed Activity mit verschiedenen Fragments
»
Android Example Code : Tabbed Activity with Different Fragments
»
Android Tabbed Activity mit ViewPage2 Adapter
»
Android: ein Video transparent erstellen mit VideoView, MediaPlayer und FrameLayout
»
Android: mit to open an alarm an activity even in locked screen
»
Android: mit einem Alarm eine Activity öffnen
»
Android : einen kleine Musik Media Abspieler erstellen
»
Android: Layout Constraint zu RelativeLayout
»
Android Studio : Button mit Event erstellen
»
Samsung S6 ScreenMirroring ganz einfach

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Info@CodeDocu.de