检查在notifyDataSetChanged

时间:2017-12-14 18:35:37

标签: android android-recyclerview android-adapter android-checkbox

我在recyclerView适配器中有复选框onCheckedChanged。我更改了自定义类的onChecked方法。这是我的适配器来源

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<PackagesList> transactionList;
private Context mContext;
private int layoutID;
private static Typeface mediumTypeface, lightTypeface;
private int valueInPixels;
private boolean isLoading;


public interface OnLoadMoreListener {
    void onLoadMore();
}

public void setmOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
    this.mOnLoadMoreListener = mOnLoadMoreListener;
}

private OnLoadMoreListener mOnLoadMoreListener;


private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;

public void setCheckAllcheckbox(boolean checkAllcheckbox) {
    this.checkAllcheckbox = checkAllcheckbox;
}

public boolean isShowingCheckbox;
public boolean checkAllcheckbox;

public void setIsShowingCheckbox(boolean isShowingCheckbox) {
    this.isShowingCheckbox = isShowingCheckbox;
}

public boolean isShowingCheckbox() {
    return isShowingCheckbox;
}


public RecyclerAdapter(int layoutID, final List<PackagesList> transactions, Context context, NestedScrollView nestedScrollView) {

    this.layoutID = layoutID;
    this.mContext = context;
    this.transactionList = transactions;
    mediumTypeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Medium.ttf");
    valueInPixels = (int) mContext.getResources().getDimension(R.dimen.u_common_text_size_large);
    lightTypeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Light.ttf");


    if (nestedScrollView != null) {
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (v.getChildAt(v.getChildCount() - 1) != null) {
                    if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                            scrollY > oldScrollY) {
                        if (!isLoading) {

                            if (mOnLoadMoreListener != null) {

                                mOnLoadMoreListener.onLoadMore();
                            }
                            isLoading = true;

                        }
                    }
                }
            }
        });
    }

}

public void setLoaded() {
    isLoading = false;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == VIEW_TYPE_LOADING) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_loading_more, parent, false);
        return new LoadingViewHolder(v);
    } else if (viewType == VIEW_TYPE_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(layoutID, parent, false);
        return new TransactionHolder(v);
    }
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public int getItemViewType(int position) {
    return transactionList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof TransactionHolder) {
        final TransactionHolder transactionHolder = (TransactionHolder) holder;
        final PackagesList packagesList = transactionList.get(position);

        if (packagesList != null) {
            transactionHolder.title.setText(packagesList.getTitle());
            transactionHolder.tracking.setText("Tr. Code: " + packagesList.getTracking());
            transactionHolder.desc.setText(packagesList.getDesc());
            transactionHolder.date.setText(packagesList.getDate());
            transactionHolder.price.setText(packagesList.getPrice() + "$");

            String backgroundColor = packagesList.getColor().replace("0x", "#");


            transactionHolder.mainLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onclickListerner != null)
                        onclickListerner.onClickListener("enter", position);
                }
            });

            if (transactionHolder.swipeLayout != null) {
                transactionHolder.swipeLayout.setOnSwipeItemClickListener(new SwipeLayout.OnSwipeItemClickListener() {
                    @Override
                    public void onSwipeItemClick(boolean left, int index) {
                        if (left) {
                            if (onclickListerner != null)
                                onclickListerner.onClickListener("editAction", position);

                        } else {
                            if (onclickListerner != null)
                                onclickListerner.onClickListener("deleteAction", position);
                        }
                    }
                });


            }
            if (isShowingCheckbox) {
                if (!transactionHolder.appCompatCheckBox.isShown())
                    transactionHolder.appCompatCheckBox.setVisibility(View.VISIBLE);
                transactionHolder.circleImageView.setVisibility(View.GONE);
                transactionHolder.letterImageView.setVisibility(View.GONE);


                transactionHolder.appCompatCheckBox.setChecked(packagesList.isChecked());


                transactionHolder.appCompatCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {

                        if (packagesList.isChecked())
                            packagesList.setChecked(false);
                        else
                            packagesList.setChecked(true);


                    }
                });

                transactionHolder.appCompatCheckBox.setChecked(packagesList.isChecked());

            } else {
                transactionHolder.appCompatCheckBox.setVisibility(View.GONE);
                transactionHolder.appCompatCheckBox.setChecked(false);
                if (packagesList.getLogo().length() > 0) {
                    Picasso.with(mContext).load(packagesList.getLogo()).into(transactionHolder.circleImageView);
                    transactionHolder.letterImageView.setVisibility(View.INVISIBLE);
                    transactionHolder.circleImageView.setVisibility(View.VISIBLE);

                } else if (packagesList.getTitle().length() > 0) {

                    transactionHolder.letterImageView.setOval(true);
                    transactionHolder.letterImageView.setInitAllProperties(Color.parseColor("#ffffff"),
                            Color.parseColor(backgroundColor),
                            mediumTypeface, valueInPixels);
                    transactionHolder.letterImageView.setLetter(packagesList.getNickname());
                    transactionHolder.letterImageView.setVisibility(View.VISIBLE);
                    transactionHolder.circleImageView.setVisibility(View.INVISIBLE);


                } else {

                    Picasso.with(mContext).load(packagesList.getDefaultLogo()).into(transactionHolder.circleImageView);
                    transactionHolder.letterImageView.setVisibility(View.INVISIBLE);
                    transactionHolder.circleImageView.setVisibility(View.VISIBLE);
                }
            }

            transactionHolder.mainLayout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    setIsShowingCheckbox(true);
                    notifyDataSetChanged();

                    return true;
                }
            });


        }
    } else if (holder instanceof LoadingViewHolder) {
        LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
        loadingViewHolder.progressBar.setVisibility(View.VISIBLE);


    }

}


