gpt4 book ai didi

java - ExpandableListView 不调用 getGroupView 也不填充

转载 作者:行者123 更新时间:2023-12-01 11:19:22 27 4
gpt4 key购买 nike

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

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);
}

自定义适配器.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 方法。任何帮助将不胜感激。

最佳答案

改变

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

return rootView;

关于java - ExpandableListView 不调用 getGroupView 也不填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31442725/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com