ExpandableListView不调用getGroupView也不填充

时间:2015-07-15 23:05:09

标签: java android expandablelistview

我正在编写一个使用ExpandableListView功能的Android应用程序,出于某种原因,我似乎无法填充视图。我最初使用的是常规ListView,其中填充了正确的信息。然后我重构了代码以使用ExpandableListView。以下是我项目中的所有相关摘录:

SeasonsFragment.java(在MainActivity中正确调用)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    ArrayList<Season> seasons = ((App)getActivity().getApplication()).getSeasonsList();
    ArrayList<RowItem> rowItems = new ArrayList<>();
    for (int i = 0; i < seasons.size(); i++) {
        RowItem items = new RowItem(seasons.get(i));
        rowItems.add(items);
    }
    CustomAdapter adapter = new CustomAdapter(getActivity(), rowItems);
    View rootView = inflater.inflate(R.layout.seasons_fragment, container, false);
    ExpandableListView list_view = (ExpandableListView) rootView.findViewById(R.id.list);
    list_view.setAdapter(adapter);

    return inflater.inflate(R.layout.seasons_fragment, container, false);
}

CustomAdapter.java

public class CustomAdapter extends BaseExpandableListAdapter {

    Context context;
    List<RowItem> rowItem;
    private HashMap<String, List<Sport>> listDataChildren = new HashMap<>();

    public CustomAdapter(Context context, List<RowItem> rowItem) {
        Log.i("Custom Adapter", "Created");
        this.context = context;
        this.rowItem = rowItem;
        for (RowItem item : rowItem){
            Season season = item.getSeason();
            listDataChildren.put(item.getText(), season.getSports());
        }
    }

    @Override
    public int getGroupCount() {
        return rowItem.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return listDataChildren.get(rowItem.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return rowItem.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listDataChildren.get(rowItem.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        Log.i("Group ID", "Getting group ID");
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Log.i("Group View", "Group view called");
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_item, null);
        }
        final RowItem row_pos = rowItem.get(groupPosition);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
        txtTitle.setText(row_pos.getText());
        Button button = (Button) convertView.findViewById(R.id.button); //TODO: Check memory for subscription status
        if (row_pos.isSubscribed()){
            row_pos.setSubscribed(false);
            button.setBackgroundResource(R.drawable.checkmark3);
        } else {
            row_pos.setSubscribed(true);
            button.setBackgroundResource(R.drawable.ic_add_black_24dp);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {/*
                if (item.isSubscribed()){
                    item.setSubscribed(false); //TODO: This isn't working properly.
                    v.setBackgroundResource(R.drawable.checkmark3);
                } else {
                    item.setSubscribed(true);
                    v.setBackgroundResource(R.drawable.ic_add_black_24dp);
                }*/
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText("TEST");
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

所有布局XML都在ListView中正确膨胀,但在ExpandableListView中没有任何东西在膨胀。方法getGroupView甚至没有被调用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

更改

return inflater.inflate(R.layout.seasons_fragment, container, false);

return rootView;