【问题标题】:NestedScrollView is not workingNestedScrollView 不起作用
【发布时间】:2017-05-04 05:43:26
【问题描述】:

这是我的 .xml 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.Nestedsrollview 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:orientation="vertical"
    tools:context="com.example.rushabh123453.attendancepict.Attendance">




        <LinearLayout android:layout_weight="1" android:layout_height="0dp" android:layout_width="match_parent"
            android:orientation="vertical"
            android:background="@color/blue"
            android:paddingBottom="24dp">

            <TextView
                android:id="@+id/tv_attfinal"
              android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="78"

                android:gravity="center"
                android:textSize="120sp"
                android:textColor="@color/yellow"
                android:background="@color/blue"


            />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:text="Hello"
                android:textColor="@color/yellow"
                android:textSize="40sp"
                android:background="@color/blue"
                android:gravity="center"/>


        </LinearLayout>
        <LinearLayout android:layout_weight="1" android:layout_height="0dp" android:layout_width="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawer_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
            </LinearLayout>
</android.support.v4.Nestedsrollview >

我想使用 nestedscrollview 滚动整个布局,但它显示非法语句错误:host only one child。

我已经尝试将它的每个孩子都变成 wrap_content,但这也很有效

build.gradle(应用级别)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.rushabh123453.attendancepict"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'org.jsoup:jsoup:1.7.3'
}

【问题讨论】:

  • 一个 NestedScrollView 只能有一个孩子。您的 NestedScrollView 有两个孩子。
  • 那么有什么办法呢?
  • 更新你的布局,让 NestedScrollView 只有一个孩子而不是两个。
  • 但是我想用权重来分割屏幕所以我不能改变它,还有其他选择吗?
  • 你到底想要什么?意味着当顶层向上滚动时?

标签: android android-nestedscrollview


【解决方案1】:
try, Put NestedScrollView in Main Linear layout

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context="com.example.MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

              <TextView
            android:id="@+id/tv_attfinal"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="78"
            android:gravity="center"
            android:textSize="120sp"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:text="Hello"
            android:textSize="40sp"
            android:gravity="center"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/product_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:scrollbars="none" />

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

【讨论】:

  • 你有没有像这样在回收站视图中添加 setNestedScrollingEnabled(false) : - RecyclerView bestRecyclerView = (RecyclerView)findViewById(R.id.product_list); bestRecyclerView.setNestedScrollingEnabled(false);
【解决方案2】:

首先使用android.support.v4.widget.NestedScrollView

您的 xml 中的第二个更改:

<android.support.v4.widget.NestedScrollView 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"
>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:paddingBottom="24dp">
        <TextView
            android:id="@+id/tv_attfinal"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="78"
            android:gravity="center"
            android:textSize="120sp"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:text="Hello"
            android:textSize="40sp"
            android:gravity="center"/>
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/drawer_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</LinearLayout>

好的,现在试试这个..

更新了构建 Gradle

替换这个:-

   dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

     compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.1.1'
    testCompile 'junit:junit:4.12'

  }

【讨论】:

猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
相关资源
最近更新 更多