Android gridview的锁定滚动条
我的UI中有一个gridview,但它垂直滚动。我已将所有滚动条属性设置为false,但它仍在滚动。知道为什么会这样吗?请帮忙 这是我的布局Android gridview的锁定滚动条,android,gridview,scroll,Android,Gridview,Scroll,我的UI中有一个gridview,但它垂直滚动。我已将所有滚动条属性设置为false,但它仍在滚动。知道为什么会这样吗?请帮忙 这是我的布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layou
<?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>
GridView
设计为垂直滚动,我不知道您可以阻止它滚动。如果不想滚动,请不要使用GridView
尝试为GridView添加或覆盖setOnTouchListener
,然后在onTouch方法中,您可以使用如下代码使GridView不滚动:
gridview.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
return true;
}
return false;
}
});
使用此自定义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);
}
}
你好你能发布你的布局吗?奥特拉,请看我已经完成了。@PoojaBohora,我也面临同样的问题。你有什么解决办法吗?@Parag:正如我所研究的,我们不能锁定gridview的垂直滚动。@PoojaBohora下面的代码解决了这个问题。试试这个。这个解决方案解决了上面的问题,但创建了一个新的问题:你不能再通过滑动手指来取消按钮按下。这意味着,如果你触摸左下角网格中的一个按钮,然后将其滑动到右上角,那么当你抬起时,左下角的按钮将调用onClick,就像你从未移动过手指一样。当你拖动时,按钮也将保持在按下状态。但是我们应该使用什么呢?比如说,我想在我的屏幕上的某个地方安装8*8平方字段的网格,每个字段只包含一个字母。。我应该使用什么?@Ewoks:要么使用
表格布局
、嵌套的线性布局
,要么创建自己的视图组
,实现您喜欢的任何业务规则。使用ConstraintLayout
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);
}
}