Android - 在某些可扩展列表的子级中操纵可见性

时间:2012-04-23 19:11:00

标签: android listview visibility children

在Android中,我试图在 getChildView()函数中操作一些可扩展列表的子行,但下一个代码似乎无法正常工作:

@Override
public View getChildView(int groupPosition, int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {
        View childRow = inflater.inflate(R.layout.buslinescheduleitem, null);
        if(childPosition%4==0) {
            int k = 0;
            TextView textView;
            while((childPosition+k) < getChildrenCount(groupPosition) && k < 4){
                textView= (TextView) childRow.findViewById(textViewIds[k]);
                textView.setText(getChild(groupPosition, childPosition+k));
                k++;
            }
            childRow.setVisibility(View.VISIBLE);
        }
        else { childRow.setVisibility(View.GONE); }
        return childRow;
}

虽然某些子行已使用Visibility GONE进行了修改且未显示视图,但groupView的“漏洞”不应该显示(就好像这些行具有可见性且具有INVISIBLE值)。创建可扩展列表时是否可以隐藏一些子行?

1 个答案:

答案 0 :(得分:0)

固定。我每四行只创建一行:

@Override
public View getChildView(int groupPosition, int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {
    View childRow = inflater.inflate(R.layout.buslinescheduleitem, null);
        int k = 0;
        TextView textView;
        while(((childPosition*NUMBER_ROWS)+k) < getChildrenSize(groupPosition) && k < NUMBER_ROWS){
            textView= (TextView) childRow.findViewById(textViewIds[k]);
            textView.setText(getChild(groupPosition, (childPosition*NUMBER_ROWS)+k));
            k++;
        }
        while(k < NUMBER_ROWS){
            textView= (TextView) childRow.findViewById(textViewIds[k]);
            textView.setVisibility(View.GONE);
            k++;
        }
        return childRow;    
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    if(!isInverted) return (int) Math.ceil((float)busLine.getBusScheduleRound().get(groupPosition).getHours().size()/(float)NUMBER_ROWS);
    else return (int) Math.ceil((float)busLine.getBusScheduleReturn().get(groupPosition).getHours().size()/(float)NUMBER_ROWS);
}

public int getChildrenSize(int groupPosition){
    if(!isInverted) return busLine.getBusScheduleRound().get(groupPosition).getHours().size();
    else return busLine.getBusScheduleReturn().get(groupPosition).getHours().size();        
}

尽管如此,我还没有达到修改可见性的目的。