@Override
public int getItemCount() {
    return transactionList == null ? 0 : transactionList.size();

}

public PackagesList getItem(int position) {
    return transactionList.get(position);
}

public interface onclickListerner {
     void onClickListener(String type, int pos);

     void onLongClickListener(String type, int pos);

}

class LoadingViewHolder extends RecyclerView.ViewHolder {
    public MaterialProgressBar progressBar;

    public LoadingViewHolder(View itemView) {
        super(itemView);
        progressBar = itemView.findViewById(R.id.progressBar);
    }
}

public class TransactionHolder extends RecyclerView.ViewHolder {

    private TextView title;
    private TextView tracking;
    private TextView desc;
    private TextView price;
    private TextView date;
    private CircleImageView circleImageView;
    private LetterImageView letterImageView;
    private SwipeLayout swipeLayout;
    private LinearLayout mainLayout;

    private AppCompatCheckBox appCompatCheckBox;

    public TransactionHolder(final View view) {
        super(view);
        title = view.findViewById(R.id.u_title);
        tracking = view.findViewById(R.id.u_tracking);
        desc = view.findViewById(R.id.u_desc);
        price = view.findViewById(R.id.u_price);
        date = view.findViewById(R.id.u_date);
        letterImageView = view.findViewById(R.id.iv_avatar);

        appCompatCheckBox = view.findViewById(R.id.checkbox);

        swipeLayout = itemView.findViewById(R.id.swipe_layout);
        mainLayout = view.findViewById(R.id.mainLayout);

        circleImageView = view.findViewById(R.id.circle_image);
        title.setTypeface(mediumTypeface);
        tracking.setTypeface(mediumTypeface);
        price.setTypeface(mediumTypeface);
        desc.setTypeface(lightTypeface);
        date.setTypeface(mediumTypeface);

    }


}

private onclickListerner onclickListerner;

public void setOnclickListerner(onclickListerner onclickListerner) {
    this.onclickListerner = onclickListerner;
}
}

我在recyclelerView中有onLoad更多方法。在加载更多方法时,我在recyclerView中添加了新元素并调用了notifyDataSetChanged.After notifyDataSetChanged,onCheckedChanged自动调用,isChecked在复选框中为false。

这是我加载更多方法的代码段

 if (loadMore) {

                    Log.e("recyclerAdapter after",recyclerAdapter.isShowingCheckbox()+"");
                    if (serverStatus==4 &&!recyclerAdapter.isShowingCheckbox())
                        recyclerAdapter.setIsShowingCheckbox(true);
                    packagesLists.remove(packagesLists.size() - 1);
                    recyclerAdapter.notifyItemRemoved(packagesLists.size());

                    recyclerAdapter.setLoaded();
                    recyclerAdapter.notifyDataSetChanged();


                }

如果复选框已选中,则取消选中notifyDataSetChanged复选框状态。如何在notifyDataSetChanged之后重写我的代码以保存复选框状态? 感谢

1 个答案:

答案 0 :(得分:0)

在数组中存储已复选的复选框位置,然后在notifyDataSetChanged()获取数组中的位置后,使用setChecked()方法按位置选中复选框。

编辑: 您可以像这些

一样从适配器更新recylerview
public void updateData(ArrayList<ViewModel> viewModels) {
    items.clear();
    items.addAll(viewModels);
    notifyDataSetChanged();
}

public void removeItem(int position) {
    items.remove(position);
    notifyItemRemoved(position);
}

现在这些方法将在同一个适配器中,因此您可以立即查看checkboxex