Skip to content

Instantly share code, notes, and snippets.

@mberberoglu
Last active September 29, 2018 05:11
Show Gist options
  • Save mberberoglu/9524793 to your computer and use it in GitHub Desktop.
Save mberberoglu/9524793 to your computer and use it in GitHub Desktop.
Android Swipe Views with Tab
//Main Container
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
// Tabs node
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Detail Tab"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
</RelativeLayout>
//Adapter
package com.mustafab.NinePx.Adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.mustafab.NinePx.Fragment.Place.DetailFragment;
import com.mustafab.NinePx.Fragment.Place.TimelineFragment;
import com.mustafab.NinePx.Fragment.Place.TopListFragment;
import java.util.ArrayList;
public class PlaceViewPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
private String[] titles = {"Detail", "Timeline", "Top Posts"};
public PlaceViewPagerAdapter(FragmentManager fm) {
super(fm);
fragments.add(new DetailFragment());
fragments.add(new TimelineFragment());
fragments.add(new TopListFragment());
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
//Fragment
package com.mustafab.NinePx.Fragment.Place;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mustafab.NinePx.R;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_place_detail, container, false);
return rootView;
}
}
//Initializer
package com.mustafab.NinePx.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mustafab.NinePx.Adapter.PlaceViewPagerAdapter;
import com.mustafab.NinePx.Api.PlaceApi;
import com.mustafab.NinePx.R;
public class PlaceFragment extends Fragment {
PlaceViewPagerAdapter placeTabAdapter;
ViewPager viewPager;
public PlaceFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_place, container, false);
placeTabAdapter = new PlaceViewPagerAdapter(getFragmentManager());
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(placeTabAdapter);
return rootView;
}
}
//Eger tab isimlerini görmek isterseniz main container'i güncellemeniz gerekmektedir
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment