为什么添加导航组件后RecylerView.notifyDataSetChanged()停止工作?

时间:2019-07-11 16:41:53

标签: android android-recyclerview

我从Android Jetpack实现了Navigation,然后在数据更改后,我的应用程序中的所有RecyclerViews停止更新。在调试模式下,我看到数据确实发生了更改,并且调用了notifyDataSetChanged(),但是RV实际上并未更新(未添加新项)。

我尝试使用其他方法,例如notifyItemInserted()等。试图通知外部适配器。没有任何帮助。

片段:

...
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.profnastil_fragment, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    ...
    ImageButton addRoofBtn = view.findViewById(R.id.roof_add_btn);
    addRoofBtn.setOnClickListener(v -> {
        ...
        Square roof = new Square(width, height);
        mViewModel.addRoof(roof);
    });
    mViewModel = ViewModelProviders.of(this).get(ProfnastilViewModel.class);
    mProfnastilAdapter = new SquaresAdapter(mViewModel, SquaresAdapter.SQUARE_TYPE_ROOF);
    RecyclerView roofRv = view.findViewById(R.id.roof_list);
    RecyclerView.LayoutManager wallsLayoutManager = new LinearLayoutManager(getContext());
    roofRv.setLayoutManager(wallsLayoutManager);
    roofRv.setHasFixedSize(true);
    roofRv.setAdapter(mProfnastilAdapter);
    mViewModel.getRoofs().observe(this, roofs -> mProfnastilAdapter.setSquares(roofs));
}

适配器:

...
public SquaresAdapter(SquareViewModelBase model, String squareType) {
    mModel = model;
    mSquares = new ArrayList<>();
    setHasStableIds(true);
    mSquareType = squareType;
}

public static class SquareHolder extends RecyclerView.ViewHolder {
    View view;
    TextView heightText;
    TextView widthText;
    TextView squareText;

    private SquareHolder(@NonNull View itemView) {
        super(itemView);
        view = itemView;
        heightText = itemView.findViewById(R.id.square_height);
        widthText = itemView.findViewById(R.id.square_width);
        squareText = itemView.findViewById(R.id.result_square);
    }
}

@Override
public int getItemCount() {
    if (mSquares == null) return 0;
    return mSquares.size();
}

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

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_square, parent, false);
    return new SquareHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
    final Square square = mSquares.get(position);
    SquareHolder vh = (SquareHolder) viewHolder;
    vh.heightText.setText(StringUtils.format(square.getHeight()));
    vh.widthText.setText(StringUtils.format(square.getWidth()));
    vh.squareText.setText(StringUtils.format(square.getSquare()));
    vh.view.findViewById(R.id.item_del_btn).setOnClickListener(v -> {
        mModel.removeSquare(square, mSquareType);
    });
}

public void setSquares(List<Square> newSquares) {
    mSquares.clear();
    mSquares.addAll(newSquares);
    notifyDataSetChanged();
}

ViewModel:

...
private MutableLiveData<List<Square>> mRoofs = new MutableLiveData<>();
public void addRoof(Square roof) {
    List<Square> roofs = mRoofs.getValue();
    if (roofs == null) roofs = new ArrayList<>();
    roofs.add(roof);
    mRoofs.setValue(roofs);
}

在实现导航图之前,一切正常。会发生什么以及如何使其起作用?

1 个答案:

答案 0 :(得分:0)

最后,在Fragment上添加view.requireLayout()对我有帮助:

mViewModel.getRoofs().observe(this, roofs -> {
    mProfnastilAdapter.setSquares(roofs));
    view.requireLayout();
};

但是我仍然不明白发生了什么,以及为什么它对我有帮助=) 如果有人向我解释,我将不胜感激。