Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to assign different fragments to each tab #5

Open
lyrathaprinces opened this issue Dec 5, 2019 · 2 comments
Open

How to assign different fragments to each tab #5

lyrathaprinces opened this issue Dec 5, 2019 · 2 comments

Comments

@lyrathaprinces
Copy link

Hi ,,,
override fun getItem(position: Int): Fragment = ListItemsFragment.newInstance()
Instead of using same ListItemFragment to all tabs,,,how to use different fragments to each one?

@lyrathaprinces
Copy link
Author

public class HomeColorTabActivity extends AppCompatActivity {
private HomeColorTabAdapter adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
ColorMatchTabLayout var20;
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_colortab_home);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//
String[] colorsArray = this.getResources().getStringArray(R.array.colors);
TypedArray iconsArray = this.getResources().obtainTypedArray(R.array.icons);
String[] textsArray = this.getResources().getStringArray(R.array.texts);
//
int index$iv = 0;
String[] var7 = colorsArray;
int var8 = colorsArray.length;

    for(int var9 = 0; var9 < var8; ++var9) {
        String item$iv = var7[var9];
        int index = index$iv++;
        boolean var13 = false;
        String tabName = textsArray[index];
        int selectedColor = Color.parseColor(item$iv);
        Drawable icon = iconsArray.getDrawable(index);
        var20 = (ColorMatchTabLayout)findViewById(R.id.colorMatchTabLayout);
        var20.addTab(ColorTabAdapter.createColorTab(var20, tabName, selectedColor, icon));

    }
    //
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    FragmentManager var10003 = this.getSupportFragmentManager();
    ColorMatchTabLayout tabLayout = (ColorMatchTabLayout)findViewById(R.id.colorMatchTabLayout);
   adapter = new HomeColorTabAdapter(var10003, tabLayout.count());
    adapter.addFragment(new FragmentHome());
  adapter.addFragment(new FragmentAlbums());
   adapter.addFragment(new FragmentArtist());
 //  adapter.addFragment(new FragmentCategories());
    adapter.addFragment(new FragmentHome());


    viewPager.setAdapter((PagerAdapter)(adapter));
 //   viewPager.setAdapter(adapter);
  // tabLayout.setupWithViewPager(viewPager);
   // viewPager.setCurrentItem(1);//second tab as default

    viewPager.addOnPageChangeListener(new ColorTabLayoutOnPageChangeListener(tabLayout));
  //  viewPager.setCurrentItem(0);
    /////
    ((ColorMatchTabLayout)findViewById(R.id.colorMatchTabLayout)).addOnColorTabSelectedListener((OnColorTabSelectedListener)(new OnColorTabSelectedListener() {
    public void onSelectedTab(@Nullable ColorTab tab) {
        ViewPager var10000 = (ViewPager)findViewById(R.id.viewPager);
        Intrinsics.checkExpressionValueIsNotNull(var10000, "viewPager");
        var10000.setCurrentItem(tab != null ? tab.getPosition() : 0);
        ((ViewPager)findViewById(R.id.viewPager)).setBackgroundColor(tab != null ? tab.getSelectedColor() : ContextCompat.getColor((Context)HomeColorTabActivity.this, R.color.colorPrimary));
       /* var10000 = (ViewPager)findViewById(R.id.viewPager);
        Intrinsics.checkExpressionValueIsNotNull(var10000, "viewPager");
        Drawable var2 = var10000.getBackground();
        Intrinsics.checkExpressionValueIsNotNull(var2, "viewPager.background");
        var2.setAlpha(128);*/

    }

    public void onUnselectedTab(@Nullable ColorTab tab) {
        Log.e("Unselected ", "tab");
    }
}));



    ////


}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;

        default:
            return super.onOptionsItemSelected(menuItem);
    }
    return true;
}

@lyrathaprinces
Copy link
Author

those who are looking for the same ,, i have included switch case in adapter

@OverRide
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentHome();
case 1:
return new FragmentAlbums();
case 2:
return new FragmentArtist();
case 3:
return new FragmentCategories();
default:
// This should never happen. Always account for each position above
return null;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant