Framelayout的CoordinatorLayout无效

时间:2017-09-24 07:00:55

标签: android android-coordinatorlayout android-framelayout

我在tabLayout中使用了viewPager三个标签,我想在滚动时使用CoordinatorLayout来隐藏和显示我的工具栏。我尝试的时候没用。

MainActivity布局:我在我的Framelayout中添加了app:layout_behavior="@string/appbar_scrolling_view_behavior",但它无效。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="tw.idv.morton.mydailysugar.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolBar"
            app:contentInsetStart="0dp"
            android:layout_width="match_parent"
            android:layout_height="35dp"></android.support.v7.widget.Toolbar>

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

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>

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

我的标签是一个布局:我尝试将ScrollView更改为android.support.v4.widget.NestedScrollView,但它仍无效。

<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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:paddingLeft="@dimen/appPadding"
    android:paddingRight="@dimen/appPadding"
    android:paddingTop="@dimen/appPadding"
    android:background="#ffffff"
    tools:context=".MyTabs.MyDailyInput">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    // here is lots of button and text and layout...    

    </ScrollView>


</LinearLayout>

这是我的标签布局设置:

<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".MyTabs.HomeTabs">

    <tw.idv.morton.mydailysugar.MyTabs.NoSwipingViewPager
        android:id="@+id/viewPagerDataReport"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <View
        android:background="@android:color/darker_gray"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:clickable="true"
        style="@style/AppTabLayout"
        app:tabTextAppearance="@style/AppTabTextAppearance"
        android:background="@color/appColor"
        app:tabTextColor="@android:color/white"
        app:tabGravity="fill"/>
</LinearLayout>

我添加了app:layout_behavior="@string/appbar_scrolling_view_behavior"android.support.v4.widget.NestedScrollView为什么它还没有用? 任何帮助,将不胜感激 。提前谢谢。

1 个答案:

答案 0 :(得分:1)

那是因为您还没有在工具栏中设置滚动标记。使用工具栏的以下代码

import Foundation

enum TileSize : Int, Codable
{
    case smallTile = 3
    case mediumTile = 2
    case largeTile = 1
}

class Tile: NSObject, Codable
{
    var tileSize: TileSize = .smallTile
    var imageName: String = "";
    var isDragged = false

    init(tileSize: TileSize, imageName: String) {
        self.tileSize = tileSize
        self.imageName = imageName
    }

    private enum CodingKeys : String, CodingKey
    {
        case tileSize
        case imageName
    }
}

class ConsumptionTile: Tile
{
    init(tileSize: TileSize) {
        super.init(tileSize: tileSize, imageName: "")
    }

    required init(from decoder: Decoder) throws {
        fatalError("init(from:) has not been implemented")
    }

    override var imageName: String
        {
        get
        {
            return "consumption"
        }
        set
        {
            super.imageName = newValue
        }
    }
}

var defaultTiles : [Tile] = []
defaultTiles.append(ConsumptionTile(tileSize: .largeTile))
defaultTiles.append(Tile(tileSize: .largeTile, imageName: "my-devices"))
defaultTiles.append(Tile(tileSize: .mediumTile, imageName: "live-verbrauch"))
defaultTiles.append(Tile(tileSize: .smallTile, imageName: "zaehlerstand"))
defaultTiles.append(Tile(tileSize: .smallTile, imageName: "vertrag"))

let jsonEncoder = JSONEncoder()
let jsonData = try! jsonEncoder.encode(defaultTiles)

let out = try! JSONDecoder().decode([Tile].self, from: jsonData)
for tile in out {
    print(">>> \(tile.imageName)")
}

此处进一步阅读有关滚动技术的更多信息 https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout