cocos2d_x+lua【4.1】 ——ListView的使用

来源:互联网 发布:命名空间的含义php 编辑:程序博客网 时间:2024/05/22 09:45

一、创建列表层

  1. function create_listview()
  2. local layer = CCLayerColor:layerWithColorWidthHeight(ccc4(255, 0, 0, CORLORLAYERENABLE),640, 200)
  3. layer:setAnchorPoint(CCPointMake(0,0))
  4. layer:setIsRelativeAnchorPoint(true)
  5. --anchorPoint这个属性虽然是属于CCNode的、但CCLayer设置anchorPoint没有效果、
  6. --CCLayeranchorPoint被默认设定在(0, 0)、在setAnchorPoint之前要先设置setIsRelativeAnchorPoint = true
  7. layer:setPosition(PT(165,120))
  8. -- 两种排列 CCListViewModeHorizontal 水平 CCListViewModeVertical 竖直
  9. m_pList = CCListView:viewWithMode(CCListViewModeHorizontal)
  10. -- 初始化控件
  11. ListView m_pList:setContentSize(CCSize(640,200))
  12. m_pList:setDelegateName('testListView')
  13. m_pList:setAnchorPoint(CCPointMake(0,0))
  14. m_pList:setPosition(PT(0,0))  --两种分隔 CCListViewCellSeparatorStyleNone CCListViewCellSeparatorStyleSingleLine m_pList:setSeparatorStyle(CCListViewCellSeparatorStyleNone) layer:addChild(m_pList) return layerend

  1. <span style="font-size: 2em; font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px;">二、实现的方法</span>

–返回有多少页

  1. function testListView_CCListView_numberOfCells(listview,data)
  2.     data.nNumberOfRows= listview:size()
  3. end

–listview在滑动中

  1. function testListView_triggerDidScrollToRow(nRow)
  2.     local i = nRow
  3.     cclog("scrolling:"..i)
  4. end

–cell被选中

  1. function testListView_triggerDidClickCellAtRow(nRow)  
  2.  local i = nRow
  3. cclog("clicked:"..i)
  4. end

–将listview层的触摸打开

  1. function setTouch_openLayer(layer)
  2. layer:registerScriptTouchHandler(onTouch_openLayer,nil,-129,nil)
  3. layer:setIsTouchEnabled(true)
  4. end

–具体显示什么内容,每一页的内容

  1. function testListView_triggerCellForRow(listview,data)
  2.     m_nCurrnetPage = data.nRow + 1   --data.nRow0开始
  3.     local cell = CCListViewCell:node()
  4.     cell:setOpacity(0)
  5.     cell:setContentSize(m_pList:getContentSize())
  6.     cell:setSelectionColor(ccc4(0, 0, 0, 0))
  7.     data.cell = cell
  8.     _curCell = cell
  9.     local listItemSize = CCSize:new(m_pList:getContentSize().width , m_pList:getContentSize().height)
  10.     for n = 1, data.nNumberOfRows do
  11.     cell:addChild(xxxxxx)
  12.     end
  13. end

三、listview的使用

  1. listview_layer = create_listview()
原创粉丝点击