#

 

mit verschiedenen Seiten: achtung... hier wird zurzeit noch die gleiche View angezeigt

 

In Android werden Seiten mit einem Tab-Reiter als Tabbed Activity bestehend aus einer Activity Seite mit einem TabLayout als Tab-Reiter Element plus ein ViewPager2 Element erstellt,

Das ViewPager2 Element wird durch einen ViewPager2Adapter geladen

 

 

 

ViewPager2Adapter

package com.example.demo_viewpager2;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
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 pageDefault();
        }
    }
   
@Override
   
public int getItemCount() {return 3; }
}

 

 

 

 

MainActivity.java

 

package com.example.demo_viewpager2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.TableLayout;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

   
TabLayout tabLayout;
   
ViewPager2 viewPager2;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_main);
       
tabLayout = findViewById(R.id.tabs);
       
viewPager2 = findViewById(R.id.view_pager);

        
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();
    }
}

 

 

 

 

Layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   
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="31dp"
       
tools:layout_editor_absoluteY="110dp"
       
tools:ignore="MissingConstraints"

       
app:tabMode="scrollable"
       
>

        <
com.google.android.material.tabs.TabItem
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="📌 Alarms" />

        <
com.google.android.material.tabs.TabItem
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text=" Test" />

        <
com.google.android.material.tabs.TabItem
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="Wednesday" />
    </
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="113dp"
       
tools:layout_editor_absoluteY="255dp"
       
android:orientation="horizontal"

       
/>

</
LinearLayout>

 

 

 

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:id="@+id/fragment_page1"
   
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:gravity="center_vertical"
       
android:textSize="20sp"
       
android:textStyle="bold"
       
android:text="This is Page 0" />

</
FrameLayout>

 

 

 

 

 

 

Problem

Dabei werden zurzeit als Fehler die zweite Seite falsch angezeigt

Mobile

.

0123movie.net