๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
APP/ANDROID

[๋ถ€์ŠคํŠธ์ฝ”์Šค]์•ˆ๋“œ๋กœ์ด๋“œ ํƒญ(Tab)

by mingzoo 2020. 2. 28.

ํƒญ ๋ฒ„ํŠผ(TAB)

ํƒญ์€ sdk๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์œ„์ ฏ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.

ํƒญ์€ ์นด์นด์˜คํ†ก, ์ธ์Šคํƒ€๊ทธ๋žจ, ํŽ˜์ด์Šค๋ถ๊ณผ ๊ฐ™์€ sns์™€ ๋งŽ์€ ์–ดํ”Œ์˜ ํ™”๋ฉด ๊ตฌ์„ฑ์— ํ™œ๋ฐœํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.

 

ํƒญ์˜ ๊ตฌ์„ฑ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” activity๋ฒ„ํŠผ๊ณผ famelayout(ํ”„๋ ˆ์ž„ ๋ ˆ์ด์•„์›ƒ)์ด๋ผ๋Š” ์œ„์ ฏ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ์ „์ฒด์ ์ธ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•˜์—ฌ ๋ฒ„ํŠผ ์„ ํƒ์‹œ, ํ•ด๋‹น๋˜๋Š” fragment๋ฅผ ๋„์šฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋œ๋‹ค.

๋”ฐ๋ผ์„œ, ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ทธ ๋ฒ„ํŠผ์— ํ•ด๋‹น๋˜๋Š” fragment๊ฐ€ ํ”„๋ ˆ์ž„๋ ˆ์ด์•„์›ƒ์— ์‹คํ–‰
์•„๋ž˜์˜ ์ฝ”๋“œ ์ฐธ์กฐ

 

MainActivity.java

package com.example.actionbar_ex;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ScrollingTabContainerView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {
    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();

        getSupportFragmentManager().beginTransaction().add(R.id.container, fragment1).commit();

        TabLayout tabs = (TabLayout)findViewById(R.id.tabs);

        tabs.addTab(tabs.newTab().setText("์นœ๊ตฌ"));
        tabs.addTab(tabs.newTab().setText("์ผ๋Œ€์ผ์ฑ„ํŒ…"));
        tabs.addTab(tabs.newTab().setText("๊ธฐํƒ€"));

        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                //๋ช‡๋ฒˆ์งธํƒญ์ธ์ง€ ์ธ๋ฑ์Šค๊ฐ’์ด ๋„˜์–ด์˜ด

                Fragment selected = null;
                if(position==0){
                    selected = fragment1;
                }
                else if(position ==1){
                    selected = fragment2;
                }
                else if(position == 2){
                    selected = fragment3;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.container, selected).commit();
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimaryDark"
                android:theme="@style/ThemeOverlay.AppCompat.Dark"
                android:id="@+id/toolbar">

            </androidx.appcompat.widget.Toolbar>

            <com.google.android.material.tabs.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_light"
                app:tabMode="fixed"
                app:tabGravity="fill"
                app:tabTextColor="@color/colorPrimary"
                app:tabSelectedTextColor="@color/colorAccent">

            </com.google.android.material.tabs.TabLayout>

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/container">

        </FrameLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>

fragment.xml (1,2,3 ํ˜•์‹ ๋™์ผ)

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="593dp"
        android:gravity="center"
        android:text="@string/fragment1"
        android:textColor="#000000"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/next">

    </Button>

</LinearLayout>

Fragment.java(1,2,3 ๋™์ผ)

package com.example.actionbar_ex;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootview = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);

        return rootview;
    }
}

 


!๋‚ด๊ฐ€ ๋ฐœ๊ฒฌํ•œ ์˜ค๋ฅ˜!

xmlํŒŒ์ผ์—์„œ android:text="next"์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด์„œ

strings.xml์ด๋ผ๋Š” ํŒŒ์ผ์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์ŠคํŠธ๋ง์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

fragment.xmlํŒŒ์ผ์—์„œ android:text="@string/next"๋กœ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค.

<resources>
    <string name="app_name">Actionbar_ex</string>
    <string name="fragment2">fragment2</string>
    <string name="next">next</string>
    <string name="fragment3">fragment3</string>
    <string name="fragment1">fragment1</string>
</resources>

728x90