【问题标题】:Android Tabbed Activity ScrollingAndroid 选项卡式活动滚动
【发布时间】:2016-07-13 10:39:28
【问题描述】:

我想创建一个 Android 应用。对于我的主屏幕,我使用选项卡式活动。看起来是我想要的。但是当我用 ScrollView 包围我的 TextView 时,它只会滚动文本,但它应该看起来像这样:

所以它应该在我滚动之前查看:

滚动后是这样的:

这是选项卡式活动:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

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

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

这里是文本片段:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.obware.story.Activitys.MainActivity$PlaceholderFragment">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

有人知道我该怎么做吗?

【问题讨论】:

标签: java android xml android-activity scroll


【解决方案1】:

由于这是一个 Tablayout,我假设您使用的是 Fragments 和 ViewPager。 (即使没有查看器,这仍然适用)

在这种情况下,您必须向您的活动发送一个答案,这是隐藏工具栏的基础。在您的基础活动中声明一个具有选项卡布局的界面。

说:

interface HideToolBarInterface{

  public void hideTheToolbar(boolean shouldbehidden);
}

现在在你的 Activity 中实现这个接口并覆盖声明的抽象方法。

例如:

public void hideTheToolbar(boolean shouldbehidden){
 if(shouldbehidden){
  getSupportActionBar().hide(); // if you have set your own toolbar, hide it similarly
 }else{
   getSupportActionBar().show(); 
 }
}

现在在您的片段中,为您的 ListView 或 RecyclerView 设置一个 onScrollListener 来显示列表。

现在当列表滚动时,您可以使用监听器 onScrollChanged,应用您的逻辑调用接口方法来隐藏工具栏,如:

//on some condition(say SCROLL_STATE_FLING) do the following:
((HideToolbarInterface)getActivity()).hideTheToolbar(true);

//on some other conditon(say SCROLL_STATE_IDLE unhide it:
((HideToolbarInterface)getActivity()).hideTheToolbar(false);

您可以在此处查看其他选项:

android lollipop toolbar: how to hide/show the toolbar while scrolling?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多