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

You*_*uri 3 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"
    />
Run Code Online (Sandbox Code Playgroud)

滑动选项卡在选项卡滑动时工作正常,但它不适用于选项卡单击.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) {

    }

}
Run Code Online (Sandbox Code Playgroud)

Raj*_*sit 7

如果您的标签没有聚焦在前面,请尝试下面的一个.

findViewById(R.id.tabs).bringToFront();
Run Code Online (Sandbox Code Playgroud)

另外,我想你忘了用.

tabLayout.setupWithViewPager(viewPager);
Run Code Online (Sandbox Code Playgroud)