MixedTabActivity.java CallsFragment.java UTab.java UTab.java +
Android - Java Apache Config Windows Batch Linux Bash CSS Gradle HTML ini File iOS - Swift Java JavaScript JSON Format Kotlin LESS Markdown MySQL PHP Plain Text Properties File Rust Go Lang SCSS Swift XML
public class MixedTabActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Button btnAction = (Button) findViewById(R.id.btnAction);
final UTab tab = new UTab(this, R.id.viewPager, R.id.tabLayout);
tab.add(CallsFragment.class, "Calls", R.drawable.ic_call_black_24dp);
tab.add(MessagesFragment.class, "Messages", R.drawable.ic_message_black_24dp);
tab.add(ContactsFragment.class, "Contacts", R.drawable.ic_people_black_24dp);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tab.setIcon(2, R.drawable.ic_android_black_24dp);
tab.setTitle(2, "Android");
}
});
}
}
public class CallsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calls, container, false);
}
}
public class UTab {
public static class Item {
private Fragment fragment;
private String title;
private int icon;
public Item(Class<? extends Fragment> fragmentClass, String title, int icon) {
try {
this.fragment = fragmentClass.newInstance();
this.title = title;
this.icon = icon;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public Item(Fragment fragment, String title, int icon) {
this.fragment = fragment;
this.title = title;
this.icon = icon;
}
public Fragment getFragment() {
return fragment;
}
public String getTitle() {
return title;
}
public int getIcon() {
return icon;
}
public void setTitle(String value) {
title = value;
}
public void setIcon(int value) {
icon = value;
}
}
public class Adapter extends FragmentPagerAdapter {
private List<Item> items = new ArrayList<>();
public Adapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
return items.get(position).getFragment();
}
public Item getRawItem(int position) {
return items.get(position);
}
@Override
public int getCount() {
return items.size();
}
public void add(Item item) {
items.add(item);
}
@Override
public CharSequence getPageTitle(int position) {
return items.get(position).getTitle();
}
}
private ViewPager viewPager;
private TabLayout tabLayout;
private Adapter adapter;
public UTab(AppCompatActivity activity, int viewPagerId, int tabLayoutId) {
View view = activity.getWindow().getDecorView();
viewPager = (ViewPager) view.findViewById(viewPagerId);
tabLayout = (TabLayout) view.findViewById(tabLayoutId);
adapter = new Adapter(activity.getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
private void refreshIcons() {
for (int i=0; i<adapter.getCount(); i++) {
int itemIcon = adapter.getRawItem(i).getIcon();
if (itemIcon != 0) {
tabLayout.getTabAt(i).setIcon(adapter.getRawItem(i).getIcon());
}
}
}
public void add(Class<? extends Fragment> fragmentClass, String title, int icon) {
Item item = new Item(fragmentClass, title, icon);
adapter.add(item);
adapter.notifyDataSetChanged();
refreshIcons();
}
public void add(Class<? extends Fragment> fragmentClass, String title) {
add(fragmentClass, title, 0);
}
public void add(Class<? extends Fragment> fragmentClass, int icon) {
add(fragmentClass, null, icon);
}
public void setIcon(int index, int icon) {
adapter.getRawItem(index).setIcon(icon);
refreshIcons();
}
public void setTitle(int index, String title) {
adapter.getRawItem(index).setTitle(title);
adapter.notifyDataSetChanged();
refreshIcons();
}
}
public class UTab {
public static class Item {
private Fragment fragment;
private String title;
private int icon;
public Item(Class<? extends Fragment> fragmentClass, String title, int icon) {
try {
this.fragment = fragmentClass.newInstance();
this.title = title;
this.icon = icon;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public Item(Fragment fragment, String title, int icon) {
this.fragment = fragment;
this.title = title;
this.icon = icon;
}
public Fragment getFragment() {
return fragment;
}
public String getTitle() {
return title;
}
public int getIcon() {
return icon;
}
public void setTitle(String value) {
title = value;
}
public void setIcon(int value) {
icon = value;
}
}
public class Adapter extends FragmentPagerAdapter {
private List<Item> items = new ArrayList<>();
public Adapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
return items.get(position).getFragment();
}
public Item getRawItem(int position) {
return items.get(position);
}
@Override
public int getCount() {
return items.size();
}
public void add(Item item) {
items.add(item);
}
@Override
public CharSequence getPageTitle(int position) {
return items.get(position).getTitle();
}
}
private ViewPager viewPager;
private TabLayout tabLayout;
private Adapter adapter;
public UTab(AppCompatActivity activity, int viewPagerId, int tabLayoutId) {
View view = activity.getWindow().getDecorView();
viewPager = (ViewPager) view.findViewById(viewPagerId);
tabLayout = (TabLayout) view.findViewById(tabLayoutId);
adapter = new Adapter(activity.getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
private void refreshIcons() {
for (int i=0; i<adapter.getCount(); i++) {
int itemIcon = adapter.getRawItem(i).getIcon();
if (itemIcon != 0) {
tabLayout.getTabAt(i).setIcon(adapter.getRawItem(i).getIcon());
}
}
}
public void add(Class<? extends Fragment> fragmentClass, String title, int icon) {
Item item = new Item(fragmentClass, title, icon);
adapter.add(item);
adapter.notifyDataSetChanged();
refreshIcons();
}
public void add(Class<? extends Fragment> fragmentClass, String title) {
add(fragmentClass, title, 0);
}
public void add(Class<? extends Fragment> fragmentClass, int icon) {
add(fragmentClass, null, icon);
}
public void setIcon(int index, int icon) {
adapter.getRawItem(index).setIcon(icon);
refreshIcons();
}
public void setTitle(int index, String title) {
adapter.getRawItem(index).setTitle(title);
adapter.notifyDataSetChanged();
refreshIcons();
}
}