如何在中心选择选项卡

时间:2016-07-05 07:43:03

标签: android

我遇到了问题,我需要尽快解决问题。 我正在研究android应用程序。我有8个我想要的标签,当我点击其中一个然后选中标签必须进入中心。  这是我的代码

measureChildren()

这是我的Activity类

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bhitch.bhitch.com.bhitch.CategoryActivity">


    <include
        android:id="@+id/toolbar_layout"
        layout="@layout/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:fitsSystemWindows="true"
        android:id="@+id/image_viewPager"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/toolbar_layout">

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/firsttabstrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"/>

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@id/image_viewPager"
        android:layout_gravity="bottom"

        android:layout_marginTop="150dp"
        app:tabGravity="center"
       app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/tab_focus"
        app:tabTextColor="@android:color/white">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="700dp"
        android:layout_below="@id/tabLayout">

        <android.support.v4.view.PagerTabStrip
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/secondtabstrip"
            android:layout_gravity="top"/>

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



</RelativeLayout>

这是viewpageradapter类

public class CategoryActivity extends AppCompatActivity {
    ViewPager viewPager;
    CategoryAdapter adapter;
    GridView gridView;
    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager imageViewPager;
    CategoryAdapter imageAdapter;
    ViewPagerAdapter viewPagerAdapter;
    PagerTabStrip firsttabstrip;
    PagerTabStrip secondtabstrip;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        toolbar = (Toolbar) findViewById(R.id.toolbar_layout);
        setSupportActionBar(toolbar);

       viewPager = (ViewPager) findViewById(R.id.viewPager);
        imageViewPager = (ViewPager) findViewById(R.id.image_viewPager);
        firsttabstrip=(PagerTabStrip)findViewById(R.id.firsttabstrip);
        secondtabstrip=(PagerTabStrip)findViewById(R.id.secondtabstrip);
     imageAdapter = new CategoryAdapter(this);
        imageViewPager.setAdapter(imageAdapter);


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        toolbar.setTitle("Category Activity");


        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragments(new HealthFragment(), "Health");
        viewPagerAdapter.addFragments(new BeautyFragment(), "Beauty");
        viewPagerAdapter.addFragments(new AutomobileFragment(), "Automobile");
        viewPagerAdapter.addFragments(new CinemaFragment(), "Cinema");
        viewPagerAdapter.addFragments(new FoodFragment(), "Food");
        viewPagerAdapter.addFragments(new BeautyFragment(), "Music");
        viewPagerAdapter.addFragments(new ElectronicFragment(), "Electronics");
        viewPagerAdapter.addFragments(new EducationFragment(), "Education");



        viewPager.setAdapter(viewPagerAdapter);



        tabLayout.setupWithViewPager(viewPager);

        // tabLayout.setupWithViewPager(imageViewPager);




              viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }


                  @Override
            public void onPageSelected(int position) {
                imageViewPager.setCurrentItem(position);
                tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);

            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });


        imageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}

当我点击它时,第1,第2,第7,第8个标签不会进入中心,其余标签位于中心。我想制作一个圆形的标签链,这样当我点击它们中的任何一个时,选中的标签必须位于中心。提前谢谢。

0 个答案:

没有答案