我有一个自定义的ArrayAdapter,我在其他视图的顶部以编程方式显示的视图存在问题。我的XML列表布局有一些固定的视图,然后我根据对象动态添加其他视图。只有我动态添加的视图才重复。显然,当视图被回收时,它不像其他视图一样清除视图。但经过大量搜索后,我找不到在arrayAdapter中动态创建视图的任何示例。任何帮助,将不胜感激。感谢。
CustomArrayAdapter.java
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Show/Hide Columns"
ItemsSource="{Binding Columns}">
<MenuItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"></TextBlock>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</ContextMenu>
更新代码:
public class CustomArmyAdapter extends ArrayAdapter<CustomArmy> {
GridLayout mGrid;
// View lookup cache
private static class ViewHolder {
}
public CustomArmyAdapter(Context context, ArrayList<CustomArmy> armies) {
super(context, 0, armies);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomArmy army = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.individual_army_view, parent, false);
}
mGrid = (GridLayout) convertView.findViewById(R.id.armyGridContainer);
TextView armyNameView = (TextView) convertView.findViewById(R.id.army_name_value);
armyNameView.setText(army.getName());
int row = buildArmyCountAndIconViews(army);
TextView townHallLabelView = (TextView) convertView.findViewById(R.id.townHallLabel);
GridLayout.LayoutParams paramTHLabel = new GridLayout.LayoutParams();
paramTHLabel.rowSpec = GridLayout.spec(row);
paramTHLabel.columnSpec = GridLayout.spec(0, 3);
paramTHLabel.setMargins(0,10,0,0);
townHallLabelView.setLayoutParams(paramTHLabel);
TextView townHallValueView = (TextView) convertView.findViewById(R.id.townHallValue);
GridLayout.LayoutParams paramTownHall = new GridLayout.LayoutParams();
paramTownHall.rowSpec = GridLayout.spec(row);
paramTownHall.columnSpec = GridLayout.spec(3, 2);
paramTownHall.setMargins(5,10,0,0);
townHallValueView.setText(army.getTownHallRequired());
townHallValueView.setLayoutParams(paramTownHall);
Button loadButton = (Button) convertView.findViewById(R.id.loadButton);
GridLayout.LayoutParams loadParam = new GridLayout.LayoutParams();
loadParam.rowSpec = GridLayout.spec(row + 1);
loadParam.columnSpec = GridLayout.spec(2, 3);
loadButton.setLayoutParams(loadParam);
return convertView;
}
private int buildArmyCountAndIconViews(CustomArmy army) {
int column = 0;
int row = 1;
for (LinkedHashMap.Entry<String, Integer> armySet : army.getArmyValuesMap().entrySet() ) {
buildArmyIconView(armySet.getKey(), column, row);
buildArmyCountView(armySet.getValue(), column, row);
if (column < 9) {
column++;
} else {
column = 0;
row += 2;
}
}
return row + 3;
}
private void buildArmyIconView(String iconName, int column, int row) {
ImageView iconView = new ImageView(getContext());
GridLayout.LayoutParams iconParam = new GridLayout.LayoutParams();
iconParam.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getContext().getResources().getDisplayMetrics());
iconParam.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getContext().getResources().getDisplayMetrics());
iconParam.columnSpec = GridLayout.spec(column);
iconParam.rowSpec = GridLayout.spec(row);
iconParam.setMargins(7,10,0,0);
iconView.setImageResource(getIconFromName(iconName));
iconView.setLayoutParams(iconParam);
mGrid.addView(iconView);
}
private void buildArmyCountView(int count, int column, int row) {
TextView countView = new TextView(getContext());
GridLayout.LayoutParams paramCount = new GridLayout.LayoutParams();
paramCount.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getContext().getResources().getDisplayMetrics());
paramCount.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getContext().getResources().getDisplayMetrics());
paramCount.columnSpec = GridLayout.spec(column);
paramCount.rowSpec = GridLayout.spec(row + 1);
paramCount.setMargins(7,5,0,0);
countView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
countView.setText("x" + String.valueOf(count));
countView.setLayoutParams(paramCount);
mGrid.addView(countView);
}
private int getIconFromName(String iconName) {
int iconId = getContext().getResources().getIdentifier(iconName + "1", "drawable", "com.clashtoolkit.clashtoolkit");
return iconId;
}
}
答案 0 :(得分:1)
好的,所以这是基于布莱恩的答案。我想出了如何避免重复。我将删除else子句中的所有视图,然后重新创建动态视图。这解决了我的问题。如果有人看到这个问题让我知道,但现在这就是我要去的地方
工作代码
lm()
答案 1 :(得分:0)
如果您实施ViewHolder
模式,我认为您应该能够在Layout
文件中添加另一个individual_army_view.xml
来保存动态视图。然后在getView()
中,您可以为该布局添加视图。像这样:
public static class ViewHolder {
public LinearLayout linearLayout;
...
}
@Override
public view getView(int position, View convertView, ViewGroup parent) {
CustomArmy army = getItem(position);
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
holder.linearLayout.addView(<yourCustomView>);
} ...
}