带有复选框的自定义列表视图问题
Custom listview issue with checkbox
我正在尝试创建一个带有复选框的列表视图。所以每当我单击一个复选框并将其向下或向上滚动时,这就是问题所在,它会自动检查列表中我没有单击的其他复选框。我已经在此处搜索并阅读了具有相同问题的其他主题,但其中 none 解决了我的问题。
这是我目前所做的。
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_songs, null, true);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongName);
viewHolder.tvSongArtist = (TextView) convertView.findViewById(R.id.tvArtistName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvSongName.setText(_songs.get(position).getSongname());
viewHolder.tvSongArtist.setText(_songs.get(position).getArtistname());
viewHolder.checkBox.setTag(R.integer.btnplusview, convertView);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
_songs.get(getPosition).setSelected(buttonView.isChecked());
if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);
}
}
});
convertView.setTag(R.id.tvSongName, viewHolder.tvSongName);
convertView.setTag(R.id.tvArtistName, viewHolder.tvSongArtist);
convertView.setTag(R.id.checkBox, viewHolder.checkBox);
viewHolder.checkBox.setTag(position);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());
return convertView;
}
我应该怎么做才能解决这个问题?
提前致谢!
每当你滚动 setOnCheckedChangeListener
时调用
删除 setOnCheckedChangeListener
并实施 setOnClickListener
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.checkbox.isChecked()) {
your logic id true
} else {
your logic id false
}
}
});
滚动时调用 setOnCheckedChangeListener。因此,要在获取视图中处理该问题,您需要执行以下操作 -
viewHolder.checkBox.setOnCheckedChangeListener(null);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());
您首先需要设置 onchecked null 以便在滚动时当视图被填充并且您正在为复选框视图分配值时,您不会调用 onCheckedListener。
然后你需要调用OnCheckedChangeListener
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
//remove this line from the code. The below one
_songs.get(getPosition).setSelected(buttonView.isChecked());
if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);
}
}
});
我正在尝试创建一个带有复选框的列表视图。所以每当我单击一个复选框并将其向下或向上滚动时,这就是问题所在,它会自动检查列表中我没有单击的其他复选框。我已经在此处搜索并阅读了具有相同问题的其他主题,但其中 none 解决了我的问题。
这是我目前所做的。
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_songs, null, true);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongName);
viewHolder.tvSongArtist = (TextView) convertView.findViewById(R.id.tvArtistName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvSongName.setText(_songs.get(position).getSongname());
viewHolder.tvSongArtist.setText(_songs.get(position).getArtistname());
viewHolder.checkBox.setTag(R.integer.btnplusview, convertView);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
_songs.get(getPosition).setSelected(buttonView.isChecked());
if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);
}
}
});
convertView.setTag(R.id.tvSongName, viewHolder.tvSongName);
convertView.setTag(R.id.tvArtistName, viewHolder.tvSongArtist);
convertView.setTag(R.id.checkBox, viewHolder.checkBox);
viewHolder.checkBox.setTag(position);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());
return convertView;
}
我应该怎么做才能解决这个问题?
提前致谢!
每当你滚动 setOnCheckedChangeListener
时调用
删除 setOnCheckedChangeListener
并实施 setOnClickListener
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.checkbox.isChecked()) {
your logic id true
} else {
your logic id false
}
}
});
滚动时调用 setOnCheckedChangeListener。因此,要在获取视图中处理该问题,您需要执行以下操作 -
viewHolder.checkBox.setOnCheckedChangeListener(null);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());
您首先需要设置 onchecked null 以便在滚动时当视图被填充并且您正在为复选框视图分配值时,您不会调用 onCheckedListener。
然后你需要调用OnCheckedChangeListener
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
//remove this line from the code. The below one
_songs.get(getPosition).setSelected(buttonView.isChecked());
if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);
}
}
});