android GridView禁止上下滑动以及禁止滚动条显示的方法。

来源:互联网 发布:基尼系数数据 编辑:程序博客网 时间:2024/04/28 18:56
 一、 android处理鼠标滚轮事件,并不是如下的函数:
1) public boolean onKeyDown(int keyCode, KeyEvent event)
2) public boolean dispatchKeyEvent(KeyEvent event) 
3) public boolean onTouchEvent(MotionEvent event)
 而是如下的函数
publicboolean onGenericMotionEvent(MotionEvent event);
所有View和Activity都可重写该函数,来自己处理滚轮事件。
网上很多人说用重写dispatchTouchEvent和onTouchEvent方法可以实现禁止滚动的功能,我测试了下,都是不行的,只有重写onGenericMotionEvent方法可以。
具体代码如下:
import android.annotation.SuppressLint;import android.content.Context;import android.util.Log;import android.view.MotionEvent;import android.widget.GridView;public class MyGridView extends GridView {private static final String TAG = "FreeRDP.SessionActivity";public MyGridView(Context context) {super(context);// TODO Auto-generated constructor stub}// 通过重新dispatchTouchEvent方法来禁止滑动@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubLog.v(TAG, "dispatchTouchEvent....4444444444........." + ev.getAction());if (ev.getAction() == MotionEvent.ACTION_MOVE) {return true;// 禁止Gridview进行滑动}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// 重写的onTouchEvent回调方法Log.v(TAG, "dispatchTouchEvent...5555........" + event.getAction());switch (event.getAction()) {// 按下case MotionEvent.ACTION_DOWN:return super.onTouchEvent(event);// 滑动case MotionEvent.ACTION_MOVE:break;// 离开case MotionEvent.ACTION_UP:return super.onTouchEvent(event);}// 注意:返回值是falsereturn false;}@SuppressLint({ "NewApi", "NewApi" })@Overridepublic boolean onGenericMotionEvent(MotionEvent event) {// 重写的onTouchEvent回调方法switch (event.getAction()) {// 按下case MotionEvent.ACTION_DOWN:return super.onGenericMotionEvent(event);// 滑动case MotionEvent.ACTION_MOVE:break;// 离开case MotionEvent.ACTION_UP:return super.onGenericMotionEvent(event);}// 注意:返回值是falsereturn false;}}
二、 禁止滚动条显示
     有二种方法禁止滚动条显示:
     1、xml添加android:scrollbars="none"属性。
     2、代码实现:gridView.setVerticalScrollBarEnabled(false);


 

2 0
原创粉丝点击