如何通过布局在android中的垂直scrollview内滚动到gridview?

时间:2016-09-17 07:51:26

标签: android android-layout

在垂直滚动视图中应用滚动到gridview时,只能进行父滚动,但子gridview不会滚动..

android xml中有没有关于这个问题的想法?

3 个答案:

答案 0 :(得分:1)

只需向gridview添加一个属性即可启用scrollview和gridview滚动。

机器人:nestedScrollingEnabled ="真"

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linearLayoutTimelineButtons">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:id="@+id/relativeHeader"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <GridView
                        android:id="@+id/gridMatchedUserGallery"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignBottom="@+id/imgProfileBackground"
                        android:layout_alignTop="@+id/imgProfileBackground"
                        android:nestedScrollingEnabled="true"
                        android:numColumns="3" />
                    </RelativeLayout>
              </RelativeLayout>
       </RelativeLayout>
</ScrollView>

答案 1 :(得分:0)

您可以将RecyclerView与GridLayoutManager一起使用,然后您可以在scrollview中滚动gridview(使用gridLayout的Recyclerview)。

答案 2 :(得分:0)

我认为它的工作原理 将gridview更改为packagename.ExpandableHeightGridView

3>Building with tools version "15.0".
3>Project file contains ToolsVersion="12.0". This toolset may be unknown or missing, in which case you may be able to resolve this by installing the appropriate version of MSBuild, or the build may have been forced to a particular ToolsVersion for policy reasons. Treating the project as if it had ToolsVersion="15.0". For more information, please see http://go.microsoft.com/fwlink/?LinkId=293424.
3>Target "_CheckForInvalidConfigurationAndPlatform" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets" from project "C:\devvm\git\SBD.JobTalk.MyobApi\MYOBAPITests\MYOBAPITests.csproj" (target "BeforeRebuild" depends on it):
3>Task "Error" skipped, due to false condition; ( '$(_InvalidConfigurationError)' == 'true' ) was evaluated as ( '' == 'true' ).
3>Task "Warning" skipped, due to false condition; ( '$(_InvalidConfigurationWarning)' == 'true' ) was evaluated as ( '' == 'true' ).
3>Using "Message" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
3>Task "Message"
3>  Configuration=Debug
3>Done executing task "Message".
3>Task "Message"
3>  Platform=AnyCPU
3>Done executing task "Message".
3>Task "Error" skipped, due to false condition; ('$(OutDir)' != '' and !HasTrailingSlash('$(OutDir)')) was evaluated as ('bin\Debug\' != '' and !HasTrailingSlash('bin\Debug\')).
3>Task "Error" skipped, due to false condition; ('$(BaseIntermediateOutputPath)' != '' and !HasTrailingSlash('$(BaseIntermediateOutputPath)')) was evaluated as ('obj\' != '' and !HasTrailingSlash('obj\')).
3>Task "Error" skipped, due to false condition; ('$(IntermediateOutputPath)' != '' and !HasTrailingSlash('$(IntermediateOutputPath)')) was evaluated as ('obj\Debug\' != '' and !HasTrailingSlash('obj\Debug\')).
3>Done building target "_CheckForInvalidConfigurationAndPlatform" in project "MYOBAPITests.csproj".

然后为ExpandableHeightGridView创建新类,这里是类

<com.magazine.screens.ExpandableHeightGridView
    android:id="@+id/gv_issues"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="6dp"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:numColumns="2"
    android:isScrollContainer="false"
    android:verticalSpacing="5dp">
</com.magazine.screens.ExpandableHeightGridView>

并且在您的课程中添加此代码非常重要

package com.magazine.screens;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;

/**
* Created by muhammed.haris on 31-03-2017.
*/

public class ExpandableHeightGridView extends GridView {
boolean expanded = false;

public ExpandableHeightGridView(Context context) {
    super(context);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs,
                                int defStyle) {
    super(context, attrs, defStyle);
}

public boolean isExpanded() {
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // HACK! TAKE THAT ANDROID!
    if (isExpanded()) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}
}

我希望它绝对有用