0

I'm working on an Android Studio app and I would like to hava an indicator of in which fragment user is. There is 4 pages, each one with some fragments and I would like to highlight the fragment where user is (the yellow bar)

enter image description here

Actually, the yellow bar don't move when fragment change. Is it possible to make changement automatic or have I to programm the bar for each fragment ? I don't know if it can help, but I use androidx and ViewPager2

public void setUp(final FormActivity context, final Toolbar toolbar, final Spinner spinner, final DrawerLayout drawerLayout, final ViewPager2 pager, final SlidingTabLayout tabs, FragmentActivity supportFragmentActivity) {
        this.drawerLayout = drawerLayout;
        this.pager = pager;
        this.tabs = tabs;
        this.progressBar = view.findViewById(R.id.progressBar);

In the .xml file :

<pack.myrhs.views.NVHeaderTextView
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginEnd="@dimen/drawer_side_margin
android:layout_marginRight="@dimen/drawer_side_margin"
android:layout_marginBottom="@dimen/drawer_divider_margin"
android:text="loading..."
android:textColor="@color/textIconsColor"
android:visibility="gone" />    

And that's the NVHeaderTextView class:

public class NVHeaderTextView extends AppCompatTextView {
    public NVHeaderTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if(isInEditMode())
            return;

        String fontName = "fonts/Roboto-Medium.ttf";

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
        setTextSize(14);
        setTextColor(Color.argb((int) (255 * 1.00), 255, 255, 255));


        int flags = getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG
                | Paint.ANTI_ALIAS_FLAG;
        setPaintFlags(flags);

    }
}

UPDATE

I'm using SlidingTabLayout, so the TabLayoutMediator doesn't work (it seems to me). I'm now, using the setViewPager() method:

@Override
        protected void onPostExecute(Void params) {

            pager.setAdapter(adapter);

            tabs.setViewPager(pager);

            if (adapter != null)
                adapter.notifyDataSetChanged();

            progressBar.setVisibility(View.GONE);

            drawerLayout.closeDrawers();

            cancel(true);
        }

Thanks for advance.

  • Can you share your code? if you set it up correctly using ViewPager and TabLayout, it should work. – AagitoEx Jul 12 '22 at 11:33
  • I don't really know what part of the code is needed. The app wasn't mine at first and there is too much files for I gave you all. All I can say you is that there is a fragment for each page (A,B,C,D), there is an adapter where I build the ArraList frags which one I add the four fragments. The progress bar is declared in my navigation drawer – Eva D'hordain Jul 12 '22 at 11:55
  • Doesn't this answer question? https://stackoverflow.com/questions/55372259/how-to-use-tablayout-with-viewpager2-in-android – momvart Jul 12 '22 at 12:55
  • @momvart thanks, it help me by lead me to understand that it's the tabLayout which do what I need. But I'm using SlidingTabLayout, so I can't apply the TabLayoutMediator – Eva D'hordain Jul 12 '22 at 13:55

1 Answers1

0

SOLUTION

I finally solve my problem by switching my SlindingTabLayout to TabLayout.

In my setUp() method for my pages:

// Creation of the link between the tabLayout and the pager
    tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
        @Override
        public void onTabSelected (TabLayout.Tab tab){
            pager.setCurrentItem(tab.getPosition());
        }
        @Override
        public void onTabUnselected (TabLayout.Tab tab){
        }
        @Override
        public void onTabReselected (TabLayout.Tab tab){
        }
    });
    pager.registerOnPageChangeCallback(new OnPageChangeCallback() {
        @Override
        public void onPageSelected ( int position){
            tabs.selectTab(tabs.getTabAt(position));
        }
    });

With the init of the tabs for each page:

    drawerPageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

            if (currentPage != position) {

                if (position != 1) { //not page2
                    //TOOLBAR
                    spinner.setVisibility(View.GONE);
                }

                adapter = null;

                switch (position) {
                    case 0:
                        adapter = pageOneAdapter;
                        toolbar.setTitle(getResources().getString(R.string.page1));
                        currentPage = 0;
                        // Set up of the TabLayout
                        tabs.removeAllTabs();
                        tabs.addTab(tabs.newTab().setText("A"));
                        tabs.addTab(tabs.newTab().setText("B"));
                        tabs.addTab(tabs.newTab().setText("C"));
                        tabs.addTab(tabs.newTab().setText("D"));
                        break;
                }
            }
        }
    });
Vatsal Dholakiya
  • 545
  • 4
  • 19