我无法在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;
}
}
如何解决此问题?
我需要你的帮助。
否:希望您能理解。我的英语不好。您可以问自己不了解的地方。
答案 0 :(得分: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>