元素缺少网格

时间:2018-04-05 14:41:12

标签: android android-layout

我有一个Android项目。它有视频播放列表。有多行播放列表在网格行中具有多个视频。当我将MAX UI CHILDS = 5时,带有5个视频的播放列表会删除最后一个视频(第5个视频)。当我将MAX UI CHILDS = 6时,第6个视频的播放列表正在被削减。通过viewflipper可以完美地显示视频数量较高或较低的播放列表。

这是代码

公共类Horizo​​ntalListView {

private int maxChild = 0;
//private int max_ui_width = 0;
private int dataLength = 0;
private int parentId = -1;
private int curUIIdx = 0;
private int stDataIdx = 0;
private boolean hasFocus = false;
private int MAX_UI_CHILDS = 5;
private int ORIG_MAX_UI_CHILDS = 5;

private Context actCtx = null;
private LinearLayout scrollViewObj = null;
private HorizontalScrollView listViewObj = null;
private View[] childViews = null;
private HorizontalListViewEventListener adapter = null;

private CustomGridViewEventListener eventCallback = null;

public HorizontalListView HorizontalListView() {
    return this;
}

public void setScrollView(int parentInd, HorizontalListViewEventListener adapterObj,int maxviews, HorizontalScrollView scViewObj, Context actCtxt) {
    Utilities.logDebug("CustomListView_Horizontal: setScrollView called");

    listViewObj= scViewObj;
    dataLength = 0;
    reset();
    actCtx = actCtxt;
    scrollViewObj = null;
    scrollViewObj = (LinearLayout)listViewObj.getChildAt(0);
    parentId = parentInd;

    adapter = null;
    maxChild = maxviews;
    adapter = adapterObj;
}

public void reset() {
    Utilities.logDebug("CustomListView_Horizontal: reset called");
    if(scrollViewObj != null)
        scrollViewObj.removeAllViews();
    adapter = null;
    childViews = null;
    dataLength = 0;
    curUIIdx = 0;
    stDataIdx = 0;
}

public void update(int size) {
    dataLength = size;

    if(dataLength < ORIG_MAX_UI_CHILDS) {
        MAX_UI_CHILDS = dataLength;
        maxChild = MAX_UI_CHILDS;

    } else {
        MAX_UI_CHILDS = ORIG_MAX_UI_CHILDS;
        maxChild = MAX_UI_CHILDS;
    }

    childViews = null;
    childViews = new View[maxChild];

    for(int i=0; i<maxChild; i++) {
        childViews[i] = null;
    }

}

public void setFocus() {
    Utilities.logDebug("CustomListView_Horizontal: setFocus called with " + stDataIdx);
    hasFocus = true;
    updateListView();
    updateListFocus(-1, curUIIdx);
}


public void removeFocus() {
    Utilities.logDebug("CustomListView_Horizontal: removeFocus called with " + stDataIdx);
    hasFocus = false;
    updateListView();
    updateListFocus(curUIIdx, -1);
}


public void setFocusAt(int newIdx) {
    Utilities.logDebug("CustomListView_Horizontal: setFocusAt called with " + newIdx);
    if(newIdx <= (dataLength - maxChild)) {
        //newidx is less then last set of data
        stDataIdx = newIdx - curUIIdx;

    } else if(newIdx > (dataLength - maxChild)) {
        //newIdx is in last set of data so update ui only
        stDataIdx = dataLength - maxChild;
        curUIIdx = newIdx -  stDataIdx;
    }
    hasFocus = true;
    updateListView();
    updateListFocus(-1, curUIIdx);
}

public boolean navigateRight() {
    int prevUIIdx = curUIIdx;

    if (curUIIdx < (Math.round(maxChild / 2) - 1))
        curUIIdx++;

    else if ((stDataIdx >= (dataLength - maxChild)) && ((curUIIdx + stDataIdx) < (dataLength - 1)))
        curUIIdx++;

    else if (stDataIdx < (dataLength - maxChild))
        stDataIdx++;

    if (curUIIdx < (dataLength)) {
        updateListView();

        if (hasFocus == true)
            updateListFocus(prevUIIdx, curUIIdx);
    }

    return true;
}


public boolean navigateLeft() {
    int prevUIIdx = curUIIdx;

    if(curUIIdx > Math.round(maxChild / 2))
        curUIIdx --;

    else if((stDataIdx == 0) && (curUIIdx > 0))
        curUIIdx --;

    else if(stDataIdx > 0)
        stDataIdx --;

    else if((stDataIdx == 0) && (curUIIdx == 0))
        return false;

    if (curUIIdx < (dataLength - 1)) {
        updateListView();

        if (hasFocus == true)
            updateListFocus(prevUIIdx, curUIIdx);
    }
    return true;
}


public void onItemClicked() {
    if(eventCallback != null) {
        eventCallback.onCellClicked((RelativeLayout) scrollViewObj.getChildAt(curUIIdx), parentId, stDataIdx + curUIIdx );

    }
}


public void setAdapter( int size) {
    Utilities.logDebug("CustomListView_Horizontal: setAdapter called");

    dataLength = size;
    if(dataLength < MAX_UI_CHILDS) {
        MAX_UI_CHILDS = dataLength;
        maxChild = MAX_UI_CHILDS;
    }

    childViews = new View[maxChild];

    for(int i=0; i<maxChild; i++) {
        childViews[i] = null;
    }

    curUIIdx = 0;
    stDataIdx = 0;
    updateListView();
}


public void updateListView() {
    Utilities.logDebug("CustomListView_Horizontal: updateListView called for row: " + parentId);

    if (childViews == null)
        return;

    int pos = stDataIdx;
    scrollViewObj.removeAllViews();

    Utilities.logDebug("CustomListView_Horizontal: updateListView dataLength " + dataLength + " MAX_UI_CHILDS: " + MAX_UI_CHILDS);

    if (hasFocus == true && (dataLength >= (MAX_UI_CHILDS - 1)) && (dataLength != MAX_UI_CHILDS))
        maxChild = MAX_UI_CHILDS - 1;

    else {
        maxChild = MAX_UI_CHILDS;

        Utilities.logDebug("CustomListView_Horizontal: updateListView: maxChild" + maxChild + " MAX_UI_CHILDS " + MAX_UI_CHILDS);
        //when removing focus, stDatIdx needs to be updated as number of visible cells will increase and can go out of bound
        if ((pos + MAX_UI_CHILDS) > dataLength) {
            stDataIdx = stDataIdx - 1;
            pos = stDataIdx;
        }
    }

    for (int i = 0; i < maxChild; i++) {
        scrollViewObj.addView(adapter.getListCell(pos, childViews[i], parentId));
        pos++;
    }

    Utilities.logDebug("CustomListView_Horizontal: updateListView exiting with max child: " + maxChild);
}


public void setEventCallback(CustomGridViewEventListener eventCallbackObj) {
    eventCallback = eventCallbackObj;
}


private void updateListFocus(int toBeUnfocused, int toBeFocused) {
    Utilities.logDebug("CustomListView_Horizontal: updateListFocus called with focued idx: " + stDataIdx + toBeFocused);
    if(eventCallback != null) {

        if (toBeUnfocused != -1) {
            eventCallback.onCellFocusRemoved((RelativeLayout) scrollViewObj.getChildAt(toBeUnfocused), parentId, stDataIdx + toBeUnfocused);
        }

        if (toBeFocused != -1) {
            eventCallback.onCellFocused((RelativeLayout) scrollViewObj.getChildAt(toBeFocused), parentId, stDataIdx + toBeFocused );
        }
    }
}

}

0 个答案:

没有答案