In order to realize RecyclerView to display a variety of types of data, including all kinds of Headers and Footers, after referring to drakeet’s MultiType, focus on optimizing the display of one-to-many, that is, a data type corresponding to a variety of layout. News.class, for example, may display text, left, right, large, multigraph, and other item layouts. In this case, you need to distinguish by the type field in the data (the get() method that you specify for the attribute). Here, via the Java8 feature passing method reference, This problem is solved by executing this method to obtain the corresponding value of the data through KeyGenerator.
The library not only displays different layouts for the same data type, but also displays different layouts for different data types. If you have not registered your data type globally through MultiTypeRegistry or locally through MultiTypeAdapter, you can use this library to display different layouts for the same data type. The library will not crash the application and will return the default layout prompt. You can also register your own default data type. See the following for details.
MultiState
MultiState is a library that works better with MultiType in order to manage load, success, failure, and empty requests for data.
RecyclerViewWrapper
RecyclerViewWrapper this library combined with [MultiType] and [MultiState] and the use of SmartRefreshLayout this library to unified management of each loading state and simple packaging pull down refresh, pull up loading more related functions, welcome to download APK experience.
The Sample preview
Gradle configuration
// MultiType api 'com.sunfusheng:MultiType:<latest-version>' // MultiState api 'com.sunfusheng:MultiState:<latest-version>' // RecyclerViewWrapper compile 'com. Sunfusheng: RecyclerViewWrapper: < latest version - >' / / if you don't want to use the repository versions of RecyclerView, Can this configuration API 'com. Sunfusheng: RecyclerViewWrapper: < latest version - >', {exclude group: 'com.android.support'} // If the library is not synchronized, add it to build.gradle under the project: allprojects { repositories { maven { url 'https://dl.bintray.com/sfsheng0322/maven' } } }
Copy the code
The use of MultiType
Global registration
MultiTypeRegistry.getInstance().register(News.class, News::getType, News.TYPE_TEXT, new TextBinder());
MultiTypeRegistry.getInstance().register(News.class, News::getType, News.TYPE_BIG_IMAGE, new BigImageBinder());
MultiTypeRegistry.getInstance().register(News.class, News::getType, News.TYPE_RIGHT_IMAGE, new RightImageBinder());
MultiTypeRegistry.getInstance().register(News.class, News::getType, News.TYPE_THREE_IMAGES, new ThreeImagesBinder());
MultiTypeRegistry.getInstance().register(Music.class, new MusicBinder());
MultiTypeRegistry.getInstance().register(Video.class, new VideoBinder());Copy the code
Register default or unsupported types
MultiTypeRegistry.getInstance().registerDefaultBinder(new NonsupportBinder());Copy the code
Local registration, local registration will override the global
MultiTypeAdapter multiTypeAdapter = new MultiTypeAdapter();
multiTypeAdapter.register(News.class, News::getType, News.TYPE_TEXT, new TextBinder());
multiTypeAdapter.register(News.class, News::getType, News.TYPE_BIG_IMAGE, new BigImageBinder());
multiTypeAdapter.register(News.class, News::getType, News.TYPE_RIGHT_IMAGE, new RightImageBinder());
multiTypeAdapter.register(News.class, News::getType, News.TYPE_THREE_IMAGES, new ThreeImagesBinder());
multiTypeAdapter.register(Music.class, new MusicBinder());
multiTypeAdapter.register(Video.class, new VideoBinder());Copy the code
Initialize, set data
RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(multiTypeAdapter); multiTypeAdapter.setItems(@NonNull List<? > items); multiTypeAdapter.notifyDataSetChanged();Copy the code
The use of the MultiState
attribute | role |
---|---|
loadingLayout | Loading layout |
normalLayout | Display data layout normally |
errorLayout | Load failure layout |
emptyLayout | Data is empty layout |
loadingState | Initial loading state |
The XML configuration
<com.sunfusheng.multistate.MultiStateView
android:id="@+id/multiStateView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:loadingLayout="@layout/layout_loading"
app:normalLayout="@layout/layout_recyclerview"
app:errorLayout="@layout/layout_error"
app:emptyLayout="@layout/layout_empty"
app:loadingState="loading"/>Copy the code
Setting the loading state
multiStateView.setLoadingState(@LoadingState int loadingState);
Copy the code
APK download address, go and try it on your phone
Personal wechat official account
Reward is true love ^_^
About me
Personal email: [email protected]
Personal blog: sunfusheng.com
Jane books home page
Sina weibo