Android Tablayout With ViewPager, Fragments And Constraint layout

 res/layout/activity_main.xml
<? xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:id="@+id/constraintLayout"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        app:tabTextColor="@color/white"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabIndicatorColor="@color/colorAccent"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        android:layout_gravity="bottom">
    </android.support.design.widget.TabLayout>
   </android.support.v4.view.ViewPager>
</ android.support.constraint.ConstraintLayout>

 res/layout/fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2" />

        <!-- TextViews from 3-13 -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="14" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1" />

        <!-- TextViews from 2-19 -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="20" />

    </android.support.v7.widget.LinearLayoutCompat>
    </ScrollView>
</FrameLayout>

ViewPagerFragmentAdapter .java
package com.example.geekscompete.tablayouttextcolordemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;

public class ViewPagerFragmentAdapter extends FragmentPagerAdapter {

    private SparseArray<Fragment> childFragments=new SparseArray<>();
    private String titles[] = {"ONE","TWO","THREE"};

    public ViewPagerFragmentAdapter(FragmentManager fm) {
        super(fm);
        childFragments.put(0, new BlankFragment());
        childFragments.put(1, new BlankFragment());
        childFragments.put(2, new BlankFragment());
    }

    // This determines the fragment for each tab
    @Override
    public Fragment getItem(int position) {
    return childFragments.get(position);
    }

    // This determines the number of tabs
    @Override
    public int getCount() {
    return childFragments.size();
    }

    // This determines the title for each tab
    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        switch (position) {
            case 0:
                return titles[0];
            case 1:
                return titles[1];
            case 2:
                return titles[2];
            default:
                return null;
        }
    }
}

MainActivity.java
package com.example.geekscompete.tablayouttextcolordemo;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;

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

        viewPager=findViewById(R.id.viewpager);
        tabLayout=findViewById(R.id.tablayout);

        viewPager.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
    }
}

android:layout_gravity="top"
android:layout_gravity="bottom"


No comments:

Post a Comment

Popular Posts