Anton Makov
Anton Makov

Reputation: 845

Tablayout inside fragment

I'm trying to add tablayout in one of my fragments, but despite everything I found in the web nothing seems to work properly. In other fragments I don't want to see the tab layout.

HomeFragment xml:

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


    <android.support.design.widget.TabLayout
        android:id="@+id/home_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabMode="scrollable"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/home_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

</LinearLayout> 

HomeFragment code:

public class HomeFragment extends Fragment {

private TabLayout _homeTabLayout;
private ViewPager _homeViewPager;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.home_fragment,container,false);

    final TabLayout tabLayout = (TabLayout) v.findViewById(R.id.home_tabs);
    final ViewPager viewPager = (ViewPager) v.findViewById(R.id.home_viewpager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFragment(new SyncedEpisodesTabFragment(), "One");
    adapter.addFragment(new AvailableEpisodesTabFragment(), "Two");

    viewPager.setAdapter(adapter);

    tabLayout.setupWithViewPager(viewPager);


    return v;

}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter  adapter = new ViewPagerAdapter (getChildFragmentManager());
    adapter.addFragment(new AvailableEpisodesTabFragment(), "PHOTOS");
    adapter.addFragment(new SyncedEpisodesTabFragment(), "HI-FIVES");
    viewPager.setAdapter(adapter);
}

private class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(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);
        mFragmentTitleList.add(title);
    }

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

}

app bar main .xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context="com.follow.series.activities.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

                 <!--I'm inserting the tab layout here-->


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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />



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

In this case I'm just seeing the viewpager with the text without the tabs When I add the tab layout inside Toolbar I'm seeing the tabs all the time.

Upvotes: 2

Views: 7046

Answers (3)

Amjad Mughal
Amjad Mughal

Reputation: 1

I suggest using the following attribute in your App Bar Layout

app:elevation="0dp"

Upvotes: 0

Gulov Khurshed
Gulov Khurshed

Reputation: 71

for removing shadow I use the following line of code. You can add this in your styles like I did:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowContentOverlay">@null</item>
</style>

Or write this line as toolbar's attribute:

<android.support.v7.widget.Toolbar
android:windowContentOverlay="@null"
/>

And I suggest using the following attribute in your frame layout, because without this attribute views in fragment can overlap your toolbar

app:layout_behavior="@string/appbar_scrolling_view_behavior"

Upvotes: 0

Anton Makov
Anton Makov

Reputation: 845

With the answer of Muhammad Hafiq Iqmal I managed to fix this issue by adding another linearlayout that contain both the tooltip and the fragment container

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context="com.follow.series.activities.MainActivity">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />




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



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


    </LinearLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

I haven't noticed this, but now I have an issue with the toolbar, it has a shadow below at the bottom, even though it has beneath a tab layout. I tried to play with the elivation attribute both in tab layout and also in toolbar element but without success strange shadow

Upvotes: 3

Related Questions