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 Code: Tabbed Activity mit verschiedenen Fragments

 

 

 

Code res/layout/activity_main.xml

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



    <
com.google.android.material.tabs.TabLayout
       
android:id="@+id/tabs"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
        
tools:layout_editor_absoluteX="182dp"
       
tools:layout_editor_absoluteY="373dp">
    </
com.google.android.material.tabs.TabLayout>

    <
androidx.viewpager2.widget.ViewPager2
       
android:id="@+id/view_pager"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
tools:layout_editor_absoluteX="145dp"
       
tools:layout_editor_absoluteY="500dp" />

</
LinearLayout>

 

 

 

Java Code: java/project/MainActivity.java

package com.example.demo_viewpager2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

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

        //< get elements >
       
TabLayout tabLayout = findViewById(R.id.tabs);
       
ViewPager2 viewPager2 = findViewById(R.id.view_pager);
       
//</ get elements >


       
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
       
viewPager2.setAdapter(adapter);

       
new TabLayoutMediator(tabLayout, viewPager2,
               
new TabLayoutMediator.TabConfigurationStrategy() {
                   
@Override
                   
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText(
"Tab " + (position + 1));
                    }
                }).attach();
    }
}

 

 

Adapter:

The adapter loads the fragments into the pageviewer2

package com.example.demo_viewpager2;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

public class ViewPagerAdapter extends FragmentStateAdapter {

   
public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity)
    {
       
super(fragmentActivity);
    }

   
@NonNull
    @Override
   
public Fragment createFragment(int position) {

       
switch (position) {
           
case 0:
               
return  new page1();
           
case 1:
               
return  new page2();
           
default:
               
return  new page3();
        }
    }
   
@Override
   
public int getItemCount() {return 3; }
}

 

 

 

Framgent page 1

Standard new Fragments

 

Xml Code framgent_page1

<?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:background="@color/white"
       
android:textSize="33sp"
       
/>

</
FrameLayout>

 

 

 

 

Java code page 1

 

package com.example.demo_viewpager2;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link
Fragment} subclass.
 * Use the {@link
page1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class page1 extends Fragment {

   
// TODO: Rename parameter arguments, choose names that match
   
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
   
private static final String ARG_PARAM1 = "param1";
   
private static final String ARG_PARAM2 = "param2";

   
// TODO: Rename and change types of parameters
   
private String mParam1;
   
private String mParam2;

   
public page1() {
       
// Required empty public constructor
   
}

   
/**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param
param1 Parameter 1.
     * @param
param2 Parameter 2.
     * @return A new instance of fragment page1.
     */
    //
TODO: Rename and change types and number of parameters
   
public static page1 newInstance(String param1, String param2) {
       
page1 fragment = new page1();
       
Bundle args = new Bundle();
       
args.putString(ARG_PARAM1, param1);
       
args.putString(ARG_PARAM2, param2);
       
fragment.setArguments(args);
       
return fragment;
    }

   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
if (getArguments() != null) {
           
mParam1 = getArguments().getString(ARG_PARAM1);
           
mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

   
@Override
   
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            
Bundle savedInstanceState) {
       
// Inflate the layout for this fragment
        
return inflater.inflate(R.layout.fragment_page1, container, false);
    }
}

 

 

 

 

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