我正在使用Recycler视图来显示元素列表。当我们点击状态更改及其背景颜色时,每行上都有一个按钮。我在状态更新后调用notifyDataSetChanged()但是没有刷新recyclelerView。
else if (allGoals.getStatus() == WorkoutCashConstants.GOALS_ACHIEVE) {
boolean networkStatus = checkNetworkStatus();
if (networkStatus) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
if (DATE_ACCESS == null) {
currentTimeStamp = dateFormat.format(new Date());
} else {
currentTimeStamp = DATE_ACCESS;
}
progDialog = ProgressDialog.show(CompanyGoals_AllGoals_Fragment.this.getActivity(), "", "Goal Progress Recorded");
progDialog.setCancelable(true);
AchieveGoals achieveGoals = new AchieveGoals(Integer.parseInt(allGoals.getGoalID()), currentTimeStamp);
Call<AchieveGoals> achieveGoalsCall = apiModule.achieveGoal(achieveGoals);
achieveGoalsCall.enqueue(new Callback<AchieveGoals>() {
@Override
public void onResponse(Response<AchieveGoals> response, Retrofit retrofit) {
progDialog.dismiss();
if (response.isSuccess()) {
progDialog.dismiss();
goalsAchieved = response.body();
if (goalsAchieved.getAppStatusCode() == WorkoutCashConstants.SUCCESS_API) {
realm = Realm.getInstance(getContext());
realm.beginTransaction();
AllGoalsDB goalsDB = realm.where(AllGoalsDB.class)
.equalTo("goalID", allGoals.getGoalID()).findFirst();
CompeletedDatesDB compeletedDatesDB = realm.createObject(CompeletedDatesDB.class);
compeletedDatesDB.setCompletedDate(currentTimeStamp);
goalsDB.getCompeletedDatesDBs().add(compeletedDatesDB);
realm.commitTransaction();
UserInfoDB userInfoDB = realm.where(UserInfoDB.class)
.equalTo(WorkoutCashConstants.
COLUMN_USER_ID, userId)
.findFirst();
if (userInfoDB != null) {
realm.beginTransaction();
userInfoDB.setSweatPoints(goalsAchieved.getData().getSweatPoints());
realm.commitTransaction();
}
realm.close();
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
notifyDataSetChanged()检查getItemCount(),如果更改了与适配器关联的列表中的项目计数,则notifyDataSetChanged()生效。在您的情况下,您可以在适配器中使用toggleSelection函数。我正在附加我的一个示例适配器,以便您可以了解我们如何实现此行为。
public class ToggleSelectionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Cursor mCursor;
private SparseBooleanArray selectedItems;
public ToggleSelectionListAdapter(Cursor cursor) {
mCursor = cursor;
selectedItems = new SparseBooleanArray();
}
public void toggleSelection(int pos) {
if (selectedItems.get(pos, false)) {
selectedItems.delete(pos);
} else {
selectedItems.put(pos, true);
}
notifyItemChanged(pos);
}
public int getSelectedItemCount() {
return selectedItems.size();
}
public void clearSelections() {
selectedItems.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(final View itemView) {
super(itemView);
// Initialize your items of each row here
}
public void bindView(int pos) {
try {
if (mCursor.isClosed())
return;
mCursor.moveToPosition(pos);
// Maintain a checked item list so that you can have a track if the item is clicked or not
if (checkedItems.contains(number) itemView.setBackgroundResource(R.drawable.background_selected);
else itemView.setBackgroundResource(R.drawable.background_normal);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkedItems.contains(number)) {
checkedItems.remove(number);
} else {
checkedItems.add(number);
}
// Get the index of which item should toggle the background
int idx = mRecyclerView.getChildAdapterPosition(v);
toggleSelection(idx);
}
}
});
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
ViewHolder vh = (ViewHolder) holder;
vh.bindView(position);
}
}
@Override
public int getItemCount() {
if (mCursor == null) {
return 0;
}
int n = mCursor.getCount();
return n;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
synchronized public void swapCursor(Cursor cursor) {
mCursor = cursor;
notifyDataSetChanged();
}
}