gpt4 book ai didi

android - 当我们在 android 的可扩展 ListView 中打开特定组时,GroupText 发生了变化

转载 作者:行者123 更新时间:2023-11-29 22:07:57 28 4
gpt4 key购买 nike

我有一个问题,当我们点击特定组打开时,我的组文本会自动更改。我找不到这个问题的根源。请向我建议有关相同的任何解决方案。

enter image description here

打开后:

enter image description here

代码(扩展适配器):

public class ExpResearchListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for
// groups[i].
Context context;
private String[] groups = { "Research by Brand", "Research by Category",
"Research by Price" , "Research by Fuel Economy", "Recently Viewed"};
GridView label;
TextView groupText;
String group;
ImageView groupImg;
//ArrayList<String> groups = new ArrayList<String>();
ArrayList<Integer> groupImage = new ArrayList<Integer>();
ArrayList<Integer> childElement = new ArrayList<Integer>();
private Integer[][] children = {
{ R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3, R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6, R.drawable.icon_7, R.drawable.icon_8, R.drawable.icon_9,
R.drawable.icon_10, R.drawable.icon_11, R.drawable.icon_12, R.drawable.icon_13, R.drawable.icon_14, R.drawable.icon_15, R.drawable.icon_16, R.drawable.icon_17, R.drawable.icon_18,
R.drawable.icon_19, R.drawable.icon_20, R.drawable.icon_21, R.drawable.icon_22, R.drawable.icon_23, R.drawable.icon_24, R.drawable.icon_25, R.drawable.icon_26, R.drawable.icon_27,
R.drawable.icon_28, R.drawable.icon_29, R.drawable.icon_30, R.drawable.icon_31, R.drawable.icon_32, R.drawable.icon_33, R.drawable.icon_34, R.drawable.icon_35, R.drawable.icon_36,
R.drawable.icon_37, R.drawable.icon_38, R.drawable.icon_39, R.drawable.icon_40, R.drawable.icon_41, R.drawable.icon_42, R.drawable.icon_43, R.drawable.icon_44, R.drawable.icon_45,
R.drawable.icon_46}
};
LinearLayout linear;

public ExpResearchListAdapter(Context context, ArrayList<String> groups, ArrayList<Integer> groupImage, ArrayList<Integer> childElement, LinearLayout linear)
{
this.context=context;
//this.groups = groups;
this.groupImage = groupImage;
this.childElement = childElement;
this.linear = linear;
}

public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}

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

public int getChildrenCount(int groupPosition) {
int i = 0;
try {
i = children[groupPosition].length;

} catch (Exception e) {
}

return 1;
}



public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView==null)
{
LayoutInflater inflater = LayoutInflater.from(context);
convertView = (View) inflater.inflate(R.layout.brand_research_grid, null);
label = (GridView) convertView.findViewById(R.id.gv_ResearchList_Child);
label.setAdapter(new GridAdapter(context, childElement));
label.setCacheColorHint(Color.WHITE);

// initialize the following variables (i've done it based on your layout
// note: rowHeightDp is based on my grid_cell.xml, that is the height i've
// assigned to the items in the grid.
//final int spacingDp = 20;
final int colWidthDp = 50;
final int rowHeightDp = (childElement.size()/3)*10;
//final int rowHeightDp = 107;

// convert the dp values to pixels
//final float COL_WIDTH = context.getResources().getDisplayMetrics().density * colWidthDp;
final float COL_WIDTH = 10;
//final float ROW_HEIGHT = context.getResources().getDisplayMetrics().density * rowHeightDp;
final float ROW_HEIGHT = 107f;
final float SPACING = context.getResources().getDisplayMetrics().density * 0;
System.out.println("===================================RowHeight"+ROW_HEIGHT);
System.out.println("===================================RowHeightDP"+rowHeightDp);


// calculate the column and row counts based on your display
final int colCount = (int)Math.floor((linear.getWidth() - (2 * SPACING)) / (COL_WIDTH + SPACING));
final int rowCount = (int)Math.ceil((childElement.size() + 0d) / 3);
//final int rowCount = 16;

// calculate the height for the current grid
final int GRID_HEIGHT = Math.round(rowCount * (ROW_HEIGHT + SPACING));
System.out.println("===================================GHieght"+GRID_HEIGHT);
System.out.println("===================================colCount"+colCount);
System.out.println("===================================rowCount"+rowCount);
// set the height of the current grid
label.getLayoutParams().height = GRID_HEIGHT;
}





return convertView;
}

public Object getGroup(int groupPosition) {
//return groups.get(groupPosition);
return groups[groupPosition];
}

public int getGroupCount() {
return groups.length;
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.research_list_exp_group, null);
groupText = (TextView) convertView
.findViewById(R.id.tv_ResearchList_ExpParentElement);
groupImg = (ImageView) convertView
.findViewById(R.id.img_ResearchList_GroupParentImage);
group = (String) getGroup(groupPosition);
// convertView.setClickable(false);
groupText.setText(groups[groupPosition]);
groupImg.setImageResource(groupImage.get(groupPosition));
}


return convertView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

public boolean hasStableIds() {
return true;
}

}

提前致谢。

最佳答案

问题出在getGroupView() 函数,您错误地使用了convertView。如果 convertView 为 null,则您正在正确地展开布局并填充 View 。但是,如果它不为空,则您不会更改 View !该 View 是一个循环 View - 它是以前的组项目 View 之一,但您不知道是哪一个!你也必须填写它。当您点击组项目时,内容会被重新绘制,所有 View 可能会被回收。

您在 getChildView() 函数中遇到了同样的问题。

检查 this教程,尤其是示例 9.5。它展示了如何正确使用回收 View (以及如何使用 holder 模式)。

请注意,您的代码中还有另一个问题 - 您正试图在 ListView (ExpandableListView) 中使用 GridView。不要那样做!不要使用两个能够在另一个内部滚动的小部件。除非每个 ScrollView 使用不同的轴进行滚动,否则不要这样做。

关于android - 当我们在 android 的可扩展 ListView 中打开特定组时,GroupText 发生了变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341330/

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