2

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;
    }
}

3 Answers3

1
switch (position) {
        case 0:
            FragmentUniversities tab1 = new FragmentUniversities();
            return tab1;
        case 1:
            FragmentColleges tab2 = new FragmentColleges();
            return tab2;
        case 2:
            FragmentSchool tab3 = new FragmentSchool();
            return tab3;

        case 3:
            FragmentOthers tab4 = new FragmentOthers();
            return tab4;

        default:
            return null;
    }
johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • Hi, it doesn't make any difference the outcome of going a specific tab e.g. colleges clicked on the main activity should get me to colleges tab in second activity. Thank you anyways. – Jalal Atsakzai Jul 20 '16 at 13:55
  • ok , can you post the code when you calling the second activity? – johnrao07 Jul 20 '16 at 13:57
1

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) {

        }
    });
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
  • Thank you so much, that worked for me. However if i wanted apply the same process to other tabs, for instance if school is clicked it should take me to school. – Jalal Atsakzai Jul 20 '16 at 14:04
  • If you want school,write intent.putExtra("position",2); and if you want your last tab, write intent.putExtra("position",3); – Dhruvi Jul 20 '16 at 14:10
  • Once again thank you very much. really appreciate your help! – Jalal Atsakzai Jul 20 '16 at 18:56
  • i didn't try this until now and it doesn't seem to be working. what I have done is: intent.putExtra("position",1); intent.putExtra("position",2); intent.putExtra("position",3); context.startActivity(intent); and how to do i set the mPager.setcurrenitem... for it to work such as press uni > goes to uni tab, press college > takes you to college tab , > press school > takes your to school tab. – Jalal Atsakzai Aug 08 '16 at 09:18
  • Your SlidingTabLayout is Custom view extending TabLayout? – Dhruvi Aug 08 '16 at 09:49
  • yeh i added that, it still didn't make any difference unfortunately. However, the first time you instructed me and it only worked for one tab, meaning all cards where linked to the second position "college" in activity two. – Jalal Atsakzai Aug 08 '16 at 12:17
0

I have found the solution as the problem was with the intents and it will only call the last intent but it's been a while since i posted this question. In addition to Drv's solution, I made a for loop and placed the intents in there.

//give whichever position you want to set for ex: 0 for clicking universities or 1 for clicking colleges and so on.
        for(int i =0; i <=coverlist.size(); i++){
            if(getAdapterPosition()==0){
                intent.putExtra("position",0);
                context.startActivity(intent);
                break;
            }else if (getAdapterPosition()==1){
                intent.putExtra("position",1);
                context.startActivity(intent);
                break;
            }
            else if (getAdapterPosition()==2){
                intent.putExtra("position",2);
                context.startActivity(intent);
                break;
            }else{
                intent.putExtra("position",3);
                context.startActivity(intent);
                break;
            }
        }

This allowed me to navigate to the correct tabs.