带有 viewModel 和实时数据的 RecyclerView 适配器

我开始使用 android jetpack arch 组件并遇到了一些困惑。另请注意数据绑定不是一个选项。


我有一个具有 RecylcerView 的活动。我有一个如下所示的 ViewModel


public class Movie extends ViewModel {



public Movie movie;

public URL logoURL;


private MutableLiveData<Drawable> logo;


public MutableLiveData<Drawable> getLogo() {


    if (logo == null) {

        logo = new MutableLiveData<>();

    }

    return logo;

}



public PikTvChannelItemVM(Movie movie, URL logo) {


    this.movie = movie;

    this.logoURL = logoURL;



}


public Bitmap getChannelLogo() {


  //Do some network call to get the bitmap logo from the url

 }


}

以上都很好,尽管在我的回收站视图中有以下代码。尽管在 onbindviewholder 中,当我尝试从 viewmodels 实时数据中观察返回的图像时,它需要一个生命周期所有者参考,而我的回收者视图中没有。


HUX布斯
浏览 634回答 3
3回答

慕容森

您的适配器列表类型不应是视图模型。如果您想在没有绑定的情况下使用实时数据,您需要在列表更新时自动更改 ui。你可以这样做:首先,您的电影类必须是模型类,而不是视图模型。像往常一样做适配器。只需添加一个setList(Movie)&nbsp;方法。此方法应更新适配器列表。不要忘记在更新列表后通知适配器。然后创建 livedata 列表,如MutableLiveData<List<Movie>>&nbsp;movieLiveData&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;MutableLiveData<>();你想在哪里。在活动中观察这个列表,并在observe{}中调用适配器setList(Movie)方法。毕竟,如果您的列表更新,setList(Movie)将触发 medhod,然后您的 ui 将更新。

弑天下

我会尝试向适配器发送一个列表以显示它。我会观察适配器外部的数据,因为观察数据而不是显示数据不是适配器的责任。

湖上湖

你可以这样做:视图模型:class MyViewModel : ViewModel() {&nbsp; &nbsp; private val items = MutableLiveData<List<String>>()&nbsp; &nbsp; init {&nbsp; &nbsp; &nbsp; &nbsp; obtainList()&nbsp; &nbsp; }&nbsp; &nbsp; fun obtainList() { // obtain list from repository&nbsp; &nbsp; &nbsp; &nbsp; items.value = listOf("item1", "item2", "item3", "item4")&nbsp; &nbsp; }&nbsp; &nbsp; fun getItems(): LiveData<List<String>> {&nbsp; &nbsp; &nbsp; &nbsp; return items&nbsp; &nbsp; }}您的片段(或活动):public class ContentFragment extends Fragment {&nbsp; &nbsp; private MyViewModel viewModel;&nbsp; &nbsp; private RecyclerView recyclerView;&nbsp; &nbsp; private MyAdapter adapter;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; viewModel = new ViewModelProvider(this).get(MyViewModel.class);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View root = inflater.inflate(R.layout.fragment_main, container, false);&nbsp; &nbsp; &nbsp; &nbsp; recyclerView = root.findViewById(R.id.recycler_view);&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // add observer&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; viewModel.getItems().observe(this, items -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter = new MyAdapter(items); // add items to adapter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return root;&nbsp; &nbsp; }适配器:class MyAdapter(val list: List<String>) : RecyclerView.Adapter<MyAdapter.TextViewHolder {&nbsp; &nbsp; ...&nbsp; &nbsp; override fun onBindViewHolder(holder: TextViewHolder, position: Int) {&nbsp; &nbsp; &nbsp; &nbsp; holder.textView.text = list[position] // assign titles from the list.&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}可以使用任何自定义对象代替 String 对象。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java