TabLayout的标签没有显示

时间:2015-07-21 16:26:34

标签: android android-fragments android-viewpager android-tablayout

我有一个主要活动,它承载一个片段,该片段反过来托管一个TabLayout(带有一个ViewPager)。显示标签栏,但标签本身未显示。

以下是我在显示主机片段的主要活动中的代码:

MainWindow

这是我托管TabLayout的片段,它是BMITabsFragment(s.a。):

    function order_total()
    {
    var sub_total = new Number();
    sub_total = Xrm.Page.getAttribute("new_sub_total").getValue();

    var freight_amount  = new Number();
    freight_amount = Xrm.Page.getAttribute("freightamount").getValue();

    var other_tax_amount = new Number();
    other_tax_amount = Xrm.Page.getAttribute("new_other_tax_amount").getValue();

    var freight_other_tax = new Number();
    freight_other_tax = Xrm.Page.getAttribute("new_freight_other_tax").getValue();

    var freight_value_added_tax = new Number();
    freight_value_added_tax = Xrm.Page.getAttribute("new_freight_value_added_tax").getValue();

    var database_total = new Number();
    database_total = Xrm.Page.getAttribute("new_database_total").getValue();

    var server_total = new Number();
    server_total = Xrm.Page.getAttribute("new_server_total").getValue();

    var order_other_tax = new Number();
    order_other_tax = Xrm.Page.getAttribute("new_order_other_tax").getValue();

    var order_value_added_tax = new Number();
    order_value_added_tax = Xrm.Page.getAttribute("new_order_value_added_tax").getValue();

    var adjustments = new Number();
    adjustments = Xrm.Page.getAttribute("new_adjustments").getValue();


    if (adjustments != null || order_value_added_tax != null || order_other_tax != null ||
        server_total != null || database_total != null || freight_value_added_tax != null ||
        freight_other_tax != null || other_tax_amount != null || freight_amount != null){
        Xrm.Page.getAttribute("new_total").setValue(sub_total + freight_amount + 
            other_tax_amount + freight_other_tax + freight_value_added_tax + database_total + server_total + 
            order_other_tax + order_value_added_tax + adjustments);

    }

else{
    Xrm.Page.getAttribute('new_total').setValue(sub_total);
}

}

这是我的FragmentPagerAdapter:

        Fragment fragment = new BMITabsFragment();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(Constants.BMI_TABS_FRAGMENT).commit();

这就是fragment_bmitabs.xml:

public class BMITabsFragment extends Fragment {
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
            getActivity()));

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_bmitabs, container, false);
    return view;
}
...
}

public class BMIFragmentPagerAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private FragmentManager fragmentManager;
private Context context;

public BMIFragmentPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
    this.fragmentManager = fm;

}

public BMIFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
    fragmentManager = fm;

}

@Override
public CharSequence getPageTitle(int position) {
    String[] pageTitles = context.getResources().getStringArray(R.array.page_titles_array);
    return pageTitles[position];
}

@Override
public Fragment getItem(int position) {
    SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFS_FILE, 0);
    long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1);
    Fragment fragment = null;
    switch (position){
        case 0:
            return BMITabelleFragment.newInstance(patientId);

        case 1:
            return BMIChartFragment.newInstance(patientId);

        default:
            return BMITabelleFragment.newInstance(patientId);
    }
}

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

我的代码基于https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout

上的Google Android指南

我在这里缺少什么?

注意:我正在使用AppCompatActivity和支持库v4& v7和com:android:support:设计库

7 个答案:

答案 0 :(得分:51)

我有同样的问题! 但这也解决了我。

tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

https://code.google.com/p/android/issues/detail?id=180462

答案 1 :(得分:7)

setupWithViewPager()

之后设置标签图标
private void setTabs {
   tabLayout.setupWithViewPager(viewPager);
   setupTabIcons();
} 

private void setupTabIcons() {
   tabLayout.getTabAt(0).setIcon(tabIcons[0]);
   tabLayout.getTabAt(1).setIcon(tabIcons[1]);
   tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

答案 2 :(得分:6)

像@Nathaniel Ford说的,这应该是一个错误,我改为使用设计库23.0.1.google修复它,所以将build.gradle更改为compile 'com.android.support:design:23.0.1'。 ps:您还必须将compileSdkVersion更改为23

答案 3 :(得分:1)

如果您在xml文件的Tablayout中使用android:tabPadding属性,请将其删除。

答案 4 :(得分:0)

没有其他答案对我有用,我尝试了所有答案

但是,这个人确实做到了:TabLayout not showing tabs after adding navigation bar

根据该答案,您必须将TabLayout括在AppBarLayout中。为什么?谁知道。但至少它能起作用。

示例代码(摘自该帖子):

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    android:id="@+id/appBarLayout2">


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

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

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

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:id="@+id/viewPager2"
    android:layout_below="@+id/appBarLayout2">

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

答案 5 :(得分:0)

您可以使用 FragmentStatePagerAdapter 代替 FragmentPagerAdapter 。 可能会对您有帮助。

答案 6 :(得分:0)

如果将来有人使用 TabLayout 和 ViewPager2 并面临同样的问题。只需在 .attach(); 末尾添加 new TabLayoutMediator()

new TabLayoutMediator(tabLayoutRef, viewPager2Ref, (tab, position) -> tab.setText("Tab No. "+position)).attach();