Adapter 中的 Activity/Fragment 和 ViewModel 良好实践?

发布于 2025-01-16 11:15:18 字数 360 浏览 0 评论 0原文

假设我们有包含 RecyclerView 的 Activity/Fragment。此外,它还设置了一个Adapter。为了举例说明,假设适配器必须有权访问 Fragment 才能调用显示 Snackbar 的方法。此外,假设适配器中有几个项目。我想删除一个并将其从数据库中删除。因此我应该调用 ViewModel 的方法。我已经进行了研究,但我找不到任何信息是否将片段引用到适配器中是好是坏。

你能帮我解释一下吗?另外,对于 ViewModel,我在此处找到了一些想法。

但最佳实践是什么?

Let's assume that we have Activity/Fragment which contains a RecyclerView. Furthermore, it sets an Adapter. For the sake of the example, let's say the Adapter has to have access to Fragment in order to call a method which displays a Snackbar. Moreover, Let's say there are a couple of items in the adapter. I want to delete one and remove it from the database. Therefore I should call ViewModel's methods. I've made a research but I couldn't find any information if referencing a fragment into the Adapter is good or not.

Could you help me and explain? Also for the ViewModel I've found some ideas here.

But what are the best practices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2025-01-23 11:15:18
  1. 好的适配器类应该是静态的,帮助开发人员将其与 Activity/Fragment 部分分开
  2. 不要将 Activity/Fragment 引用保存在适配器中
  3. ViewModels 应该属于 Activity 或 Fragments
  4. 适配器应该通过回调/监听器或 LiveData

伪代码 执行 Activity/Fragment 的操作:

public class MainActivity extends Activity {
    
    private interface Listener {
        void OnRemoved(@NonNull xxx removedItem);
    }
    
    private static final class MyAdapter extends ArrayAdapter<xxx> {
        private final Listener mListener;
        private MyAdapter(@NonNull final Listener listener) {
            super(...);
            this.mListener = listener;
        }
        @Override
        public void remove(xxx item) {
            super.remove(xxx); //<-- this removes item from Adapter
            this.mListener.OnRemoved(item); //<-- this triggers Activity's code
        }
    }
    
    public void onCreate(...) {
        ...
        new MyAdapter(new Listener() {
            @Override
            public void OnRemoved(@NonNull final xxx removedItem) {
                Snakbar.makeText(....).show();
            }
        });
    }
}
  1. good Adapter Classes should be STATIC helping developers to keep it separated from Activity/Fragment part
  2. don't save Activity/Fragment reference inside Adapters
  3. ViewModels should belongs to Activities or Fragments
  4. Adapters should execute Activity/Fragment's actions via Callbacks/Listeners or LiveData

Pseudo-code:

public class MainActivity extends Activity {
    
    private interface Listener {
        void OnRemoved(@NonNull xxx removedItem);
    }
    
    private static final class MyAdapter extends ArrayAdapter<xxx> {
        private final Listener mListener;
        private MyAdapter(@NonNull final Listener listener) {
            super(...);
            this.mListener = listener;
        }
        @Override
        public void remove(xxx item) {
            super.remove(xxx); //<-- this removes item from Adapter
            this.mListener.OnRemoved(item); //<-- this triggers Activity's code
        }
    }
    
    public void onCreate(...) {
        ...
        new MyAdapter(new Listener() {
            @Override
            public void OnRemoved(@NonNull final xxx removedItem) {
                Snakbar.makeText(....).show();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文