锁定gridview的滚动

时间:2011-07-13 13:09:35

标签: android gridview scroll

我的UI中有一个gridview但它垂直滚动我将所有滚动条属性设置为false但仍然滚动。知道为什么会这样吗?请帮忙。enter image description here

这是我的布局

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/bg_im" android:id="@+id/rLayout" android:isScrollContainer="false">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lLayoutCalendar" android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/linearLayoutLeftTask" android:gravity="center_horizontal"
        android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
        android:layout_marginTop="20dp" android:background="@drawable/calendar_bgnew" android:isScrollContainer="false">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_gravity="center_vertical"
            android:scrollbars="none" android:isScrollContainer="false"
            android:layout_marginTop="8dp">

            <ImageView android:id="@+id/prevMonth"
                android:layout_width="wrap_content" android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp" android:layout_height="wrap_content"
                android:src="@drawable/calendar_arrow_previous">
            </ImageView>
            <Button android:id="@+id/currentMonth" android:layout_weight="0.8"
                android:textColor="@android:color/black" android:textAppearance="?android:attr/textAppearanceMedium"
                android:background="@android:color/transparent" android:textStyle="bold"
                android:textSize="16sp" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </Button>
            <ImageView android:id="@+id/nextMonth"
                android:layout_marginRight="10dp" android:layout_marginTop="5dp"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:src="@drawable/calendar_arrow_next">
            </ImageView>
        </LinearLayout>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:paddingLeft="5dp"
            android:layout_marginTop="10dp" android:paddingRight="5dp">

            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:paddingLeft="5dp" android:text="Su" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="65px">
            </TextView>
            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:text="Mo" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="65px">
            </TextView>
            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:text="Tu" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="63px">
            </TextView>
            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:text="We" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="63px">
            </TextView>
            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:text="Th" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="63px">
            </TextView>
            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:text="Fr" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="63px">
            </TextView>
            <TextView android:typeface="sans" android:layout_marginTop="12dp"
                android:paddingLeft="5dp" android:text="Sa" android:layout_height="wrap_content"
                android:textStyle="normal" android:textColor="#7e7f7e"
                android:layout_width="63px">
            </TextView>
        </LinearLayout>
        <GridView android:numColumns="7" android:layout_marginTop="5dp"  style="overflow:lock"
            android:layout_marginBottom="10dp" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/calendar"
            android:horizontalSpacing="-1px" android:verticalSpacing="-1px"
            android:layout_marginLeft="2dp" android:isScrollContainer="false"
            android:smoothScrollbar="false" android:stackFromBottom="false"
             android:fastScrollEnabled="false" android:stretchMode="columnWidth" android:fadeScrollbars="false">
        </GridView>
    </LinearLayout>
</RelativeLayout>

3 个答案:

答案 0 :(得分:3)

使用此自定义gridview。它会自动设置高度而不会滚动:

public class MyGridView extends GridView {
    public MyGridView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSpec;

        if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
            // The great Android "hackatlon", the love, the magic.
            // The two leftmost bits in the height measure spec have
            // a special meaning, hence we can't use them to describe height.
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        } else {
            heightSpec = heightMeasureSpec;
        }

        super.onMeasure(widthMeasureSpec, heightSpec);
    }
}

答案 1 :(得分:1)

GridView旨在垂直滚动,我不知道你可以阻止它滚动。如果您不想滚动,请不要使用GridView

答案 2 :(得分:0)

public class myGridView extends GridView {

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {

    switch(event.getAction()){

    case MotionEvent.ACTION_DOWN:
    return super.onTouchEvent(event);

    case MotionEvent.ACTION_MOVE:
    //Do nothing here!!!
    break;

    case MotionEvent.ACTION_UP:
    return super.onTouchEvent(event);
    }

    //false return 
    return false;
    }
}