Пользовательские строки ListView, показывающие 1 дочерний элемент вместо всех 4 - PullRequest
0 голосов
/ 25 января 2012

У меня есть пользовательский ListView, который содержит вертикальный LinearLayout из горизонтальных LinearLayouts для каждого элемента списка.Горизонтальный LinearLayout содержит 4 кнопки.Проблема в том, что при отображении ListView отображается только 1 из 4 кнопок.

У меня есть пустой вертикальный LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dip"
    android:background="@color/all_white"
    android:orientation="vertical"
    android:id="@+id/linear_layout"
    />

Мой горизонтальный LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:background="@color/all_white">
<Button
    android:id="@+id/ws_1"
    android:layout_width="175dip"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:paddingTop="10dip"  
    android:paddingBottom="10dip" 
    />
<Button
    android:id="@+id/ws_2"
    android:layout_width="175dip"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:paddingTop="10dip"  
    android:paddingBottom="10dip" 
    />
<Button
    android:id="@+id/ws_3"
    android:layout_width="175dip"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:paddingTop="10dip"  
    android:paddingBottom="10dip" 
    />
<Button
    android:id="@+id/ws_4"
    android:layout_width="175dip"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:paddingTop="10dip"  
    android:paddingBottom="10dip" 
    />    
</LinearLayout>

и мой getView пользовательского класса BaseAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewGroup viewGroup = null;
    int numRows = 0;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewGroup = (ViewGroup) inflater.inflate(R.layout.linear_layout, null);
    } else {
        viewGroup = (ViewGroup) convertView;
        numRows = viewGroup.getChildCount();
    }
    _keys = (String[]) _workspacesMap.keySet().toArray(new String[_workspacesMap.size()]);
    List<WorkspaceInfo> workspaces = new ArrayList<WorkspaceInfo>();
    try {
        if (_keys[position] != null) {
            workspaces = _workspacesMap.get(_keys[position]);
            Collections.sort(workspaces, new InfoNameComparator());
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        //
    }

    int totalRows = 0;
    if (workspaces.size() > 0) {
        totalRows = workspaces.size() / 4 + 1;
        if (totalRows % 4 != 0) {
            totalRows++;
        }
    }

    if (numRows < totalRows) {
        for (int i = numRows; i < totalRows; i++) {
            View view;
            LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = (View) inflater.inflate(R.layout.list_row_multi_ws, null);
            viewGroup.addView(view, i);
        }
    } else if (numRows > totalRows) {
        for (int i = totalRows; i >= numRows; i--) {
            viewGroup.removeViewAt(totalRows);
        }
    }

    Iterator<WorkspaceInfo> iterator = workspaces.iterator();
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        ViewGroup temp = (ViewGroup) viewGroup.getChildAt(i);
        for (int j = 0; j < temp.getChildCount(); j++) {
            Button button = (Button) temp.getChildAt(j);
            if (iterator.hasNext()) {
                String buttonText = iterator.next().getTitle();
                button.setText(buttonText);
            } else {
                button.setVisibility(View.GONE);
            }
        }
    }
    return viewGroup;
}

1 Ответ

0 голосов
/ 25 января 2012

Попробуйте изменить метод инфляции с помощью этого:

inflater.inflate(R.layout.linear_layout, parent, false);

Это может помочь.

...