How do I make a recyclerview of mine appear on a tab instead of fixed on main?

Asked

Viewed 93 times

0

I created a recyclerview on main which is ok, tbm I created 2 tabs which are ok. But when I change between tabs the Recycler view is fixed on the screen instead of just appearing in tab1. A friend told me I needed to use context but I didn’t even understand what it is, and then someone has a suggestion?

 //RecyclerView

private RecyclerView recyclerView;
private RecyclerView.Adapter recyclerViewAdapter;
private RecyclerView.LayoutManager layoutManager;
String[] materias = {"Português", "Matemática", "Inglês"};
Double[] notas = {7.5, 10.0, 2.7};
Integer[] faltas = {5, 6, 3};

//Fim do RecyclerView

Now inside the Oncreate

 recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
 recyclerViewAdapter = new RecyclerViewAdapter(materias, notas, faltas);
 layoutManager = new LinearLayoutManager(this);
 recyclerView.setLayoutManager(layoutManager);
 recyclerView.setHasFixedSize(true);
 recyclerView.setAdapter(recyclerViewAdapter);

Tabs

    adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);


    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);


    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); 


    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return ContextCompat.getColor(MainActivity.this, R.color.colorPrimary);

            }
        });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);


}
  • Put the code you’re using, it’s better to help you

  • Ready, I put what was relevant.

  • I believe you need to call recyclerView inside the Viewpager Adapter

1 answer

0

Activity will contain one Fragment for each tab. Each Fragment will contain one recyclerview.

Structure:

-activity1|-tab1-fragment1-recyclerview1
          |-tab2-fragment2-recyclerview2

In the matter of execution flow: activity_main.xml contains a widget called viewpager. In Mainactivity.class, you call Viewpager and arrow an Adapter on it. Adapter is a special class that will contain so many fragments within it. Each fragment is an xml and a class, too. Each fragment represents the contents of a tab. Within each xml of Fragments, you create a (widget)recyclerview. Within each class of each fragment, you program recyclerview execution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.