TabLayout 选项卡无法在 Android 中单击
TabLayout tab can't click in Android
我无法在 TabLayout 中单击选项卡。我有两个选项卡(Genel 和 Ozel)。当我打开应用程序 genel open 时,我会在 genel 选项卡中滑动打开 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;
}
}
我该如何解决这个问题?
我需要你的帮助。
不是:希望你能理解。我的英语不好。有不懂的地方可以问
您忘记将 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>
我无法在 TabLayout 中单击选项卡。我有两个选项卡(Genel 和 Ozel)。当我打开应用程序 genel open 时,我会在 genel 选项卡中滑动打开 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;
}
}
我该如何解决这个问题?
我需要你的帮助。
不是:希望你能理解。我的英语不好。有不懂的地方可以问
您忘记将 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>