2swan

Tab 예제 본문

Programming/Android Example

Tab 예제

2swan 2023. 8. 9. 16:44

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"
    android:orientation="vertical"
    tools:context=".MainActivity3">


        <com.google.android.material.tabs.TabLayout
            android:id="@+id/layout_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            app:tabMode="scrollable"
            app:tabTextColor="#000000"
            app:tabSelectedTextColor="#D18282"
            app:tabIndicatorColor="#89E48D" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

fragment_tab1.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"
    android:background="#112233"
    tools:context=".Tab1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:text="Tab1Fragment" />

</FrameLayout>

 

fragment_tab2.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"
    android:background="#81A5C1"
    tools:context=".Tab2Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:text="Tab2Fragment" />

</FrameLayout>

 

fragment_tab3.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"
    android:background="#CFAC79"
    tools:context=".Tab3Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:text="Tab3Fragment" />

</FrameLayout>

main.activity

public class MainActivity3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        TabLayout tabLayout = findViewById(R.id.layout_tab);
        ViewPager2 viewPager2 = findViewById(R.id.pager_content);

        ContentPagerAdapter contentPagerAdapter = new ContentPagerAdapter(this);
        viewPager2.setAdapter(contentPagerAdapter);

        List<String> tabElement = Arrays.asList("tab1","tab2","tab3");

        //tabLayout viewPager2 연결
        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                TextView textView = new TextView(MainActivity3.this);
                textView.setText(tabElement.get(position));
                tab.setCustomView(textView);
            }
        }).attach();
    }
}

 

ContentPagerAdapter.java

public class ContentPagerAdapter extends FragmentStateAdapter {
    private  int mPageCount = 3;

    //생성자
    public ContentPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position){
            case  0:
                Tab1Fragment tab1Fragment = new Tab1Fragment();
                return tab1Fragment;
            case  1:
                Tab2Fragment tab2Fragment = new Tab2Fragment();
                return tab2Fragment;
            case  2:
                Tab3Fragment tab3Fragment = new Tab3Fragment();
                return tab3Fragment;
            default: return  null;
        }
    }

    @Override
    public int getItemCount() {
        return mPageCount;
    }
}

결과 값

'Programming > Android Example' 카테고리의 다른 글

Progressbar  (0) 2023.08.09
음악 듣기 예제  (0) 2023.08.09
슬라이드 예제 2  (0) 2023.08.09
가로(세로) 슬라이드  (0) 2023.08.09
RecyclerView 예제2  (0) 2023.08.09