English Document

VirtualLayout is a LayoutManager extension for RecyclerView, mainly provides a set of layout schemes and components between the layout reuse problems.

Design ideas

Through the customized LayoutManager, take over the whole RecyclerView layout logic; The LayoutManager manages a set of LayoutHelper, which is responsible for where the specific layout logic is implemented; Each LayoutHelper is responsible for the layout of components within a certain area of the page; Different LayoutHelper can do different layout logic, so you can provide heterogeneous layout structure in a RecyclerView page, which can provide richer capabilities than the system LinearLayoutManager, GridLayoutManager and so on. LayoutHelper is also supported to provide more layout capabilities.

use

Please refer to the latest version of MVN Repository (currently 1.0.1), the latest AAR will be published to JCenter and MavenCentral, make sure both repository sources are configured, and then introduce aar dependencies:

/ / gradle compile (' com. Alibaba. Android: vlayout: @ 1.0.1 aar ') {transitive = true}Copy the code

Or maven

// pom.xml in maven <dependency> <groupId>com.alibaba.android</groupId> <artifactId>vlayout</artifactId> < version > < / version 1.0.1 > < type > aar < type > < / dependency >Copy the code

Initialize the LayoutManager

final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
final VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);

recyclerView.setLayoutManager(layoutManager);
Copy the code

Set the size of recycling pool (if there are many views of the same type in one screen, an appropriate size should be set to prevent View creation when scrolling back and forth) :

RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
recyclerView.setRecycledViewPool(viewPool);
viewPool.setMaxRecycledViews(0, 10);
Copy the code

There are two ways to load data:

  • One is to useDelegateAdapter, can be written as usual to inherit fromDelegateAdapter.AdapterAdapter, requiring only more loads than the previous AdapteronCreateLayoutHelperMethods. The others are the same as the default Adapter.
DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, hasStableItemType); recycler.setAdapter(delegateAdapter); / / after setAdapters or add DelegateAdapter addAdapter method. The Adapter DelegateAdapter. SetAdapters (adapters); // or CustomAdapter adapter = new CustomAdapter(data, new GridLayoutHelper()); delegateAdapter.addAdapter(adapter);Copy the code
  • The other is when the business has custom complex requirements that can be inherited fromVirtualLayoutAdapterTo implement your own Adapter
public class MyAdapter extends VirtualLayoutAdapter { ...... } MyAdapter myAdapter = new MyAdapter(layoutManager); List< layoutHelper > helpers = new LinkedList<> helpers (); GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(4); gridLayoutHelper.setItemCount(25); helpers.add(gridLayoutHelper); GridLayoutHelper gridLayoutHelper2 = new GridLayoutHelper(2); gridLayoutHelper2.setItemCount(25); helpers.add(gridLayoutHelper2); / / pass layoutHelper list adapter myAdapter. SetLayoutHelpers (helpers); // Recycle recyclerView recycler.setAdapter(myAdapter);Copy the code

In this case, users need to pay attention to the need to actively call setLayoutHepers to update the layout mode when the structure of LayoutHelpers or the number of data and other elements of the layout change.

The layout properties

Each layoutHelper has its own layout properties to control layout styles, as described in the documentation.