PagerSlidingTabStrip指示灯不随页面变化而改变?

3
这段代码在我的搜索活动页面上运行正常,但在我的个人资料活动页面上不起作用:选定标签下的指示灯,我需要添加什么吗?
以下是代码:
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);
                }
            }
        });

他是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>
1个回答

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


}

// 同时创建ProfileDataFragment并根据您的位置设置数据


我不确定我理解了,在两个地方都附加了相同的监听器。 - Lion789
不需要覆盖监听器,将它们注释掉并尝试你的代码。 - Yousef Zakher
如果您正在使用此库https://github.com/astuetz/PagerSlidingTabStrip,那么就无需自己处理监听器,库本身会指示您滑动或选择新选项卡并设置指示器,如果您覆盖了监听器,则停止了库的监听器,这就是为什么我要求您在活动中注释监听器代码以使用库默认设置,您是否已经这样做了?请在屏幕启动时添加选项卡的截图,或告诉我指示器是否出现在第一个选项卡中,并且在滑动时是否移动? - Yousef Zakher
问题在于,如果我没有看到onPageselectedListener,那么我就无法根据所选页面显示不同的网格。 - Lion789
因此,您必须根据PagerAdapter中每个选项卡的位置设置片段(在您的代码中命名为TabAdapter),您可以通过仅制作一个片段并将位置作为参数发送以设置所需视图来完成此操作。我会更新我的代码以帮助您理解我。 - Yousef Zakher
让我们在聊天中继续这个讨论 - Yousef Zakher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接