[go: nahoru, domu]

Allow lazy state restoration driven by the Adapter

It is a common problem for layouts to lose scroll position due to a
configuraiton change when Adapter loads its data asyncronously.

So far, the only solution was to avoid setting the adapter on the
RecyclerView, which was not easy to implement + didn't allow adapter
to show any other items(like loading spinners).

This CL adds a state restoration mode to the adapter which has 3
options:

IMMEDIATE: Current behavior, also the default to avoid any issues;
RecyclerView restores state immediately.
WHEN_NOT_EMPTY: RecyclerView restores state as soon as Adapter has 1
or more items.
MANUAL: RecyclerView does not restore state until another call to
setStateRestorationMode is made with another value.

As state restoration happens in the LayoutManager, this change affects
how LayoutManager restores state. Now it receives the restored state
only when it can use it. Until then, state is kept in RecyclerView.

I've also changed SavedStateTests to add an index to each test
run so that it is easy to re-run a failing test.

Note: This is a re-implementation of a previously reverted Change
I35004de2f63dcb3428a6f5d8ca54d5c8cdfa0798 which broke a common use
case where devs called LayoutManager#onRestore multiple times.
That method is not intended to be called outside RV but as it is
public API, such behaviors are not surprising.
This version slightly changes how LayoutManager ignores saved state.
In the previous implementation, it had a long living flag which
made state restoration impossible after a scrollToPosition call.
This implementation instead ignores it if and only if there is a
pending scroll position.

I would still prefer the previous implementation as Views are not
designed to restore state multiple times yet there is existing
code that relies on that behavior and unfortunately LayoutManager#
onRestoreInstance state is public (due to previous API guidelines of
banning protected) so this solution finds a middle ground in between.

Bug:146365793
Bug:148104407
Test: LinearLayoutManagerSavedStateTest, SGLMSavedStateTest,
LazyStateRestorationTest, GridLayoutManagerSavedStateTest

Change-Id: I43050128fcd3842190a1bb3fa98ffd724844f0ef
14 files changed
tree: ae844eae92e4b8ed07e3ed38008fb90ffad777c6
  1. .idea/
  2. activity/
  3. ads/
  4. animation/
  5. annotation/
  6. api/
  7. appcompat/
  8. appsearch/
  9. arch/
  10. asynclayoutinflater/
  11. autofill/
  12. benchmark/
  13. biometric/
  14. browser/
  15. buildSrc/
  16. buildSrc-tests/
  17. busytown/
  18. camera/
  19. car/
  20. cardview/
  21. collection/
  22. compose/
  23. concurrent/
  24. contentaccess/
  25. contentpager/
  26. coordinatorlayout/
  27. core/
  28. cursoradapter/
  29. customview/
  30. development/
  31. docs-fake/
  32. docs-runner/
  33. documentfile/
  34. drawerlayout/
  35. dumb-tests/
  36. dynamic-animation/
  37. emoji/
  38. enterprise/
  39. exifinterface/
  40. fakeannotations/
  41. fragment/
  42. frameworks/
  43. gradle/
  44. gridlayout/
  45. heifwriter/
  46. inspection/
  47. interpolator/
  48. jetifier/
  49. leanback/
  50. leanback-preference/
  51. legacy/
  52. lifecycle/
  53. lint-demos/
  54. loader/
  55. localbroadcastmanager/
  56. media/
  57. media2/
  58. mediarouter/
  59. message-browser/
  60. mppsample/
  61. navigation/
  62. paging/
  63. palette/
  64. percentlayout/
  65. preference/
  66. print/
  67. recommendation/
  68. recyclerview/
  69. remotecallback/
  70. room/
  71. samples/
  72. savedstate/
  73. scripts/
  74. security/
  75. serialization/
  76. sharetarget/
  77. slices/
  78. slidingpanelayout/
  79. sqlite/
  80. startup/
  81. swiperefreshlayout/
  82. test/
  83. testutils/
  84. textclassifier/
  85. transition/
  86. tv-provider/
  87. ui/
  88. vectordrawable/
  89. versionedparcelable/
  90. viewpager/
  91. viewpager2/
  92. wear/
  93. webkit/
  94. work/
  95. .gitignore
  96. .mailmap
  97. build.gradle
  98. cleanBuild.sh
  99. code-review.md
  100. gradle.properties
  101. gradlew
  102. include-composite-deps.gradle
  103. include-support-library.gradle
  104. LICENSE.txt
  105. OWNERS
  106. PREUPLOAD.cfg
  107. README.md
  108. settings.gradle
  109. studiow
README.md

Android Jetpack

Jetpack is a suite of libraries, tools, and guidance to help developers write high-quality apps easier. These components help you follow best practices, free you from writing boilerplate code, and simplify complex tasks, so you can focus on the code you care about.

