首页 / JAVA / java – 带有片段的选项卡布局
java – 带有片段的选项卡布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 带有片段的选项卡布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4674字,纯文字阅读大概需要7分钟。
内容图文
![java – 带有片段的选项卡布局](/upload/InfoBanner/zyjiaocheng/735/0320b059035545fc85cc56e242cd4b3f.jpg)
我正在尝试使用Android Tablayout和PagerAdapter在每个选项卡上加载不同的片段.标签工作正常,但相应的片段没有加载到我的scree上.这是代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false);
}
}
为每个片段XML创建了总共3个选项卡类.这是我的片段xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 1"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
我的Activity类:
import android.app.ActionBar;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import java.util.logging.Logger;
public class DashboardActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title"));
tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title"));
tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}
我的寻呼机适配器类是:
public class Pager extends FragmentStatePagerAdapter {
//integer to count number of tabs
int tabCount;
//Constructor to the class
public Pager(FragmentManager fm, int tabCount) {
super(fm);
//Initializing tab count
this.tabCount= tabCount;
}
//Overriding method getItem
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
default:
return null;
}
}
//Overriden method getCount to get the number of tabs
@Override
public int getCount() {
return tabCount;
}
}
活动仪表板布局文件:
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="368dp"
android:layout_height="wrap_content"
app:tabMode="fixed"
tools:layout_editor_absoluteX="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:onClick="loadOption"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp"
tools:ignore="MissingConstraints" />
Styles.xml文件:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
有什么建议.
解决方法:
添加这一行,你很高兴
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
内容总结
以上是互联网集市为您收集整理的java – 带有片段的选项卡布局全部内容,希望文章能够帮你解决java – 带有片段的选项卡布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。