ListView 中使用checkbox状态错乱的问题
2018-04-12 本文已影响0人
弥勒佛_1959
public class OperLayerItemAdapter extends BaseAdapter {
private ArrayList<LayerItem> mList;
private Context mContext;
private LayoutInflater mInflater;// 得到一个LayoutInfalter对象用来导入布局
private int mTouchItemPosition=-1;
private String mISelPos ="";
public void SetSelPos(String pos){
mISelPos = pos;
}
public OperLayerItemAdapter(Context context, ArrayList<LayerItem> list) {
super();
mContext = context;
this.mList = list;
this.mInflater = LayoutInflater.from(context);
}
private OnSeekBarValueChangeListener mOnSeekBarValueChangeListener=null;
public interface OnSeekBarValueChangeListener{
void onClick(SeekBar seekBar, int progress, boolean fromUser,int pos);
};
public void setOnSeekBarValueChangeListener(OnSeekBarValueChangeListener SeekBarValueChangeListener){
mOnSeekBarValueChangeListener = SeekBarValueChangeListener;
}
private OnCheckedValueChangeListener mOnCheckedValueChangeListener=null;
public interface OnCheckedValueChangeListener{
void onClick(CompoundButton buttonView, boolean isChecked, int pos);
};
public void setOnCheckedValueChangeListener(OnCheckedValueChangeListener CheckedValueChangeListener){
mOnCheckedValueChangeListener = CheckedValueChangeListener;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(final int pos, View convertView, ViewGroup arg2) {
final childViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_layer_item, null);
viewHolder = new childViewHolder();
viewHolder.LayerImageView = (ImageView) convertView
.findViewById(R.id.id_layeritem_iv);
viewHolder.LayerTextView = (TextView) convertView
.findViewById(R.id.id_layeritem_tv);
viewHolder.LayerCheckBox = (CheckBox) convertView
.findViewById(R.id.id_layeritem_cb);
viewHolder.LayerSeekBar = (SeekBar)convertView.findViewById(R.id.id_layer_seekBar);
viewHolder.LayerSeekBar.setTag(pos);
convertView.setTag(viewHolder);
} else {
viewHolder = (childViewHolder) convertView.getTag();
}
if (pos%2==0) {
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.item_dark_gray));
}else{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.item_light_gray));
}
viewHolder.LayerImageView.setBackgroundColor(FeatureRenderer.getLayerColor(mList.get(pos).get_lyrId()));
String name =mList.get(pos).getlryName();
viewHolder.LayerTextView.setText(mList.get(pos).getlryName());
//在CheckBox的onCheckedChanged中重新赋值个数据组,并且将设置复选框的选中状态在CheckedChangeListener之后
viewHolder.LayerCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mOnCheckedValueChangeListener.onClick(buttonView,isChecked,pos);
if (isChecked)
mList.get(pos).setlryCheck(true);
else
mList.get(pos).setlryCheck(false);
}
});
viewHolder.LayerCheckBox.setChecked((Boolean) mList.get(pos).islryCheck());
viewHolder.LayerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int pos1 = (int) viewHolder.LayerSeekBar.getTag();
mOnSeekBarValueChangeListener.onClick(seekBar,progress,fromUser,pos);
float Opacity = (float) (progress / 100.00);
mList.get(pos).setlyrOpacity(Opacity);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
int Opacity = (int) (mList.get(pos).getlyrOpacity()*100);
viewHolder.LayerSeekBar.setProgress(Opacity);
if (mList.get(pos).getlryName().equals(mISelPos)){
viewHolder.LayerTextView.setTextColor(Color.RED);
}else{
viewHolder.LayerTextView.setTextColor(Color.BLACK);
}
return convertView;
}
class childViewHolder {
public ImageView LayerImageView;
public TextView LayerTextView;
public CheckBox LayerCheckBox;
public SeekBar LayerSeekBar;
}
}