我有当前的适配器:
public class FolderImagesAdapter extends BaseAdapter {
private List<String> mData;
private Context mContext;
public FolderImagesAdapter(Context context, List<String> data) {
mContext = context;
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = new FolderImageItemView(mContext);
}
((FolderImageItemView)convertView).setData(mData.get(position));
return convertView;
}
public void setData(List<String> data) {
mData = data;
}
}
这是我的View类:
public class FolderImageItemView extends FrameLayout implements Checkable{
private ImageView mThumbnail;
private View mSelection;
private String mPath;
private boolean mChecked;
private SelectedImagesHolder mHolder;
public FolderImageItemView(Context context) {
super(context);
init();
}
private void init() {
inflate(getContext(), R.layout.item_view_folder_image, this);
mThumbnail = (ImageView)findViewById(R.id.iv_thumb);
mSelection = findViewById(R.id.selection);
}
public void setData(String thumbPath){
mPath = thumbPath;
mHolder = SelectedImagesHolder.getInstance();
Utils.toLog(mHolder.getmSelectedImages().toArray());
Glide.with(getContext()).load(thumbPath).into(mThumbnail);
if(mHolder.getmSelectedImages().contains(thumbPath)) {
setChecked(true);
}
}
@Override
public void setChecked(boolean checked) {
mChecked = checked;
if(mChecked) {
mSelection.setVisibility(VISIBLE);
if(!mHolder.getmSelectedImages().contains(mPath)) {
mHolder.addImage(mPath);
}
} else {
mSelection.setVisibility(GONE);
//mHolder.removeImage(mPath);
}
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
}
问题是setData
方法应检查项目,持有者已经拥有(持有者是单线)。 我有单数的数据,并且setChecked(true)正在运行,但是在setChecked(false)之后的某个地方调用所有项目并且所有项目都被取消选择。
我想知道这里有什么问题,我该如何解决呢。我已经搜索了这个并找到一些帖子,说明getView
正在调用setChecked(false),但是setData
方法在那里被调用,它应该可以工作。
答案 0 :(得分:0)
所以,最终的解决方案并不是很好,但它正在发挥作用。首先是几件事:
performClick
覆盖由于某种原因未触发,但setOnClickListener(this)
和toggle
有效(我猜它与处理ListView中的点击和此类ViewGroups有关就好了。
如果我删除了Checkable的工具,它将无法工作,因为我需要触发此setChecked
我的解决方案是简单地创建boolean mIsFromClick
并仅在发生点击时将其设置为true,并且仅当此字段设置为true时才执行操作,因此,最终它们看起来像这样:
public class FolderImageItemView extends FrameLayout implements Checkable, OnClickListener {
private ImageView mThumbnail;
private View mSelection;
private String mPath;
private boolean mChecked;
private boolean mIsFromClicked = false;
private SelectedImagesHolder mHolder;
public FolderImageItemView(Context context) {
super(context);
setOnClickListener(this);
init();
}
private void init() {
inflate(getContext(), R.layout.item_view_folder_image, this);
mThumbnail = (ImageView)findViewById(R.id.iv_thumb);
mSelection = findViewById(R.id.selection);
}
public void setData(String thumbPath){
mPath = thumbPath;
mHolder = SelectedImagesHolder.getInstance();
Utils.toLog(mHolder.getmSelectedImages().toArray());
Glide.with(getContext()).load(thumbPath).into(mThumbnail);
if(mHolder.getmSelectedImages().contains(mPath)) {
mIsFromClicked = true;
setChecked(true);
}
}
@Override
public void setChecked(boolean checked) {
//Utils.toLog(new Throwable());
if(mIsFromClicked) {
mChecked = checked;
if (mChecked) {
mSelection.setVisibility(VISIBLE);
if (!mHolder.getmSelectedImages().contains(mPath)) {
mHolder.addImage(mPath);
}
} else {
mSelection.setVisibility(GONE);
mHolder.removeImage(mPath);
}
mIsFromClicked = false;
}
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
mIsFromClicked = true;
setChecked(!mChecked);
}
@Override
public void onClick(View v) {
Utils.toLog("click performed");
toggle();
}
}
更好的方法应该在答案中发布,谢谢大家。