0% found this document useful (0 votes)
44 views2 pages

XMLNS: XMLNS: :layout - Width :orientation :layout - Height XMLNS: :context

This document contains code for an Android application that implements tab navigation using a TabLayout and ViewPager. It defines a TabFragment class that inflates a layout containing a TabLayout and ViewPager. The TabLayout is linked to the ViewPager so that swiping between tabs updates the page, and vice versa. An adapter is set on the ViewPager to provide the tab content fragments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

XMLNS: XMLNS: :layout - Width :orientation :layout - Height XMLNS: :context

This document contains code for an Android application that implements tab navigation using a TabLayout and ViewPager. It defines a TabFragment class that inflates a layout containing a TabLayout and ViewPager. The TabLayout is linked to the ViewPager so that swiping between tabs updates the page, and vice versa. An adapter is set on the ViewPager to provide the tab content fragments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

<LinearLayout xmlns:android="http://schemas.android.

com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.auto.autohome1.TabFragment">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="scrollable"
android:background="#9f3036"
app:tabIndicatorColor="#ffffff"
app:tabSelectedTextColor="#ae6061"
app:tabTextColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>

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

</LinearLayout>

package com.auto.autohome1;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabFragment extends Fragment {

public TabLayout tabLayout;


public ViewPager viewPager;
public TabFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

View v = inflater.inflate(R.layout.fragment_tab,container,false);
tabLayout=(TabLayout)v.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
viewPager=(ViewPager)v.findViewById(R.id.viewpager);

viewPager.addOnPageChangeListener(new
ViewPager.OnPageChangeListener() {

@Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels) {
//nothing here
}

@Override
public void onPageSelected(int position) {
}

@Override
public void onPageScrollStateChanged(int state) {
//nothing here
}

});
//set an adpater

viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return v;
}

You might also like