JS 与 trick 代码的运用及解析全攻略
580
2022-10-26
让Recyclerview可以结合新的Android Data Binding框架
Android Data Binding + RecyclerView
Using Recyclerview with the new Android Data Binding framework.
How to start?
Just clone this repository and start playing with it! If you want to use some parts of this repository in your project read below.
Change your gradle file
In your main build.gradle add:
classpath 'com.android.tools.build:gradle:1.5.0'
In your app build.gradle add:
dataBinding { enabled = true }
Modify your layout
Remember to use your classes and packages ;-).
Modify your activity
// your activity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); usersViewModel = new UsersViewModel(); usersViewModel.users.add(new SuperUserViewModel(new User("Android", "Dev"))); binding = DataBindingUtil.setContentView(this, R.layout.users_view); binding.setUsersViewModel(usersViewModel); binding.activityUsersRecycler.setLayoutManager(new LinearLayoutManager(this)); }
Modify your ViewModel class
public class UsersViewModel extends BaseObservable { public ObservableArrayList
Some details
Your ViewModel (UsersViewModel in my example) should have field of ObservableArrayList type which will be bind to recycler view.
Next thing is ItemViewBinder. This class is used in BindingRecyclerViewAdapter for creating ViewHolders and it's item views bindings. In my example I've created CompositeItemBinder in order to support two different item types with separate layouts. If you want to display list with one data type you can use ItemBinderBase:
public ItemBinder
Please look at UsersView.java and UsersViewModel.java if something is unclear.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~