可扩展列表视图给出了错误的组位置,只有在没有视图持有者的情况下才能正常工作
expandable list view gives wrong group position, only works correctly without view holder
我无法在不使用视图持有者模式的情况下继续,因为我在每个列表中都是 运行 倒计时,没有它,Expandablelistview 会变得不稳定。也许这是一个简单的修复。提前致谢。
@Override
public View getGroupView(int groupPosition, final boolean isExpanded, View convertView, final ViewGroup parent) {
final ViewHolderItem viewHolder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_lobby, null);
viewHolder = new ViewHolderItem();
viewHolder.lobbyItemExpandIndicator = (ImageView) convertView.findViewById(R.id.lobbyItemExpandIndicator);
viewHolder.draftEnding = (TextView) convertView.findViewById(R.id.lobbyDraftEnding);
viewHolder.gameType = (TextView) convertView.findViewById(R.id.lobbyGameType);
viewHolder.accepting = (TextView) convertView.findViewById(R.id.lobbyAccepting);
viewHolder.gamesInDraft = (TextView) convertView.findViewById(R.id.lobbyGamesInDraft);
//viewHolder.draftEnding.setText(groupPosition + "");
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
final ContestItem contest = mContests.get(groupPosition);
//setCountdown(viewHolder, contest);
viewHolder.gameType.setText(contest.getGameType());
viewHolder.accepting.setText(contest.getAccepting());
viewHolder.gamesInDraft.setText(contest.getNbaGamesAmnt());
if (isExpanded) {
viewHolder.lobbyItemExpandIndicator.setImageResource(R.drawable.w_dash);
} else {
viewHolder.lobbyItemExpandIndicator.setImageResource(R.drawable.w_down);
}
return convertView;
}
static class ViewHolderItem {
TextView draftEnding;
TextView gameType;
TextView accepting;
TextView gamesInDraft;
ImageView lobbyItemExpandIndicator;
}
您应该将倒计时保存在组模型对象中,而不是保存在视图中,每次都会重复使用视图。所以你必须在你的组模型中保持倒计时,然后每次都获取视图恢复倒计时。
例如,如果您显示的数据是:
private HashMap<MenuCategoryModel, List<ChildCategoryModel>> mMainMenuExpandableList;
那么你必须在 MenuCategoryModel
中保持你的倒计时,或者如果对于每个子项目,那么在 ChildCategoryModel
中并且在获取视图组或子项(无论需要什么)时 - 恢复倒计时。
我无法在不使用视图持有者模式的情况下继续,因为我在每个列表中都是 运行 倒计时,没有它,Expandablelistview 会变得不稳定。也许这是一个简单的修复。提前致谢。
@Override
public View getGroupView(int groupPosition, final boolean isExpanded, View convertView, final ViewGroup parent) {
final ViewHolderItem viewHolder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_lobby, null);
viewHolder = new ViewHolderItem();
viewHolder.lobbyItemExpandIndicator = (ImageView) convertView.findViewById(R.id.lobbyItemExpandIndicator);
viewHolder.draftEnding = (TextView) convertView.findViewById(R.id.lobbyDraftEnding);
viewHolder.gameType = (TextView) convertView.findViewById(R.id.lobbyGameType);
viewHolder.accepting = (TextView) convertView.findViewById(R.id.lobbyAccepting);
viewHolder.gamesInDraft = (TextView) convertView.findViewById(R.id.lobbyGamesInDraft);
//viewHolder.draftEnding.setText(groupPosition + "");
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
final ContestItem contest = mContests.get(groupPosition);
//setCountdown(viewHolder, contest);
viewHolder.gameType.setText(contest.getGameType());
viewHolder.accepting.setText(contest.getAccepting());
viewHolder.gamesInDraft.setText(contest.getNbaGamesAmnt());
if (isExpanded) {
viewHolder.lobbyItemExpandIndicator.setImageResource(R.drawable.w_dash);
} else {
viewHolder.lobbyItemExpandIndicator.setImageResource(R.drawable.w_down);
}
return convertView;
}
static class ViewHolderItem {
TextView draftEnding;
TextView gameType;
TextView accepting;
TextView gamesInDraft;
ImageView lobbyItemExpandIndicator;
}
您应该将倒计时保存在组模型对象中,而不是保存在视图中,每次都会重复使用视图。所以你必须在你的组模型中保持倒计时,然后每次都获取视图恢复倒计时。
例如,如果您显示的数据是:
private HashMap<MenuCategoryModel, List<ChildCategoryModel>> mMainMenuExpandableList;
那么你必须在 MenuCategoryModel
中保持你的倒计时,或者如果对于每个子项目,那么在 ChildCategoryModel
中并且在获取视图组或子项(无论需要什么)时 - 恢复倒计时。