当我的 ListView 有更多可以出现在屏幕上的项目时,我遇到了问题;换句话说,当它滚动时。问题是,当我单击其中一个切换按钮时,它会更改 ImageView 的可见性。但是,当我单击它时,它会改变比相应单击更多的可见性。
我正在使用适配器来显示列表项。
我添加了以下代码:
public class CriteriosAdapter extends ArrayAdapter<Criterio> {
private Context context;
public CriteriosAdapter(Context context, List<Criterio> objects) {
super(context, 0, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Criterio criterio = getItem(position);
final CriterioViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_row, parent, false);
viewHolder = new CriterioViewHolder();
viewHolder.txtCriterio = (TextView)convertView.findViewById(R.id.txtCriterio);
viewHolder.tgIrregular = (ToggleButton)convertView.findViewById(R.id.tgIrregular);
viewHolder.btnCam = (ImageView) convertView.findViewById(R.id.btnCam);
convertView.setTag(viewHolder);
}
else {
viewHolder = (CriterioViewHolder)convertView.getTag();
}
viewHolder.txtCriterio.setText(criterio.nome);
viewHolder.txtCriterio.setTextColor(context.getColor(R.color.white));
viewHolder.tgIrregular.setChecked(false);
viewHolder.tgIrregular.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.tgIrregular.isChecked()) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
}
}
});
viewHolder.btnCam.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
FragmentActivity activity = (FragmentActivity)(context);
android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
FotosFragment alertDialog = new FotosFragment();
alertDialog.show(fm, "fragment_alert");
}
});
return convertView;
}
class CriterioViewHolder {
TextView txtCriterio;
ToggleButton tgIrregular;
ImageView btnCam;
}
}
有人可以帮我吗?
谢谢你。
编辑
我按照您的描述添加了所有内容。但是,我在 5 个片段中使用该适配器,如果我转到另一个片段,然后返回或滚动列表,它仍然会失去价值。
我使用了criterio.getHash,因为它对所有人都是独一无二的。和位置,可以在其他片段中重复,并使其变得怪异。
public class CriteriosAdapter extends ArrayAdapter<Criterio> {
private Context context;
public static Map<String, Criterio> irregularidades = new HashMap<String, Criterio>();
HashMap<String, Boolean> toggleButtonStateTracker = new HashMap<>();
public CriteriosAdapter(Context context, List<Criterio> objects) {
super(context, 0, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Criterio criterio = getItem(position);
final CriterioViewHolder viewHolder;
if (!toggleButtonStateTracker.containsKey(criterio.getHash())){
toggleButtonStateTracker.put(criterio.getHash(),false);
}
Log.e("Toggle Track:", toggleButtonStateTracker.toString());
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_row, parent, false);
viewHolder = new CriterioViewHolder();
viewHolder.txtCriterio = (TextView)convertView.findViewById(R.id.txtCriterio);
viewHolder.tgIrregular = (ToggleButton)convertView.findViewById(R.id.tgIrregular);
viewHolder.btnCam = (ImageView) convertView.findViewById(R.id.btnCam);
convertView.setTag(viewHolder);
}
else {
viewHolder = (CriterioViewHolder)convertView.getTag();
}
viewHolder.txtCriterio.setText(criterio.nome);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
viewHolder.txtCriterio.setTextColor(context.getColor(R.color.white));
} else {
viewHolder.txtCriterio.setTextColor(context.getResources().getColor(R.color.white));
}
viewHolder.tgIrregular.setId(criterio.id);
final boolean isChecked = toggleButtonStateTracker.get(criterio.getHash());
viewHolder.tgIrregular.setChecked(isChecked);
viewHolder.btnCam.setTag(criterio.hash);
if (isChecked) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
}
viewHolder.tgIrregular.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggleButtonStateTracker.put(criterio.getHash(), isChecked);
if (isChecked) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
irregularidades.put(criterio.getHash(), criterio);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
irregularidades.remove(criterio.getHash());
}
}
});
viewHolder.btnCam.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
FragmentActivity activity = (FragmentActivity) (context);
android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
FotosFragment alertDialog = new FotosFragment();
Bundle args = new Bundle();
args.putString("criterioTag", criterio.getHash());
alertDialog.setArguments(args);
alertDialog.setCancelable(false);
alertDialog.show(fm, "fragment_alert");
}
});
return convertView;
}
class CriterioViewHolder {
TextView txtCriterio;
ToggleButton tgIrregular;
ImageView btnCam;
}
}