TabHost

유시현·2023년 1월 27일
0

Android

목록 보기
23/34
post-thumbnail

TabHost

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab1"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab2"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab3"/>

            </LinearLayout>

            
        </FrameLayout>

    </LinearLayout>

</TabHost>

TabWidget,FrameLayout id는 @android:id/tabs,@android:id/tabcontent

package com.example.test1;

import android.os.Bundle;
import android.widget.TabHost;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TapHostActivity extends AppCompatActivity {

    TabHost tabHost;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tap_host);
        tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();

        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("TAB1").setContent(R.id.tab1));

        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("TAB2").setContent(R.id.tab2));

        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("TAB3").setContent(R.id.tab3));

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {

            }
        });


    }
}

FragmentTabHost

public class Fragment1 extends Fragment {


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_taphost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>


        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>



</androidx.fragment.app.FragmentTabHost>
public class FragmentTabHostActivity extends AppCompatActivity {

    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tap_host);
        mTabHost = (FragmentTabHost) findViewById(R.id.fragment_taphost);
        mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("frag1").setIndicator("Frag1"), Fragment1.class,null);
        mTabHost.addTab(mTabHost.newTabSpec("frag2").setIndicator("Frag2"), Fragment2.class,null);
    }
}

sticky tab

public class StickyTab extends AppCompatActivity {

    ListView listView;
    ArrayAdapter<String> mAdapter;
    TabHost tabHost;
    TabHost headerHost;
    private static final int TAB_HEADER_POSITION=1;

    static class DummyContentFactory implements TabHost.TabContentFactory {

        Context mContext;

        public DummyContentFactory(Context mContext) {
            this.mContext = mContext;
        }

        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(mContext);
            return tv;
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        listView = (ListView) findViewById(R.id.listview);
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
//        View headerView = getLayoutInflater().inflate(R.layout.view_tab_header,null);
//        listView.addHeaderView(headerView,null,false);
        TabHost.TabContentFactory dummyFactory = new DummyContentFactory(this);
        headerHost = (TabHost) getLayoutInflater().inflate(R.layout.view_tab_header,null);
        headerHost.setup();

        headerHost.addTab(headerHost.newTabSpec("tab1").setIndicator("TAB1").setContent(dummyFactory));
        headerHost.addTab(headerHost.newTabSpec("tab2").setIndicator("TAB2").setContent(dummyFactory));
        headerHost.addTab(headerHost.newTabSpec("tab3").setIndicator("TAB3").setContent(dummyFactory));

        headerHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if(!headerHost.getCurrentTabTag().equals(tabId)){
                    headerHost.setCurrentTabByTag(tabId);
                }
            }
        });

        tabHost = (TabHost) findViewById(R.id.fixed_tabhost);
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1").setContent(dummyFactory));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TAB2").setContent(dummyFactory));
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB3").setContent(dummyFactory));

        tabHost.setVisibility(View.GONE);
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if(!tabHost.getCurrentTabTag().equals(tabId)){
                    tabHost.setCurrentTabByTag(tabId);
                }
                setItem(tabId);
            }
        });

        listView.setAdapter(mAdapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if(i<TAB_HEADER_POSITION){
                    //i는 item의 위치
                    //i<1 i=0일때는 gone
                    tabHost.setVisibility(View.GONE);

                }
                else{
                    //i=1일때 보여짐
                    tabHost.setVisibility(View.VISIBLE);
                }
            }
        });


        setItem("tab1");

    }

    public void setItem(String tabId){
        mAdapter.clear();
        for(int i=0;i<20;i++){
            mAdapter.add(tabId + ":"+i);
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"/>

</TabHost>

view_tab_header.xml

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

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_header_height"/>

    <include layout="@layout/view_tab_header"
        android:id="@+id/fixed_tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
</FrameLayout>

activity_main2.xml

참고
https://www.youtube.com/watch?v=iIjw2L2fC2M&t=1s

profile
안드로이드 ,ios 공부하고 있습니다

0개의 댓글