Android 回收器视图不更新notifyDataSetChanged上的内容

Android 回收器视图不更新notifyDataSetChanged上的内容,android,android-recyclerview,Android,Android Recyclerview,我正在使用Recycler视图显示元素列表。当我们单击每一行时,每一行上都有一个按钮,状态改变,背景颜色改变。状态更新后,我将调用notifyDataSetChanged(),但recyclerView不会刷新 else if (allGoals.getStatus() == WorkoutCashConstants.GOALS_ACHIEVE) { boolean networkStatus = checkNetworkStatus();

我正在使用Recycler视图显示元素列表。当我们单击每一行时,每一行上都有一个按钮,状态改变,背景颜色改变。状态更新后,我将调用notifyDataSetChanged(),但recyclerView不会刷新

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();

                                        }
                                    }
else if(allGoals.getStatus()==WorkoutCashConstants.GOALS\u实现){
布尔网络状态=检查网络状态();
if(网络状态){
日期格式=新的简化格式(“yyyy-MM-dd”);
如果(日期\访问==null){
currentTimeStamp=dateFormat.format(新日期());
}否则{
currentTimeStamp=访问日期;
}
progDialog=ProgressDialog.show(CompanyGoals\u AllGoals\u Fragment.this.getActivity(),“”和“目标进度记录”);
progDialog.setCancelable(真);
AchieveGoals AchieveGoals=新的AchieveGoals(Integer.parseInt(allGoals.getGoalID()),currentTimeStamp);
Call achieveGoalsCall=apimule.achieveGoal(achieveGoals);
enqueue(新的回调函数(){
@凌驾
公共响应(响应、改装){
progDialog.discouse();
if(response.issucess()){
progDialog.discouse();
GoalsAcreated=response.body();
if(goalsAcreated.getAppStatusCode()==WorkoutCashConstants.SUCCESS\u API){
realm=realm.getInstance(getContext());
realm.beginTransaction();
AllGoalsDB goalsDB=realm.where(AllGoalsDB.class)
.equalTo(“goalID”,allGoals.getGoalID()).findFirst();
CompleteDDateSDB CompleteDDateSDB=realm.createObject(CompleteDDateSDB.class);
CompletedDataDB.setCompletedDate(当前时间戳);
goalsDB.getCompletedDateSDB().add(completedDateSDB);
realm.commitTransaction();
UserInfoDB UserInfoDB=realm.where(UserInfoDB.class)
.equalTo(工作时间常数)。
列(用户ID,用户ID)
.findFirst();
if(userInfoDB!=null){
realm.beginTransaction();
userInfoDB.setSweatPoints(goalsAcreated.getData().getSweatPoints());
realm.commitTransaction();
}
realm.close();
notifyDataSetChanged();
}
}
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();
        }
    }
public类ToggleSelectionListAdapter扩展了RecyclerView.Adapter{
私有游标mCursor;
专用SparseBooleanArray selectedItems;
公共切换SelectionListAdapter(光标){
mCursor=光标;
selectedItems=newsparsebooleanarray();
}
公共无效切换选择(int pos){
if(选择editems.get(pos,false)){
选择编辑项。删除(pos);
}否则{
选择editems.put(pos,true);
}
变更项目(pos);
}
public int getSelectedItemCount(){
返回selectedItems.size();
}
公共选举(){
选择editems.clear();
notifyDataSetChanged();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
公共视图持有者(最终视图项视图){
超级(项目视图);
//在此处初始化每行的项目
}
公共视图(内部位置){
试一试{
if(mCursor.isClosed())
返回;
mCursor.moveToPosition(位置);
//维护选中的项目列表,以便在单击或未单击该项目时可以有一个跟踪
if(checkedItems.contains(number)itemView.setBackgroundResource(R.drawable.background_选中);
else itemView.setBackgroundResource(R.drawable.background\u normal);
itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(checkedItems.contains(number)){
checkedItems.remove(编号);
}否则{
checkedItems.add(编号);
}
//获取应切换背景的项目的索引
我