TabLayout标签无法在Android中单击

问题描述 投票:0回答:1

我无法单击TabLayout中的标签。我有两个标签(Genel和Ozel)。当我打开应用程序常规打开时,我会在常规选项卡中打开Ozel布局。我无法在“ Ozel”选项卡中单击。代码在下面。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
            android:id="@+id/fotogpsbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
    />

    <android.support.design.widget.TabLayout
        android:id="@+id/fotogpslayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"

    >

        <android.support.design.widget.TabItem
                android:id="@+id/fotogpsgenel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Genel"
        />

        <android.support.design.widget.TabItem
            android:id="@+id/fotogpsozel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Özel"
        />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/ogesayfa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >

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

</RelativeLayout>

Java代码(MainActivity):

Toolbar fototoolbar;
    TabLayout fototabl;
    TabItem genelit, ozelit;
    ViewPager fotogpspager;

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

        fototoolbar = findViewById(R.id.fotogpsbar);
        fototabl = findViewById(R.id.fotogpslayout);
        genelit = findViewById(R.id.fotogpsgenel);
        ozelit = findViewById(R.id.fotogpsozel);
        fotogpspager = findViewById(R.id.ogesayfa);

        sayfaadapter sayfaada = new sayfaadapter(getSupportFragmentManager(), fototabl.getTabCount());
        fotogpspager.setAdapter(sayfaada);

    }

Java代码(sayfaadapter-适配器):

public class sayfaadapter extends FragmentPagerAdapter {

    private int tabsayi;

    sayfaadapter(FragmentManager fm, int tabsayi){
        super(fm);
        this.tabsayi = tabsayi;
    }

    @Override
    public Fragment getItem(int i) {

        switch(i){
            case 0:
                return new genel();
            case 1:
                return new ozel();
            default:
                return null;
        }

    }

    @Override
    public int getCount() {
        return tabsayi;
    }
}

如何解决此问题?

我需要您的帮助。

不是:希望您能理解。我的英语不好。您可以问一下您不了解的地方。

android android-tablayout
1个回答
0
投票

您忘了在视图中添加layout_below,因此viewpager会在选项卡上重叠,因此您无法单击选项卡,替换下面的xml中的代码。

<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=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/fotogpsbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme" />

    <android.support.design.widget.TabLayout
        android:id="@+id/fotogpslayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fotogpsbar"
        app:tabGravity="fill"
        app:tabMode="fixed">

        <android.support.design.widget.TabItem
            android:id="@+id/fotogpsgenel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Genel" />

        <android.support.design.widget.TabItem
            android:id="@+id/fotogpsozel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Özel" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/ogesayfa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fotogpslayout" />
</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.