3

Android 탭 레이아웃 및 PagerAdapter가있는 각 탭마다 다른 조각을로드하려고합니다. 탭이 잘 작동하지만 해당 조각이 내 화면에로드되지 않습니다. 코드는 다음과 같습니다.조각이 포함 된 탭 레이아웃

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class Tab1 extends Fragment { 

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

각 조각 XML에 대해 총 3 개의 탭 클래스가 생성되었습니다. 여기에 조각 내 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" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Tab 1" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 
</RelativeLayout> 

내 활동 클래스 :

import android.app.ActionBar; 
import android.content.Intent; 
import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.widget.Toast; 

import java.util.logging.Logger; 

public class DashboardActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{ 

    //This is our tablayout 
    private TabLayout tabLayout; 

    //This is our viewPager 
    private ViewPager viewPager; 

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

     //Adding toolbar to the activity 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Initializing the tablayout 
     tabLayout = (TabLayout) findViewById(R.id.tabLayout); 

     //Adding the tabs using addTab() method 
     tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title")); 
     tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title")); 
     tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     //Initializing viewPager 
     viewPager = (ViewPager) findViewById(R.id.pager); 

     //Creating our pager adapter 
     Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount()); 

     //Adding adapter to pager 
     viewPager.setAdapter(adapter); 

     //Adding onTabSelectedListener to swipe views 
     tabLayout.setOnTabSelectedListener(this); 
    } 

    @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) { 

    } 
} 

내 호출기 어댑터 클래스는 다음과 같습니다

public class Pager extends FragmentStatePagerAdapter { 

    //integer to count number of tabs 
    int tabCount; 

    //Constructor to the class 
    public Pager(FragmentManager fm, int tabCount) { 
     super(fm); 
     //Initializing tab count 
     this.tabCount= tabCount; 
    } 

    //Overriding method getItem 
    @Override 
    public Fragment getItem(int position) { 
     //Returning the current tabs 
     switch (position) { 
      case 0: 
       Tab1 tab1 = new Tab1(); 
       return tab1; 
      case 1: 
       Tab2 tab2 = new Tab2(); 
       return tab2; 
      case 2: 
       Tab3 tab3 = new Tab3(); 
       return tab3; 
      default: 
       return null; 
     } 
    } 

    //Overriden method getCount to get the number of tabs 
    @Override 
    public int getCount() { 
     return tabCount; 
    } 
} 

활동 대시 보드의 레이아웃 파일 :

<android.support.design.widget.TabLayout 
    android:id="@+id/tabLayout" 
    android:layout_width="368dp" 
    android:layout_height="wrap_content" 
    app:tabMode="fixed" 
    tools:layout_editor_absoluteX="8dp" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="8dp" 
    android:onClick="loadOption"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@android:color/white" 
    tools:layout_editor_absoluteY="0dp" 
    tools:layout_editor_absoluteX="8dp" 
    tools:ignore="MissingConstraints" /> 

Styles.xml 파일 :

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

</resources> 

어떤 제안.

+0

'activity_dashboard'레이아웃이 맞습니까? 즉,'ViewPager'는 보이지 않으며, 아무것도 다루지 않습니다. 또한,'Fragment' 레이아웃의'TextView'는 배경과 블렌딩되지 않는 텍스트 컬러를 가지고 있습니까? –

+0

확실하지 않습니다. 그러나 나는 스타일과 레이아웃 파일 코드를 추가했다. –

+0

'ViewPager'의'layout_width'를'match_parent'로 변경하십시오. 또한'ConstraintLayout'을 아직 사용하지 않았으므로, 가중치가 작동한다는 것을 확신하지 못합니다. 그것에 0이 아닌'layout_height'을 설정해야 할 수도 있습니다. –

답변

1

이 줄을 추가하고 당신은 당신이 당신의 활동의 onCreate() 방법에 다음 줄을 추가해야

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
      tabLayout)); 
+0

onCreate 메소드에 추가하여 행운을 빕니다. –

0

사용 tabLayout.setupWithViewPager(viewPager)

............. 
    ..................... 

    //Adding adapter to pager 
    viewPager.setAdapter(adapter); 

    tabLayout.setupWithViewPager(viewPager) 

    //Adding onTabSelectedListener to swipe views 
    tabLayout.setOnTabSelectedListener(this); 

    ........... 
    .................... 
2

을 갈 수 있습니다

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

당신은 모든 것을 잘했으나 viewpag를 묶는 한 줄을 놓쳤다. 탭 레이아웃 또는 디스플레이 탭이있는 경우

tabLayout.setupWithViewPager(viewPager); 

시도해보십시오.

0

시도해보십시오. 도움이되기를 바랍니다. 그것이 없으면

PagerAdapter 클래스

public class HomeViewPagerAdapter extends FragmentPagerAdapter { 
private List<Fragment> mFragmentList = new ArrayList<>(); 
private List<String> mTitleList = new ArrayList<>(); 

public HomeViewPagerAdapter(FragmentManager manager) { 
    super(manager); 
} 

@Override 
public Fragment getItem(int position) { 
    return mFragmentList.get(position); 
} 


@Override 
public int getCount() { 
    return mFragmentList.size(); 
} 

public void addFragment(Fragment fragment, String title) { 
    mFragmentList.add(fragment); 
    mTitleList.add(title); 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    return mTitleList.get(position); 
} 

}

MyActivity 클래스

HomeViewPagerAdapter homeViewPagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager()); 

    // add whichever fragments you want to add 
    homeViewPagerAdapter.addFragment(new Fragment(), "yourfragment"); 

    viewpager.setAdapter(homeViewPagerAdapter); 
    viewpager.setOffscreenPageLimit(2); 
    tabLayout.setupWithViewPager(viewpager); 
2

하는있는 LinearLayout 또는 다른 뷰 그룹 내에서 메인 레이아웃을 넣어. LinearLayout의 경우 :

뷰 페이지의 layout_height은 0dp입니다. match_parentlayout_width으로 변경하십시오.

이 (가) linear_layout weight="1"

또한 작업 표시 줄 아래로보기 호출기에 대한 android:layout_marginTop="?android:actionBarSize"을 추가 할 수 있습니다 제거합니다.

관련 문제