Skip to content

Commit 358a16c

Browse files
committed
类似原生Material Style Head
1 parent 361c1db commit 358a16c

File tree

8 files changed

+86
-11
lines changed

8 files changed

+86
-11
lines changed

app/src/main/java/com/think/uiloader/ui/RMaterialHeadActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.widget.ListView;
1010
import android.widget.Toast;
1111

12+
import com.think.tlr.MaterialHeaderView;
1213
import com.think.tlr.TLRLinearLayout;
1314
import com.think.tlr.TLRUIHandlerAdapter;
1415
import com.think.uiloader.App;
@@ -29,6 +30,7 @@
2930
*/
3031
public class RMaterialHeadActivity extends AppCompatActivity implements ImageContract.View {
3132
private ListView mListView;
33+
private MaterialHeaderView mHeaderView;
3234
private TLRLinearLayout mTLRLinearLayout;
3335
private ListImageAdapter mAdapter;
3436
private List<ImageEntity.Image> mImageList = new ArrayList<>();
@@ -55,6 +57,8 @@ public void onRefreshStatusChanged(View target, TLRLinearLayout.RefreshStatus st
5557
}
5658
}
5759
});
60+
mHeaderView = (MaterialHeaderView) findViewById(R.id.head);
61+
mHeaderView.setTLRLinearLayout(mTLRLinearLayout);
5862
mAdapter = new ListImageAdapter();
5963
mListView.setAdapter(mAdapter);
6064
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

app/src/main/java/com/think/uiloader/ui/RMaterialHeadKeepContentActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.widget.ListView;
1010
import android.widget.Toast;
1111

12+
import com.think.tlr.MaterialHeaderView;
1213
import com.think.tlr.TLRLinearLayout;
1314
import com.think.tlr.TLRUIHandlerAdapter;
1415
import com.think.uiloader.App;
@@ -29,6 +30,7 @@
2930
*/
3031
public class RMaterialHeadKeepContentActivity extends AppCompatActivity implements ImageContract.View {
3132
private ListView mListView;
33+
private MaterialHeaderView mHeaderView;
3234
private TLRLinearLayout mTLRLinearLayout;
3335
private ListImageAdapter mAdapter;
3436
private List<ImageEntity.Image> mImageList = new ArrayList<>();
@@ -55,6 +57,8 @@ public void onRefreshStatusChanged(View target, TLRLinearLayout.RefreshStatus st
5557
}
5658
}
5759
});
60+
mHeaderView = (MaterialHeaderView) findViewById(R.id.head);
61+
mHeaderView.setTLRLinearLayout(mTLRLinearLayout);
5862
mAdapter = new ListImageAdapter();
5963
mListView.setAdapter(mAdapter);
6064
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

app/src/main/res/layout/activity_tlrmaterialhead.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
uiloader:enableRefresh="true"
99
uiloader:enableLoad="false"
1010
uiloader:keepHeadRefreshing="true"
11-
uiloader:closeAnimDuration="1000"
1211
uiloader:keepContentLayout="false">
1312

1413
<com.think.tlr.MaterialHeaderView
14+
android:id="@+id/head"
1515
android:layout_width="match_parent"
1616
android:layout_height="60dp"
1717
uiloader:label="head"/>

app/src/main/res/layout/activity_tlrmaterialhead_keepcontent.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
uiloader:keepContentLayout="true">
1313

1414
<com.think.tlr.MaterialHeaderView
15+
android:id="@+id/head"
1516
android:layout_width="match_parent"
1617
android:layout_height="60dp"
1718
uiloader:label="head"/>

library/src/main/java/com/think/tlr/MaterialHeaderView.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import android.util.AttributeSet;
88
import android.view.Gravity;
99
import android.view.View;
10-
import android.view.animation.DecelerateInterpolator;
10+
import android.view.animation.AccelerateInterpolator;
1111
import android.widget.LinearLayout;
1212

1313
/**
@@ -21,16 +21,19 @@ public class MaterialHeaderView extends LinearLayout implements TLRUIHandler {
2121

2222
private static final int CIRCLE_DIAMETER = 40;
2323

24-
private static final float DECELERATE_INTERPOLATION_FACTOR = 2f;
25-
26-
private DecelerateInterpolator mDecelerateInterpolator;
27-
2824
private int mMediumAnimationDuration;
2925

3026
private ValueAnimator mScaleAnimator;
3127

3228
private TLRLinearLayout mTLRLinearLayout;
3329

30+
private TLRUIHandlerHook mHook = new TLRUIHandlerHook() {
31+
@Override
32+
public void handlerHook() {
33+
34+
}
35+
};
36+
3437
public MaterialHeaderView(Context context) {
3538
this(context, null);
3639
}
@@ -46,16 +49,15 @@ public MaterialHeaderView(Context context, AttributeSet attrs, int defStyleAttr)
4649
setGravity(Gravity.CENTER);
4750

4851
mMediumAnimationDuration = getResources().getInteger(
49-
android.R.integer.config_mediumAnimTime);
50-
mDecelerateInterpolator = new DecelerateInterpolator(DECELERATE_INTERPOLATION_FACTOR);
52+
android.R.integer.config_shortAnimTime);
5153
createProgressView();
5254
initUpScaleAnimator();
5355
}
5456

5557
private void initUpScaleAnimator() {
5658
mScaleAnimator = ValueAnimator.ofFloat(1.0f, 0.0f);
5759
mScaleAnimator.setDuration(mMediumAnimationDuration);
58-
mScaleAnimator.setInterpolator(mDecelerateInterpolator);
60+
mScaleAnimator.setInterpolator(new AccelerateInterpolator());
5961
mScaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
6062
@Override
6163
public void onAnimationUpdate(ValueAnimator animation) {
@@ -68,6 +70,9 @@ public void onAnimationUpdate(ValueAnimator animation) {
6870
mScaleAnimator.addListener(new AnimatorListenerAdapter() {
6971
@Override
7072
public void onAnimationEnd(Animator animation) {
73+
if (mTLRLinearLayout != null) {
74+
mTLRLinearLayout.releaseHook(mHook);
75+
}
7176
mCircleView.setScaleX(1.0f);
7277
mCircleView.setScaleY(1.0f);
7378
invalidate();
@@ -87,9 +92,16 @@ private void createProgressView() {
8792
addView(mCircleView);
8893
}
8994

95+
public void setTLRLinearLayout(TLRLinearLayout TLRLinearLayout) {
96+
mTLRLinearLayout = TLRLinearLayout;
97+
}
98+
9099
@Override
91100
public void onRefreshStatusChanged(View target, TLRLinearLayout.RefreshStatus status) {
92101
if (status == TLRLinearLayout.RefreshStatus.REFRESHING) {
102+
if (mTLRLinearLayout != null) {
103+
mTLRLinearLayout.hook(mHook);
104+
}
93105
mProgress.setAlpha(255);
94106
mProgress.start();
95107
}

library/src/main/java/com/think/tlr/TLRCalculator.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import com.think.tlr.TLRLinearLayout.LoadStatus;
1616
import com.think.tlr.TLRLinearLayout.RefreshStatus;
1717

18+
import java.util.ArrayList;
19+
import java.util.List;
20+
1821
/**
1922
* @author borney
2023
* @date 4/28/17
@@ -68,6 +71,8 @@ class TLRCalculator {
6871
private TLRUIHandler mTLRUiHandler;
6972

7073
private ValueAnimator mAutoAnimator, mResetAnimator, mKeepAnimator;
74+
75+
private final List<TLRUIHandlerHook> mHooks = new ArrayList<>();
7176
/**
7277
* 刷新阀值(高度), 加载阀值
7378
*/
@@ -406,24 +411,49 @@ public void finishRefresh(boolean isSuccess, int errorCode) {
406411
mStatusController.setAutoRefreshing(false);
407412
}
408413
if (isKeepHeadRefreshing) {
409-
resetKeepView();
414+
hookKeepView();
410415
}
411416
mStatusController.finishRefresh();
412417
mTLRUiHandler.onFinish(tLRLinearLayout.getTouchView(), true, isSuccess, errorCode);
413418
}
414419

415420
public void finishLoad(boolean isSuccess, int errorCode) {
416421
if (isKeepFootLoading) {
417-
resetKeepView();
422+
hookKeepView();
418423
}
419424
mStatusController.finishLoad();
420425
mTLRUiHandler.onFinish(tLRLinearLayout.getTouchView(), false, isSuccess, errorCode);
421426
}
422427

428+
private void hookKeepView() {
429+
if (mHooks.size() != 0) {
430+
for (TLRUIHandlerHook hook : mHooks) {
431+
hook.handlerHook();
432+
}
433+
} else {
434+
resetKeepView();
435+
}
436+
}
437+
423438
private void resetKeepView() {
424439
startResetAnimator();
425440
}
426441

442+
public void hook(TLRUIHandlerHook hook) {
443+
if (hook != null) {
444+
mHooks.add(hook);
445+
}
446+
}
447+
448+
public void releaseHook(TLRUIHandlerHook hook) {
449+
if (hook != null) {
450+
mHooks.remove(hook);
451+
}
452+
if (mHooks.size() == 0) {
453+
resetKeepView();
454+
}
455+
}
456+
427457
public float getRefreshThreshold() {
428458
return mRefreshThreshold;
429459
}

library/src/main/java/com/think/tlr/TLRLinearLayout.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,22 @@ public void removeTLRUiHandler(TLRUIHandler handler) {
513513
mUiHandlerWrapper.removeTLRUiHandler(handler);
514514
}
515515

516+
/**
517+
*
518+
* @param hook
519+
*/
520+
public void hook(TLRUIHandlerHook hook) {
521+
mCalculator.hook(hook);
522+
}
523+
524+
/**
525+
*
526+
* @param hook
527+
*/
528+
public void releaseHook(TLRUIHandlerHook hook) {
529+
mCalculator.releaseHook(hook);
530+
}
531+
516532
/**
517533
* return load is enable or not
518534
*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.think.tlr;
2+
3+
/**
4+
* Created by borney on 5/12/17.
5+
*/
6+
public interface TLRUIHandlerHook {
7+
void handlerHook();
8+
}

0 commit comments

Comments
 (0)