如何在android中的滚动视图中设置网格视图和查看寻呼机?

时间:2016-03-17 06:47:39

标签: android android-layout android-viewpager android-gridview android-scrollview

在我的XML中,我将整个屏幕设置为可滚动,我在里面创建了滚动视图,为View Viewr和Grid视图设置了Framelayout。此处网格视图仅滚动但不会滚动整个屏幕。

以下是我的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">

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

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:id="@+id/framelayoutViewpager">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

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

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnImagePrevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Previous" />

            <Button
                android:id="@+id/btnImageNext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Next" />

        </LinearLayout>

    </FrameLayout>

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/framelayoutViewpager"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:paddingTop="7dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginBottom="2dp"
        android:id="@+id/gridView" />

    </RelativeLayout>

    </ScrollView>

</LinearLayout> 

2 个答案:

答案 0 :(得分:1)

NonScrollGridView会在这里做到这一点..

<强> NonScrollGridView

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

public class NonScrollGridView extends GridViewWithHeaderAndFooter {

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

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

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

   @Override
   public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
       super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
       ViewGroup.LayoutParams params = getLayoutParams();
       params.height = getMeasuredHeight();
   }
}

答案 1 :(得分:0)

如果准备使用AppCompact支持lib,请尝试NestedScrollView和CoordinatorLayout。

示例布局代码:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


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

                android:fitsSystemWindows="true"

                app:layout_collapseMode="parallax" />
            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/btnImagePrevious"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Previous" />

                <Button
                    android:id="@+id/btnImageNext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Next" />

            </LinearLayout>


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

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/framelayoutViewpager"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:paddingTop="7dp"
            android:layout_marginTop="2dp"
            android:layout_marginRight="2dp"
            android:layout_marginBottom="2dp"
            android:id="@+id/gridView" />
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>