예제 #1
0
  private void addNewItems() {

    currentChildCount = getChildCount();
    parentLayout = false;
    int start = itemIds.size();
    int end = Math.min(start + screenMaxColumns * rows * 2, adapter.getCount());

    for (int i = start; i < end; i++) {
      int left = (i / rows) * (itemWidth + spaceHori);
      int top = (i % rows) * (spaceVert + itemHeight);
      RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(itemWidth, itemHeight);
      rlp.setMargins(left, top, 0, 0);
      View child = adapter.getView(i, null, this);
      this.addView(child, rlp);
      int viewId = child.getId();
      if (viewId == -1) {
        viewId = TvUtil.buildId();
        // 此处硬设置id同时建议开发者不用此范围id
      }
      child.setId(viewId);
      itemIds.put(viewId, i);
      bindEventOnChild(child, i);
      layoutFlag = true;
    }

    colCount = itemIds.size() % rows == 0 ? itemIds.size() / rows : itemIds.size() / rows + 1;
  }
예제 #2
0
  private void initItems() {
    // 避免冲突
    if (getChildCount() > 0) {
      return;
    }

    int screenWidth = wm.getDefaultDisplay().getWidth();
    int initCols =
        screenWidth % rowWidth == 0 ? screenWidth / rowWidth : screenWidth / rowWidth + 1;

    int initLength = Math.min(adapter.getCount(), initCols * 2 * rows);
    for (int i = 0; i < initLength; i++) {
      int left = (i / rows) * (itemWidth + spaceHori);
      int top = (i % rows) * (spaceVert + itemHeight);
      RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(itemWidth, itemHeight);
      if (initLength == 1) {
        rlp.setMargins(left, top, 0, paddingTop * 2);
      } else {
        rlp.setMargins(left, top, 0, 0);
      }

      View child = adapter.getView(i, null, this);
      this.addView(child, rlp);
      int viewId = child.getId();
      if (viewId == -1) {
        viewId = TvUtil.buildId();
        // 此处硬设置id同时建议开发者不用此范围id
      }
      child.setId(viewId);
      itemIds.put(viewId, i);
      bindEventOnChild(child, i);
      layoutFlag = true;
    }
    colCount = itemIds.size() % rows == 0 ? itemIds.size() / rows : itemIds.size() / rows + 1;

    cursor = new ImageView(getContext());
    cursorId = TvUtil.buildId();
    cursor.setId(cursorId);
    cursor.setBackgroundResource(cursorRes);
    this.addView(cursor);
    cursor.setVisibility(View.INVISIBLE);

    if (initFocus) {
      View focus = ((ViewGroup) getParent()).findFocus();
      if (focus == null) {
        View item = getChildAt(0);
        if (item != null) {
          item.requestFocus();
        }
      }
    }
  }