Reputation:
Hi I make an swipe tab app, it works whena i swipe the screen , but i don't see the tabs part. How can i resolve it? Can you help me. Thanks
Here is my XML layout
<RelativeLayout
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="com.example.hakobm.tabdemo.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
app:tabMode="fixed"
app:tabGravity="fill"
/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Here is my MainActivity file
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout= (TabLayout) findViewById(R.id.tablayout);
viewPager= (ViewPager) findViewById(R.id.viewpager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Home(),"Home");
viewPagerAdapter.addFragments(new Top(),"Top");
viewPagerAdapter.addFragments(new Buttom(),"Buttom");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
And this is my adapters part:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> fragments= new ArrayList<>();
private ArrayList<String> tabTitles = new ArrayList<>();
public void addFragments(Fragment fragments, String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
// return "Tab" + position;
}
}
i can't see the reason why my tabs don't available. I added compile 'com.android.support:design:25.3.1' into my grandle.app file
Upvotes: 0
Views: 47
Reputation: 1571
Try this:
Add an id to the appbarlayout like this:
android:id="@+id/appbar"
And add this to the tabs:
android:layout_below="@+id/appbar"
Upvotes: 1