sencha touch 实现键盘对list的操作

在list的 show()事件中,先获取第一个的节点,使其选中,再添加按键事件
按键盘变换选中后,list也会随着选中而向下滚动
 
 
var first = list.getNode(0);
list.select(first)

document.addEventListener("keydown",selectListItem,false);

function selectListItem(){
	
	var comp = activeItem_id;
	var list = comp.down('list');
	
	var length = list.all.elements.length;                  //list长度
	var first = list.getNode(0);                            //获取第一个节点
	var last = list.getNode(length-1);                      //获取最后一个节点
	var selected = list.selModel.getSelection()[0]          //当前选中节点
	var i = selected.viewIndex                              //获取选中节点的序列号
	
	var offset = list.selModel.getSelection()[0].offsetTop  //当前选中距list顶部距离
	var listHeight = list.getHeight() -200
	var scrollposition = offset + 20 - listHeight
	list.scroller.updateBoundary();                         //必须先初始化  scroller的updateBoundary()事件,才能用键盘触发list的  scrollTo()事件   
	var e = window.event.keyCode;
	
	if(e == 56 || e == 54){
		if(i<length-1){
			i++;
			var next = list.getNode(i);
			list.select(next)   ;                             //设置某一节点为选中
			list.scroller.scrollTo({x: 0, y:scrollposition},true);
		}
		else{
			list.select(first);
			list.scrollTop();
		}
	}
	else if(e == 50 || e == 52){
		if(i != 0){
			i--;
			var previous = list.getNode(i);
			list.select(previous);
			list.scroller.scrollTo({x: 0, y:scrollposition+150},true);
		}
		else{
			list.select(last);
			var offset = list.selModel.getSelection()[0].offsetTop  //当前选中距list顶部距离
			if(offset > listHeight){
				list.scroller.scrollTo({x: 0, y:offset},true);
			}
		}
	}
	else if(e == 13 || e == 53){
		var node = list.selModel.getSelection()[0];
		var record = list.getRecord(node);                 //当前选中节点的record
		comp.showActionSheet(record);
	}
}


你可能感兴趣的:(sencha touch 实现键盘对list的操作)