viewPager = new android.support.v4.view.ViewPager(this);
viewPager.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
MyPagerAdapter adapter = new MyPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
base.addView(viewPager);
tabLayout = new android.support.design.widget.TabLayout(this);
tabLayout.setTabG ravity(tabLayout.GRAVITY_FILL);
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#000000"));
///You can change tab underline(indicator) color
from here
tabLayout.setTabTextColors(Color.parseColor("#00ffffff"),///Tab color when not selected Color.parseColor("#000000"));///Tab color when selected
///You can change tab text color from here
final int[ ] tabIcons = {
R.drawable.ic_1,
R.drawable.ic_2,
R.drawable.ic_3,
R.drawable.ic_4
};
///here ic_1, ic_2, etc. are the names of icons which you added in image manager
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
cod.addView(tabLayout);
}
private class MyPagerAdapter extends android.support.v4.view.PagerAdapter {
public int getCount() {
return 4;
}
@Override public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.empty, null);
LinearLayout container = (LinearLayout) v.findViewById(R.id.linear1);
if (position == 0) {
ViewGroup parent = (ViewGroup) layout1.getParent();
if (parent != null) {
parent.removeView(layout1);
}container.addView(layout1);
} else if (position == 1) {
ViewGroup parent = (ViewGroup) layout2.getParent();
if (parent != null) {
parent.removeView(layout2);
}
container.addView(layout2);
} else if (position == 2) {
ViewGroup parent = (ViewGroup) layout3.getParent();
if (parent != null) {
parent.removeView(layout3);
}
container.addView(layout3);
} else if (position == 3) {
ViewGroup parent = (ViewGroup) layout4.getParent();
if (parent != null) {
parent.removeView(layout4);
}
container.addView(layout4);
}
collection.addView(v, 0);
return v;
}
@Override public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
trash.addView((View) view);
}
@Override public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "HOME";
case 1:
return "SETTINGS";
case 2:
return "PROFILE";
case 3:
return "MUSIC";
default:
return null;
}
///you can change the name of tabs from HOME, SETTINGS, PROFILE, etc you want to
}
@Override public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);}
@Override public Parcelable saveState() {
return null;
}
}
android.support.v4.view.ViewPager viewPager;
android.support.design.widget.TabLayout tabLayout;
private void foo() {
///add 4 linearV with height=WRAP_CONTENT and width = MATCH_PARENT and name that linears id (layout1, layout2, layout3, layout4)
///add 2 linearV or H with height and width = WRAP_CONTENT and name their id (base and trash)
///add one more linearH on the topp of all linear and name its id (cod), it will show yout tabs
///base and trash should be set in between cod and that 4 linears
///add anything to that 4 linear will show in the screen as different when particular tab will be clicked
///now add a custom layout (empty) and add a linear V or H with height and width = MATCH_PARENT
Komentar