잡동사니

반응형

질문

내 앱을 시작할 때 내 TabLayout과 겹치는 Fragment을 제외하고는 모든 것이 정상입니다. 탭 전환은 괜찮지 만 Fragment 레이아웃이 레이아웃 아래에 있는 대신 전체 화면을 덮는 것 같습니다. 해결책을 알고 계신 분이 있습니까? 스크린샷 : https://imgur.com/a/odeKD0W 추가하는 것을 잊지 않았으면 좋겠어요. 그렇다면 저에게 물어보세요. 감사합니다.

Activity_Main

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tabLayout = findViewById(R.id.tabBar);
        TabItem tabItem1 = findViewById(R.id.tab1);
        TabItem tabItem2 = findViewById(R.id.tab2);
        TabItem tabItem3 = findViewById(R.id.tab3);
        final ViewPager viewPager = findViewById(R.id.viewPager);

        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

        viewPager.setAdapter(pagerAdapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
        viewPager.addOnPageChangeListener(new TabLayout
                .TabLayoutOnPageChangeListener(tabLayout));
    }
}

PagerAdapter class

public class PagerAdapter extends FragmentPagerAdapter {

    private int numOfTabs;
    public PagerAdapter (FragmentManager fm, int numOfTabs) {
        super (fm);
        this.numOfTabs = numOfTabs;
    }
    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new FragmentTab1();
            case 1:
                return new FragmentTab2();
            case 2:
                return new FragmentTab3();
            default:
                return null;
        }

    }

    @Override
    public int getCount() {
        return numOfTabs;
    }
}

Activity_Main xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabBar"
        android:layout_width="409dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab1"
            android:text="Tab 1" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab2"
            android:text="Tab 2" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab3"
            android:text="Tab 3" />
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/tabBar" />
  </androidx.constraintlayout.widget.ConstraintLayout>

 

답변1

당신은 사용하고 있습니다

app:layout_constraintTop_toTopOf="@id/tabBar"

대신에

app:layout_constraintTop_toBottomOf="@id/tabBar"

첫 번째는 Fragment의 상단을 tablayout의 상단에 맞추고 겹쳐집니다. Fragment의 상단은 대신 탭바의 하단으로 제한되어야합니다.



 

 

 

 

출처 : https://stackoverflow.com/questions/62107544/fragment-overlapping-tab-layout

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band