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

 一、 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);

你不勇敢,没人替你坚强。

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

相关文章:

你感兴趣的文章:

标签云: