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

android– 使用SlidingtabLayout单击选项卡时切换标签不起作用,第1张

概述<includeandroid:id="@+idool_bar"layout="@layoutoolbar"/><android.support.design.widget.TabLayoutandroid:id="@+idabs"android:layout_width="match_parent"android:layout_height=

<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) {    }}

解决方法:

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

findVIEwByID(R.ID.tabs).bringToFront();

另外,我想你忘了用.

tabLayout.setupWithVIEwPager(vIEwPager);
总结

以上是内存溢出为你收集整理的android – 使用SlidingtabLayout单击选项卡时切换标签不起作用全部内容,希望文章能够帮你解决android – 使用SlidingtabLayout单击选项卡时切换标签不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存