使用SlidingtabLayout单击选项卡时切换标签不起作用

时间:2016-04-18 08:38:55

标签: android android-viewpager android-tablayout

<include
    android:id="@+id/tool_bar"
    layout="@layout/toolbar"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/PrimaryColor"
    android:layout_below="@+id/tool_bar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tabs"
    />

滑动选项卡在选项卡滑动时工作正常,但在单击选项卡时不起作用。 Tab lick不会将您刷到相关的标签内容。 下面是MainActivity.java,我正在使用viewpager和SlidingTabLayout:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //Declaring All The Variables Needed
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;

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

       /*
        Assigning view variables to their respective view in xml
        by findViewByID method
         */

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        /*
        Creating Adapter and setting that adapter to the viewPager
        setSupportActionBar method takes the toolbar and sets it as
        the default action bar thus making the toolbar work like a normal
        action bar.
         */
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        setSupportActionBar(toolbar);

        /*
        TabLayout.newTab() method creates a tab view, Now a Tab view is not the view
        which is below the tabs, its the tab itself.
         */

        final TabLayout.Tab tasbih1 = tabLayout.newTab();
        final TabLayout.Tab tasbih2 = tabLayout.newTab();
        final TabLayout.Tab tasbih3 = tabLayout.newTab();
        final TabLayout.Tab tasbih4 = tabLayout.newTab();

        /*
        Setting Title text for our tabs respectively
         */

        tasbih1.setText("تسبيح");
        tasbih2.setText("إستغفار");
        tasbih3.setText("عدد الركعات");
        tasbih4.setText("إتجاه القبلة");

        /*
        Adding the tab view to our tablayout at appropriate positions
        As I want home at first position I am passing home and 0 as argument to
        the tablayout and like wise for other tabs as well
         */
        tabLayout.addTab(tasbih1, 0);
        tabLayout.addTab(tasbih2, 1);
        tabLayout.addTab(tasbih3, 2);
        tabLayout.addTab(tasbih4, 3);


        /*
        TabTextColor sets the color for the title of the tabs, passing a ColorStateList here makes
        tab change colors in different situations such as selected, active, inactive etc

        TabIndicatorColor sets the color for the indiactor below the tabs
         */

        tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.drawable.tab_selector));
        tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));

        /*
        Adding a onPageChangeListener to the viewPager
        1st we add the PageChangeListener and pass a TabLayoutPageChangeListener so that Tabs Selection
        changes when a viewpager page changes.
         */

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));




        /*
        SensorManager mSensorManager;

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
            //Toast.makeText(this, "Magnetic sensor exists", Toast.LENGTH_LONG).show();

        }
        else {
            Toast.makeText(this, "Magnetic sensor doesn't exist", Toast.LENGTH_LONG).show();
        }
        */
    }

    public void onTabSelected(TabLayout.Tab tab) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

    }

}

7 个答案:

答案 0 :(得分:7)

如果你的标签没有聚焦在前面,请尝试低于一。

findViewById(R.id.tabs).bringToFront();

另外,我想你忘了使用。

tabLayout.setupWithViewPager(viewPager);

答案 1 :(得分:2)

添加以下覆盖方法。

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

});

答案 2 :(得分:1)

ActivityFragment课程中,如果没有添加 -

TabLayout.setupWithViewPager(ViewPager);

在这种情况下,当您进行滑动时,标签不会更改,只会ViewPager。对于其他情况,您必须发布您的代码

答案 3 :(得分:1)

如果viewpager与tablayout不重叠,您可以签入xml。如果重叠,则选项卡选择不wrok

答案 4 :(得分:1)

我面对同样的问题,我解决了它  ViewPager上的android:layout_below="@+id/sliding_tabs"android:layout_height="wrap_content"上的SlidingTablayout

    <com.androidbegin.pagertabstriptutorial.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">        
    </com.androidbegin.pagertabstriptutorial.SlidingTabLayout>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_below="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>

答案 5 :(得分:0)

我之前遇到过这个问题。

即使ViewPager与您的标签不重叠(正如其他答案所示),某些元素也可能重叠。根据我的经验,很多时候,Toolbar是罪魁祸首。

首先,看看这个布局,并确保你有类似的东西。特别要注意TabLayout的属性:

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        app:tabMode="scrollable">

        <android.support.design.widget.TabItem
            android:id="@+id/tab_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_home" />
<!-- And so on, with more tabs... -->

如果您具有上述内容,请确保在相应活动的onCreate方法中包含相应的代码。这样的事情应该有效:

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

        final TabLayout tabLayout = findViewById(R.id.tab_layout);
        final ViewPager viewPager = findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(this, getSupportFragmentManager(), tabLayout.getTabCount());

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // Nothing needed here.
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // Nothing needed here.
            }
        });
    }

请注意,在该代码中,我使用自定义FragmentStatePagerAdapterPagerAdapter。您可以使用您想要的任何寻呼机适配器。

答案 6 :(得分:-1)

发生了同样的事情。我用

修复了它
android:layout_below="@+id/appBarLayout"