刷新 recyclerView 从带有旋转器的房间填充

Refresh recyclerView filled from room with spinner case

当我 select 微调器中的某些项目时,我想在 RecyclerView 中刷新我的数据。 RecyclerView 放置在选项卡 activity 中的片段中,我的 RecyclerView 从房间中填充了实时数据。我为 RecyclerView 创建了观察者,但我不明白如何改变我的观察者或做其他事情。 可能是我需要重新创建此选项卡... 请告诉我我能做什么。

当我创建新的观察者时,我在 RecyclerView 中获得了新数据,但是当我与这些数据交互时,我看到我所有的旧观察者都在运行并对我的动作做出反应。

如何从数据库中查询另一个数据并将其传递给观察者或创建新的观察者并删除旧的?

我的片段

public class AllSitesFragment extends Fragment implements  AdapterView.OnItemSelectedListener {


@BindView(R.id.recycler_view)
RecyclerView recyclerView;

@BindView(R.id.tvEmptyMessage)
TextView tvEmptyMessage;

@BindView(R.id.spinner)
Spinner spinner;

private WebSitesViewModel mWebSitesViewModel;
private MyRecViewAdapter adapter;

public AllSitesFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_all_sites, container,  false);
    ButterKnife.bind(this, v);
    setHasOptionsMenu(true);

    mWebSitesViewModel = ViewModelProviders.of(this).get(WebSitesViewModel.class);

    adapter = new MyRecViewAdapter(getContext(), mWebSitesViewModel, AllSitesFragment.this );
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

    //Observer
    mWebSitesViewModel.getAllWebSites().observe(this, new Observer<List<WebSites>>() {
        @Override
        public void onChanged(@Nullable final List<WebSites> webSites) {
            adapter.setWebSites(webSites);


            if (adapter.getmWebSites().size() == 0) {
                tvEmptyMessage.setText(R.string.not_saved_sites);
                tvEmptyMessage.setVisibility(View.VISIBLE);
            } else {
                tvEmptyMessage.setVisibility(View.INVISIBLE);
            }
        }
    });

    spinner.setOnItemSelectedListener(this);


    mWebSitesViewModel.getAllWebSites().removeObservers(getActivity());
    return v;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (position) {
        case 0:

             // Refresh

            break;
        case 1:

             //Refresh

            break;

    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

您需要在 viewModel 中过滤 getAllWebSites() 函数的结果,因此您需要在该 viewModel 中使用 setter 来 set/update 您的过滤器。

您可以通过在 viewModel 中使用 MutableLiveData 变量来实现,该变量将用作过滤网站集合的参数。

这可以通过使用 switchMap 转换来完成。

它会是这样的(non-functional,根据你的需要调整它):

ViewModel

public class SitesViewModel extends AndroidViewModel {

    private final YourManager yourManager;
    private final LiveData<WebSites> webSites;
    private final MutableLiveData<YourItem> yourItemFilter;

    public SitesViewModel(Application application) {
        super(application);
        yourManager = new YourManager();
        yourItemFilter = new MutableLiveData<>();
        webSites = Transformations.switchMap(yourItemFilter, input -> yourManager.getAllWebSites(input));
    }

    public void setYourItemFilter(YourItem filter) {
        yourItemFilter.setValue(filter);
    }

    public LiveData<WebSites> getAllWebSites() {
        return webSites;
    }
}

然后在您的 activity 中简单地为所选项目设置过滤器值:

Activity

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (position) {
        case 0:
            YourItem item = (YourItem) parent.getItemAtPosition(position);
            mWebSitesViewModel.setYourItemFilter(item);
            break;
        case 1:
            ...
    }
}