我正在使用CircularImageView库。我的CircularImageView
中有一个ViewHolder
,我想在点击时更改其边框颜色。
我的onBindViewHolder
:
public void onBindViewHolder(ThumbnailAdapter.ViewHolder holder, int position) {
... //getting the relevant user from dataset and other irrelevant stuff
if(selectedUsers.size() == 0 || selectedUsers.contains(user)) {
holder.thumbnail.setBorderColor(R.color.selected);
} else {
holder.thumbnail.setBorderColor(R.color.not_selected);
}
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedUsers.contains(user)) {
selectedUsers.remove(user);
} else {
selectedUsers.add(user);
}
notifyDataSetChanged();
}
});
}
奇怪的是,第一个着色工作(边框颜色确实不是xml中预定义的颜色),但任何进一步的更改都不会影响视图(尽管调用了事件并且值已更改)。我试图改变drawable以消除与RecyclerView
相关的问题,并且它按预期工作。
我做错了吗?也许库中有一个错误(如果是这样,我找不到它)?任何帮助将不胜感激!
编辑:更多适配器代码:
public ThumbnailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.thumbnail, parent, false);
return new ThumbnailAdapter.ViewHolder(v);
}
static class ViewHolder extends RecyclerView.ViewHolder {
CircularImageView thumbnail;
ViewHolder(View v) {
super(v);
thumbnail = (CircularImageView) v.findViewById(R.id.user_thumbnail);
}
}
private List<User> mDataSet;
private List<User> selectedUsers;
public ThumbnailAdapter(List<User> dataSet) {
mDataSet = dataSet;
selectedUsers = new ArrayList<>();
}
设置适配器:
mAdapter = new ThumbnailAdapter(users);
mRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
mRecyclerView.setAdapter(mAdapter);
答案 0 :(得分:1)
我得到了它,试试这个:
public void onBindViewHolder(RecyclerView.ViewHolder mholder, int position) {
final User user = mList.get(position);
final ViewHolder holder = (ViewHolder) mholder;
if(selectedUsers.size() == 0 || selectedUsers.contains(user))
{
//holder.thumbnail.setBorderColor(Color.GREEN);
setBorder(holder.thumbnail, true);
} else {
//holder.thumbnail.setBorderColor(Color.RED);
setBorder(holder.thumbnail, false);
}
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedUsers.contains(user)) {
selectedUsers.remove(user);
} else {
selectedUsers.add(user);
}
//notifyItemChanged(position);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public void setBorder(CircularImageView circularImageView, boolean selected) {
// Set Border
if(selected) {
circularImageView.setBorderColor(context.getResources().getColor(R.color.colorPrimary));
circularImageView.setBorderWidth(50);
circularImageView.setBackgroundColor(0xffffffff);
}
else
{
circularImageView.setBorderColor(context.getResources().getColor(R.color.colorAccent));
circularImageView.setBorderWidth(50);
circularImageView.setBackgroundColor(0xffffffff);
}
// Add Shadow with default param
//circularImageView.addShadow();
// or with custom param
//circularImageView.setShadowRadius(1);
//circularImageView.setShadowColor(Color.WHITE);
}
您应该为提高绩效做些其他事情:在View.OnClickListener()
内部切换notifyDataSetChanged()
,这会强制LayoutManager
的完整布局,以获得精确方法notifyItemChanged(position);
这将执行您想要的任务而不重绘所有内容
希望这有帮助! =)