Redas Shuliakas
Redas Shuliakas

Reputation: 55

Android Tablayout,why tabs not moving?

I was following this tutorial https://www.youtube.com/watch?v=zcnT-3F-9JA I've used his code from github, but output is wrong. I've put 3 tabs on the top, and when I press on tab, activity supposed to change, but in reality, nothing happens, I still have only my main_activity on the screen. Hope someone will help. Here is my codes

PS - Yes I have also 3 xml files for every fragment (even I have also main_activity, but I have to find out what's wrong, and then will asign 1tab with main activity). I have also 3 java files for this 3 tabs.

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:background="#FFFFFF"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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


        <TextView
            android:id="@+id/lvltext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/expa"
            android:layout_centerInParent="true"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/leveltext"
            android:textColor="@color/black"
            android:textSize="30sp" />

        <TextView

            android:id="@+id/lvlnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/lvltext"
            android:layout_centerInParent="true"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/levelnum"
            android:textColor="@color/black"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/girl"
            android:layout_width="wrap_content"
            android:layout_height="262dp"
            android:layout_below="@id/lvlnum"
            android:src="@drawable/girl" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/girl"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/button"
            android:textColor="@color/black" />

        <ImageView
            android:id="@+id/girl2"
            android:layout_width="match_parent"
            android:layout_height="258dp"
            android:layout_below="@id/button"
            android:src="@drawable/fitnessmodel" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/girl2"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/button2"
            android:textColor="@color/black" />

        <LinearLayout
            android:layout_below="@id/tabs"
            android:id="@+id/expa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/team_a_score"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/futuracondensed"
                    android:gravity="center"
                    android:paddingBottom="5dp"
                    android:text="0"
                    android:textColor="@color/black"
                    android:textSize="60sp" />
            </LinearLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@color/black" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/team_b_score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/futuracondensed"
                    android:gravity="center"
                    android:paddingBottom="5dp"
                    android:text="100"
                    android:textColor="@color/black"
                    android:textSize="60sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

Java

public class MainActivity extends AppCompatActivity {
    private SectionsPageAdapter mSectionsPageAdapter;

    private ViewPager mViewPager;

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

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "TAB1");
        adapter.addFragment(new Tab2Fragment(), "TAB2");
        adapter.addFragment(new Tab3Fragment(), "TAB3");
        viewPager.setAdapter(adapter);
    }


    @Override
    public void onResume() {
        super.onResume();
    }
}



public class SectionsPageAdapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

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

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

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

Upvotes: 1

Views: 1407

Answers (3)

androholic
androholic

Reputation: 696

use this XML as given in the library u have followed then whatever design u want to add them to the fragment layouts u have taken.things will work fine

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context="com.example.chirag.slidingtabsusingviewpager.MainActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SlidingTabsUsingViewPager"
            android:textSize="20dp"/>


    </android.support.v7.widget.Toolbar>

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

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

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

    </android.support.v4.view.ViewPager>

</LinearLayout>

Upvotes: 2

Android Geek
Android Geek

Reputation: 9225

try this code:

OneFragment.java

OneFragment.java
package info.androidhive.materialtabs.fragments;

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

import info.androidhive.materialtabs.R;


public class OneFragment extends Fragment{

public OneFragment() {
    // Required empty public constructor
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

 }

fragment_one.xml

 fragment_one.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="info.androidhive.materialtabs.fragments.OneFragment">

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/one"
    android:textSize="40dp"
    android:textStyle="bold"
    android:layout_centerInParent="true"/>

</RelativeLayout>

activity_main.xml

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
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="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        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>

MainActivity.java

  package info.androidhive.materialtabs.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import info.androidhive.materialtabs.R;
import info.androidhive.materialtabs.fragments.OneFragment;
import info.androidhive.materialtabs.fragments.ThreeFragment;
import info.androidhive.materialtabs.fragments.TwoFragment;

[![enter image description here][1]][1]public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

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);
    }
  }
 }

enter image description here

and add the main activity calling 3 tabs

     private class MyPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int pos) {
        switch(pos) {
            case 0: return SimpleFragment.newInstance("FirstFragment, Instance 1");
            case 1: return PieView.newInstance("SecondFragment, Instance 1");
            case 2: return PieView1.newInstance("ThirdFragment, Instance 1");
             case 3: return DataSaveDetails.newInstance("ThirdFragment, Instance 1");
              default:
        }
        return null;
    }

it works please try this

Upvotes: 0

Qalb Hussain
Qalb Hussain

Reputation: 21

On your getItem() method. Use Switch and return fragments their instead of adding all the fragments in addFragment method.

    public Fragment getItem(int position){
     switch(position){
       case 0 : return new Tab1Fragment();
       case 1 : return new Tab2Fragment();
       case 2 : return new Tab3Fragment();
     }

    }

Upvotes: 0

Related Questions