向下滚动时,Android操作栏+标签会消失吗?

时间:2017-03-27 19:19:24

标签: android android-studio android-actionbar

某些应用程序具有操作栏+选项卡,当您向下滚动时,选项卡会缩小,直到只有操作栏可见。

我有自己的代码:

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    style="@style/CategoryTab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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

我可以做些什么来实现这种行为?

1 个答案:

答案 0 :(得分:0)

我试过了,它起作用了...

在适配器类中,创建一个Context对象,并将其添加到适配器类的构造函数中。

private Context context;

public MyAdapter(FragmentManager fm,Context context) {
    super(fm);
    this.context = context;
}

然后在Adapter类getPageTitle中创建一个函数,并使用上下文返回String。

public CharSequence getPageTitle(int position) {

    switch (position) {

        case 0 :
            return context.getString(R.string.chat);
        case 1:
            return context.getString(R.string.status);
        case 2:
            return context.getString(R.string.call);

    }

    return super.getPageTitle(position);
}

现在,转到MainActivity。设置适配器以查看寻呼机时,使用此功能,

viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),this));

最后一步是使用功能setupWithViewPager。

tabLayout.setupWithViewPager(viewPager);

希望它对您有用。 :)

进一步查询,您还可以在Facebook和Instagram上与我联系

www.facebook.com/himanshu.dubey.967806

www.instagram.com/_xplosion __ /?hl = zh_CN