Jalal Atsakzai Jalal Atsakzai - 4 months ago 21
Android Question

Android Development: Directing to specific tabs in fragment

This is how the Main Activity looks like. By clicking on each cardview in the main activity it takes me to Activity two which holds fragments with sliding tab layout and recyclerview. The issue i'm having is trying to direct each cardview to its own specific tabs in the second activity rather than a default tab which is the first one. I tried looking at solutions online but it didn't do much help. Any sort of help would be much appreciated, thanks in advance.

Just the clarify, the intent call from the first main activity to the second activity is done through the adapter.

This is the Main Activity

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CoverAdapter adapter;
private List<Covers> coversList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

initCollapsingToolbar();

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

coversList = new ArrayList<>();
adapter = new CoverAdapter(this, coversList);

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);

prepareCovers();

try {
Glide.with(this).load(R.drawable.cover).into((ImageView) findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}


}

private void prepareCovers() {
int[] covers = new int[]{
R.drawable.school};
Covers academiaCover = new Covers("Universities",covers[0]);
coversList.add(academiaCover);

academiaCover = new Covers("Colleges", covers[0]);
coversList.add(academiaCover);

academiaCover = new Covers("School", covers[0]);
coversList.add(academiaCover);

academiaCover = new Covers("Others", covers[0]);
coversList.add(academiaCover);

adapter.notifyDataSetChanged();


}

private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);

// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.app_name));
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
}


This is the second activity

public class ActivityTwo extends AppCompatActivity {

private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private static final int UNIVERSITES = 0;
private static final int COLLEGES = 1;
private static final int SCHOOLS = 2;
private static final int OTHERS = 3;
public static ProgressBar spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_two);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setTitleTextColor(Color.WHITE);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mPager);

spinner = (ProgressBar)findViewById(R.id.progress_bar);
spinner.setVisibility(View.GONE);


}

class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs=getResources().getStringArray(R.array.tabs);
}

@Override
public Fragment getItem(int position) {
Fragment myFragment = null;

switch (position){
case UNIVERSITES:
myFragment = FragmentUniversities.newInstance("","");
break;

case COLLEGES:
myFragment = FragmentColleges.newInstance("","");
break;

case SCHOOLS:
myFragment = FragmentSchool.newInstance("","");
break;
case OTHERS:
myFragment = FragmentOthers.newInstance("","");
break;

}

return myFragment;

}

@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}

@Override
public int getCount() {
return 4;
}
}

Drv Drv
Answer

In your intent send data manually which position of tab you want to display as below:

   Intent intent=new Intent(YOUR_ACTIVITY_CONTEXT,ActivityTwo.class);
   //give whichever position you want to set for ex: 0 for clicking universities or 1 for clicking colleges and so on.
   intent.putExtra("position",1);
   YOUR_ACTIVITY_CONTEXT.startActivity(intent);

And in your ActivityTwo:

   mPager.setCurrentItem(getIntent().getIntExtra("position",0));

Add below lines to get page of respective tabs:

  mTabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });