Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
Widget
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Android Widget
Widget
Commits
f4d3a96e
Commit
f4d3a96e
authored
Nov 17, 2020
by
王雷
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增SingleLiveEvent
parent
6e0534da
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
1 deletion
+55
-1
build.gradle
tools/build.gradle
+1
-1
SingleLiveEvent.java
...rc/main/java/com/qimai/android/tools/SingleLiveEvent.java
+54
-0
No files found.
tools/build.gradle
View file @
f4d3a96e
...
@@ -42,7 +42,7 @@ repositories {
...
@@ -42,7 +42,7 @@ repositories {
group
'com.qmai.android.tools'
group
'com.qmai.android.tools'
version
'1.1.
8
-SNAPSHOT'
version
'1.1.
9
-SNAPSHOT'
gradlePublish
{
gradlePublish
{
...
...
tools/src/main/java/com/qimai/android/tools/SingleLiveEvent.java
0 → 100644
View file @
f4d3a96e
package
com
.
qimai
.
android
.
tools
;
import
android.util.Log
;
import
androidx.annotation.MainThread
;
import
androidx.annotation.NonNull
;
import
androidx.annotation.Nullable
;
import
androidx.lifecycle.LifecycleOwner
;
import
androidx.lifecycle.MutableLiveData
;
import
androidx.lifecycle.Observer
;
import
org.jetbrains.annotations.NotNull
;
import
java.util.concurrent.atomic.AtomicBoolean
;
public
class
SingleLiveEvent
<
T
>
extends
MutableLiveData
<
T
>
{
private
static
final
String
TAG
=
"SingleLiveEvent"
;
private
final
AtomicBoolean
mPending
=
new
AtomicBoolean
(
false
);
@Override
public
void
observe
(
@NonNull
@NotNull
LifecycleOwner
owner
,
@NonNull
@NotNull
final
Observer
<?
super
T
>
observer
)
{
if
(
hasActiveObservers
())
{
Log
.
w
(
TAG
,
"Multiple observers registered but only one will be notified of changes."
);
}
// Observe the internal MutableLiveData
super
.
observe
(
owner
,
new
Observer
<
T
>()
{
@Override
public
void
onChanged
(
@Nullable
T
t
)
{
if
(
mPending
.
compareAndSet
(
true
,
false
))
{
observer
.
onChanged
(
t
);
}
}
});
}
@MainThread
public
void
setValue
(
@Nullable
T
t
)
{
mPending
.
set
(
true
);
super
.
setValue
(
t
);
}
/**
* Used for cases where T is Void, to make calls cleaner.
*/
@MainThread
public
void
call
()
{
setValue
(
null
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment