Skip to content

Instantly share code, notes, and snippets.

@RobertApikyan
Created March 12, 2016 19:16
Show Gist options
  • Save RobertApikyan/d291988dc7c67f063763 to your computer and use it in GitHub Desktop.
Save RobertApikyan/d291988dc7c67f063763 to your computer and use it in GitHub Desktop.
package com.example.robert.materialtabs;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
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.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
SlidingTabLayout tab ;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab= (SlidingTabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
tab.setDistributeEvenly(true);
tab.setCustomTabView(R.layout.custom_tab_view ,R.id.tab_textView , SlidingTabLayout.TabViewSpecifier.IMAGE_VIEW);
tab.setViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
int[] images = {R.drawable.a, R.drawable.b, R.drawable.c};
String[] titles ={"Frag1" , "Frag2" , "Frag3"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Frag frag = new Frag(position);
return frag;
}
@Override
public int getCount() {
return 3;
}
public int getImage(int position){
return images[position];
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
class Frag extends Fragment{
private int pos=1;
Frag(int position){
this.pos = position;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag , container , false);
TextView textView = (TextView)v.findViewById(R.id.textView);
textView.setText("this is fragment "+pos);
return v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment