Skip to content

Commit 76bdf35

Browse files
committed
增加reset keep view 的hook机制
1 parent 358a16c commit 76bdf35

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<activity android:name=".ui.RViewPagerActivity" android:label="@string/label_viewpage"/>
3535
<activity android:name=".ui.RScrollViewActivity" android:label="@string/label_scrollview"/>
3636
<activity android:name=".ui.RNotKeepHeadActivity" android:label="@string/label_notkeephead"/>
37+
<activity android:name=".ui.RKeepContentActivity" android:label="@string/label_keepcontent"/>
3738
<activity android:name=".ui.RCannotMoveHeadByTLRActivity" android:label="@string/label_cannot_move_head_by_tlr"/>
3839
<activity android:name=".ui.RAutoRefreshActivity" android:label="@string/label_autorefresh"/>
3940
<activity android:name=".ui.RRefreshMaxMoveDistanceActivity" android:label="@string/label_refresh_max_move_distance"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private List<ListItem<AppCompatActivity>> initList() {
5050
add(new ListItem(getString(R.string.label_scrollview), RScrollViewActivity.class));
5151
add(new ListItem(getString(R.string.label_otherlibrary), ROtherLibraryActivity.class));
5252
add(new ListItem(getString(R.string.label_notkeephead), RNotKeepHeadActivity.class));
53+
add(new ListItem(getString(R.string.label_keepcontent), RKeepContentActivity.class));
5354
add(new ListItem(getString(R.string.label_autorefresh), RAutoRefreshActivity.class));
5455
add(new ListItem(getString(R.string.label_cannot_move_head_by_tlr), RCannotMoveHeadByTLRActivity.class));
5556
add(new ListItem(getString(R.string.label_refresh_max_move_distance), RRefreshMaxMoveDistanceActivity.class));
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.think.uiloader.ui;
2+
3+
import android.graphics.Color;
4+
import android.os.Build;
5+
import android.os.Bundle;
6+
import android.os.Handler;
7+
import android.support.annotation.Nullable;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.view.View;
10+
import android.view.ViewTreeObserver;
11+
import android.widget.AdapterView;
12+
import android.widget.ListView;
13+
import android.widget.Toast;
14+
15+
import com.think.tlr.TLRLinearLayout;
16+
import com.think.tlr.TLRUIHandlerAdapter;
17+
import com.think.uiloader.App;
18+
import com.think.uiloader.R;
19+
import com.think.uiloader.data.entity.ImageEntity;
20+
import com.think.uiloader.ui.di.components.ActivityComponent;
21+
import com.think.uiloader.ui.di.components.DaggerActivityComponent;
22+
import com.think.uiloader.ui.mvp.contract.ImageContract;
23+
import com.think.uiloader.ui.mvp.presenter.ImagePresenter;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
import javax.inject.Inject;
29+
30+
/**
31+
* Created by borney on 4/28/17.
32+
*/
33+
public class RKeepContentActivity extends AppCompatActivity implements ImageContract.View {
34+
private ListView mListView;
35+
private TLRLinearLayout mTLRLinearLayout;
36+
private ListImageAdapter mAdapter;
37+
private List<ImageEntity.Image> mImageList = new ArrayList<>();
38+
private App mApp;
39+
private int curIndex = 0;
40+
private Handler mHandler = new Handler();
41+
42+
@Inject
43+
ImagePresenter mPresenter;
44+
45+
@Override
46+
protected void onCreate(@Nullable Bundle savedInstanceState) {
47+
super.onCreate(savedInstanceState);
48+
mApp = (App) getApplication();
49+
initActivityComponent();
50+
setContentView(R.layout.activity_tlrkeepcontent);
51+
mListView = (ListView) findViewById(R.id.content);
52+
mTLRLinearLayout = (TLRLinearLayout) findViewById(R.id.tlrlayout);
53+
mTLRLinearLayout.addTLRUiHandler(new TLRUIHandlerAdapter() {
54+
@Override
55+
public void onRefreshStatusChanged(View target, TLRLinearLayout.RefreshStatus status) {
56+
if (status == TLRLinearLayout.RefreshStatus.REFRESHING) {
57+
mPresenter.images(curIndex, 10);
58+
}
59+
}
60+
});
61+
mTLRLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
62+
@Override
63+
public void onGlobalLayout() {
64+
mTLRLinearLayout.getHeaderView().setBackgroundColor(Color.WHITE);
65+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
66+
mTLRLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
67+
} else {
68+
mTLRLinearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
69+
}
70+
}
71+
});
72+
mAdapter = new ListImageAdapter();
73+
mListView.setAdapter(mAdapter);
74+
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
75+
@Override
76+
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
77+
Toast.makeText(RKeepContentActivity.this, "onclick " + position, Toast.LENGTH_SHORT).show();
78+
}
79+
});
80+
}
81+
82+
private void initActivityComponent() {
83+
ActivityComponent component = DaggerActivityComponent.builder().applicationComponent(
84+
mApp.getApplicationComponent()).build();
85+
component.inject(this);
86+
mPresenter.setView(this);
87+
}
88+
89+
@Override
90+
protected void onResume() {
91+
super.onResume();
92+
}
93+
94+
@Override
95+
public void startImages() {
96+
97+
}
98+
99+
@Override
100+
public void imagesSuccess(final List<ImageEntity.Image> images) {
101+
if (images != null) {
102+
mHandler.postDelayed(new Runnable() {
103+
@Override
104+
public void run() {
105+
mImageList.addAll(0, images);
106+
curIndex += images.size();
107+
mAdapter.notifyImages(mImageList);
108+
mTLRLinearLayout.finishRefresh(true);
109+
}
110+
}, 1500);
111+
}
112+
}
113+
114+
@Override
115+
public void endImages() {
116+
117+
}
118+
119+
@Override
120+
public void error(int errorCode) {
121+
122+
}
123+
}

app/src/main/java/com/think/uiloader/ui/di/components/ActivityComponent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.think.uiloader.ui.RAutoRefreshActivity;
66
import com.think.uiloader.ui.RCannotMoveHeadByTLRActivity;
77
import com.think.uiloader.ui.RGridViewActivity;
8+
import com.think.uiloader.ui.RKeepContentActivity;
89
import com.think.uiloader.ui.RLListViewActivity;
910
import com.think.uiloader.ui.RListViewActivity;
1011
import com.think.uiloader.ui.RMaterialHeadActivity;
@@ -40,4 +41,5 @@ public interface ActivityComponent {
4041
void inject(RLListViewActivity activity);
4142
void inject(RMaterialHeadActivity activity);
4243
void inject(RMaterialHeadKeepContentActivity activity);
44+
void inject(RKeepContentActivity activity);
4345
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.think.tlr.TLRLinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:uiloader="http://schemas.android.com/apk/res-auto"
5+
android:id="@+id/tlrlayout"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
uiloader:enableRefresh="true"
9+
uiloader:enableLoad="false"
10+
uiloader:keepHeadRefreshing="true"
11+
uiloader:keepContentLayout="true"
12+
uiloader:refreshMaxMoveDistance="@dimen/tlr_def_head_height">
13+
14+
<ListView
15+
android:id="@+id/content"
16+
android:layout_width="match_parent"
17+
android:layout_height="match_parent"
18+
uiloader:label="content"/>
19+
20+
21+
</com.think.tlr.TLRLinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<string name="label_scrollview">下拉刷新-ScrollView</string>
1111
<string name="label_otherlibrary">下拉刷新-三方库兼容(Banner)</string>
1212
<string name="label_notkeephead">刷新时不保持头部(HeadView)</string>
13+
<string name="label_keepcontent">刷新时不移动内容(Content)体</string>
1314
<string name="label_cannot_move_head_by_tlr">下拉刷新过程中不移动头部(HeadView)</string>
1415
<string name="label_autorefresh">自动刷新</string>
1516
<string name="label_refresh_max_move_distance">刷新时,设置最大移动距离</string>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ public void removeTLRUiHandler(TLRUIHandler handler) {
514514
}
515515

516516
/**
517+
* add hook when ui ready to reset
517518
*
518519
* @param hook
519520
*/
@@ -522,6 +523,7 @@ public void hook(TLRUIHandlerHook hook) {
522523
}
523524

524525
/**
526+
* remove hook, must call if you set hook
525527
*
526528
* @param hook
527529
*/

0 commit comments

Comments
 (0)