0

コンテナアクティビティにありますが、タブが表示されないタブレイアウトでタブを表示する必要があります。このプロジェクトが役立つ場合は、ログインアクティビティ、マップアクティビティ、登録アクティビティの3つのアクティビティもあります。TabLayoutのタブが表示されない

注:マップフラグメントはマップアクティビティとは異なります。

Container.java

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    viewPager = (ViewPager) findViewById(viewPager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
    tabLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      tabLayout.setupWithViewPager(viewPager); 
     } 
    }); 
} 

private void setupViewPager(ViewPager viewPager) { 

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 

    adapter.addFragment(new MapFragment(), "Map"); 
    adapter.addFragment(new Chat(),"Chat"); 

    viewPager.setAdapter(adapter); 

} 

class ViewPagerAdapter extends FragmentPagerAdapter{ 

    private List<Fragment> mFragment = new ArrayList<>(); 
    private List<String> mFragmentTitle = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { return null; } 

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

    public void addFragment(Fragment fragment, String fragmentTitle){ 

     mFragment.add(fragment); 
     mFragmentTitle.add(fragmentTitle); 

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

activity_container.xml

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

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAlignment="center" 
     android:background="@android:color/darker_gray" 
     app:layout_scrollFlags="scroll|enterAlways" 
     android:minHeight="22dp" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabGravity="fill" 
     /> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
</android.support.design.widget.CoordinatorLayout> 

チャットフラグメント Chat.java

public class Chat extends Fragment { 

public Chat(){ 

} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

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

}

activity_chat.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_chat" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.downy.gpslocation.Chat"> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hey there this is chat" 
    android:layout_centerInParent="true"/> 
</RelativeLayout> 

ありがとうございました。

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

アダプターが空であることを言って、0に戻ってきているので:私はあなたのコードViewPagerAdapterで見た

答えて

1

を得ました。また

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

は、所与の位置の正しい断片を戻すために、nullではありません。

0

何かがあなたが持っている必要があります同様にgetCount return 0とのgetItem return null

+0

これらはオーバーライドメソッドであり、私はそれらを使用しないため、0またはnullを返します。 – TOderMAl

+0

それらを使用して、表示するフラグメント数とアダプタ数を返し、 'getPageTitle'は表示したいタイトルを返します –

関連する問題