PagerSlidingTabStrip indicator not changing from page?

It works on my search page, but not for my profile activity: the indicator lights up under the selected tab, if I need to add something HEre is the code:

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

 String[] tabTitles =  {"Completed", "Joined"};
        adapter = new TabAdapter(getSupportFragmentManager(), profileCompletedFrag, profileJoinedFrag, tabTitles);

//        // Initialize the ViewPager and set an adapter
        pager = (ViewPager) findViewById(R.id.profilePager);
        pager.setAdapter(adapter);

        // Bind the tabs to the ViewPager
        tabs = (PagerSlidingTabStrip) findViewById(R.id.profileTabs);
        tabs.setViewPager(pager);

 /**
         * on swiping the viewpager make respective tab selected
         * */
        tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                if (position == 0) {
                    currentPage = 0;
                    pager.setCurrentItem(0);


                }
                if (position == 1) {
                    currentPage = 1;
                    pager.setCurrentItem(1);

                }

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        /////////////
        tabs.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() {
            @Override
            public void onTabReselected(int position) {
                Toast.makeText(ProfileActivity.this, "Tab reselected: " + position, Toast.LENGTH_SHORT).show();
                if (position == 0) {
                    currentPage = 0;
                     pager.setCurrentItem(0);
                }
                if (position == 1) {
                    currentPage = 1;
                    pager.setCurrentItem(1);
                }
            }
        });

      

It is xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/profileParentLayout">
   <LinearLayout
       android:orientation="horizontal"
       android:gravity="center"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/profileNameLayout"
       android:layout_alignParentTop="true"
       android:layout_marginTop="20dp"
       android:layout_marginBottom="10dp">

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/profileName"
           android:layout_centerInParent="true"
           android:text="Bob Marley"/>
   </LinearLayout>


    <RelativeLayout
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_marginBottom="10dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/profileImageLayout"
        android:layout_below="@+id/profileNameLayout">

        <ImageView
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:id="@+id/profileImage"
            android:src="@drawable/camera_w"
            android:layout_centerInParent="true"/>
        <ImageView android:id="@+id/editProfileImage"
            android:layout_height="30dp"
            android:layout_width="30dp"
            android:src="@drawable/ic_launcher"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_marginBottom="10dp"
    android:id="@+id/profileDetailsLayout"
    android:layout_below="@id/profileImageLayout">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profileFollowers"
        android:text="13 followers"
        android:textSize="20dp"/>

    <View android:id="@+id/separator"
        android:layout_marginLeft="5dip"
        android:background="#ffffff"
        android:layout_width = "1dip"
        android:layout_height="fill_parent"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profileFollowing"
        android:text="45 following"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="20dp"
        />

    <View android:id="@+id/separator2"
        android:layout_marginRight="5dip"
        android:background="#ffffff"
        android:layout_width = "1dip"
        android:layout_height="fill_parent"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profilePoints"
        android:text="124 points"
        android:textSize="20dp"/>
</LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="?attr/actionBarSize"
        android:orientation="horizontal"
        android:id="@+id/tabsLayout"
        android:layout_below="@+id/profileDetailsLayout">
        <!-- for Tabs -->
        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/profileTabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:pstsShouldExpand="true"
            android:background="?attr/colorPrimary"/>

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/profilePager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </android.support.v4.view.ViewPager>
    </LinearLayout>



    <GridView
        android:layout_below="@+id/tabsLayout"
        android:id="@+id/profileGridView"
        android:layout_marginTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth" >

    </GridView>

</RelativeLayout>

      

+3


source to share


1 answer


 public class TabAdapter  extends FragmentStatePagerAdapter {

    String[] tabTitles =  {"Completed", "Joined"};

    public TabAdapter(FragmentManager fragmentManager) {
        // TODO Auto-generated constructor stub
        super(fragmentManager);
    }


    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return tabTitles[position];
    }


    @Override
    public Fragment getItem(int arg0) {

        switch (arg0) {
        case 0:
            return ProfileDataFagment.newInstance(0);
        case 1: 
            return ProfileDataFagment.newInstance(1);
        default:
            break;
        }

        return null;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2; // Number of views  
    }


}

      



// Also create a ProfileDataFagment and adjust the data according to your position

0


source







All Articles