Jetpack comprises the androidx.* package libraries, unbundled from the platform APIs. This means that it offers backward compatibility and is updated more frequently than the Android platform, making sure you always have access to the latest and greatest versions of the Jetpack components.

Our official AARs and JARs binaries are distributed through Google Maven.

You can learn more about using it from Android Jetpack landing page.

Contribution Guide

Code Review Etiquette

When contributing to Jetpack, follow the code review etiquette.

Accepted Types of Contributions

  • Bug fixes - needs a corresponding bug report in the Android Issue Tracker
  • Each bug fix is expected to come with tests
  • Fixing spelling errors
  • Updating documentation
  • Adding new tests to the area that is not currently covered by tests
  • New features to existing libraries if the feature request bug has been approved by an AndroidX team member.

We are not currently accepting new modules.

Checking Out the Code

NOTE: You will need to use Linux or Mac OS. Building under Windows is not currently supported.

  1. Install repo (Repo is a tool that makes it easier to work with Git in the context of Android. For more information about Repo, see the Repo Command Reference)
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
  1. Configure Git with your real name and email address.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  1. Create a directory for your checkout (it can be any name)
mkdir androidx-master-dev
cd androidx-master-dev
  1. Use repo command to initialize the repository.
repo init -u https://android.googlesource.com/platform/manifest -b androidx-master-dev
  1. Now your repository is set to pull only what you need for building and running AndroidX libraries. Download the code (and grab a coffee while we pull down 6GB):
repo sync -j8 -c

You will use this command to sync your checkout in the future - it’s similar to git fetch

Using Android Studio

To open the project with the specific version of Android Studio recommended for developing:

cd path/to/checkout/frameworks/support/
./studiow

and accept the license agreement when prompted. Now you're ready edit, run, and test!

If you get “Unregistered VCS root detected” click “Add root” to enable git integration for Android Studio.

If you see any warnings (red underlines) run Build > Clean Project.

Builds

Full Build (Optional)

You can do most of your work from Android Studio, however you can also build the full AndroidX library from command line:

cd path/to/checkout/frameworks/support/
./gradlew createArchive

Testing modified AndroidX Libraries to in your App

You can build maven artifacts locally, and test them directly in your app:

./gradlew createArchive

And put the following at the top of your ‘repositories’ property in your project build.gradle file:

maven { url '/path/to/checkout/out/androidx/build/support_repo/' }

Continuous integration

Our continuous integration system builds all in progress (and potentially unstable) libraries as new changes are merged. You can manually download these AARs and JARs for your experimentation.

Running Tests

Single Test Class or Method

  1. Open the desired test file in Android Studio.
  2. Right-click on a test class or @Test method name and select Run FooBarTest

Full Test Package

  1. In the project side panel open the desired module.
  2. Find the directory with the tests
  3. Right-click on the directory and select Run androidx.foobar

Running Sample Apps

The AndroidX repository has a set of Android applications that exercise AndroidX code. These applications can be useful when you want to debug a real running application, or reproduce a problem interactively, before writing test code.

These applications are named either <libraryname>-integration-tests-testapp, or support-\*-demos (e.g. support-4v-demos or support-leanback-demos). You can run them by clicking Run > Run ... and choosing the desired application.

Password and Contributor Agreement before making a change

Before uploading your first contribution, you will need setup a password and agree to the contribution agreement:

Generate a HTTPS password: https://android-review.googlesource.com/new-password

Agree to the Google Contributor Licenses Agreement: https://android-review.googlesource.com/settings/new-agreement

Making a change

cd path/to/checkout/frameworks/support/
repo start my_branch_name .
# make needed modifications...
git commit -a
repo upload --current-branch .

If you see the following prompt, choose always:

Run hook scripts from https://android.googlesource.com/platform/manifest (yes/always/NO)?

If the upload succeeds, you'll see output like:

remote:
remote: New Changes:
remote:   https://android-review.googlesource.com/c/platform/frameworks/support/+/720062 Further README updates
remote:

To edit your change, use git commit --amend, and re-upload.

Getting reviewed

  • After you run repo upload, open r.android.com
  • Sign in into your account (or create one if you do not have one yet)
  • Add an appropriate reviewer (use git log to find who did most modifications on the file you are fixing or check the OWNERS file in the project's directory)

Handling binary dependencies

AndroidX uses git to store all the binary Gradle dependencies. They are stored in prebuilts/androidx/internal and prebuilts/androidx/external directories in your checkout. All the dependencies in these directories are also available from google(), jcenter(), or mavenCentral(). We store copies of these dependencies to have hermetic builds. You can pull in a new dependency using our importMaven tool.