像下面这样子类化 GridView 并在您的布局文件中使用该自定义 GridView ,并添加一个用于禁用滚动 ( disableScrolling
) 的字段并覆盖setAdapter
以检查超过 6 个项目并dispatchTouchEvent
处理滚动能力。
public class MyGridView extends GridView {
boolean disableScrolling = false;
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
public void setAdapter(ListAdapter adapter) {
if (adapter!=null && adapter.getCount() < 6){
disableScrolling = true;
} else {
//adapter is null or count is greater than 6
disableScrolling = false;
}
super.setAdapter(adapter);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if( disableScrolling || ev.getAction()!=MotionEvent.ACTION_MOVE){
return true;
}
return super.dispatchTouchEvent(ev);
}
}