-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseFragment.java
More file actions
159 lines (131 loc) · 4 KB
/
BaseFragment.java
File metadata and controls
159 lines (131 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.vtech.app.moudle;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.vtech.app.util.Logger;
import butterknife.ButterKnife;
/**
* Created by chengzj on 2017/6/17.
* <p>
* 实现懒加载的基类fragment
*/
public abstract class BaseFragment extends Fragment {
public String TAG = "";
private Context context;
/**
* 标志位:fragment是否可见
*/
private boolean isVisible;
/**
* 标志位:是否已加载fragment视图
*/
private boolean isPrepared = false;
/**
* 标志位:是否已进行懒加载数据,保证数据只加载一次
*/
private boolean isLoadData = false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
TAG = this.getClass().getSimpleName();
Logger.d(TAG, "setUserVisibleHint() -> isVisibleToUser: " + isVisibleToUser);
if (getUserVisibleHint()) {
isVisible = true;
onVisible();
} else {
isVisible = false;
onInvisible();
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
Logger.d(TAG, "onAttach");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Logger.d(TAG, "onCreateView");
View view = inflater.inflate(getLayoutId(), container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Logger.d(TAG, "onViewCreated");
initView(view, savedInstanceState);
isPrepared = true;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Logger.d(TAG, "onActivityCreated");
initData();
onVisible();//防止viewpager加载的第一个视图时,第一个视图提前执行setUserVisibleHint方法导致懒加载无效的问题
}
/**
* fragment可见时执行
*/
private void onVisible() {
Logger.d(TAG, "onVisible isPrepared:" + isPrepared + ",isVisible:" + isVisible + ",isLoadData:" + isLoadData);
if (!isPrepared || !isVisible || isLoadData) {
return;
}
isLoadData = true;
onLazyLoad();
}
/**
* fragment不可见时执行,可选方法一般不用
*/
protected void onInvisible(){}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Logger.d(TAG, "onActivityResult");
}
@Override
public void onStart() {
super.onStart();
Logger.d(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Logger.d(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Logger.d(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Logger.d(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
Logger.d(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Logger.d(TAG, "onDestroy");
}
protected abstract int getLayoutId();
protected abstract void initView(View view, Bundle savedInstanceState);
protected abstract void initData();
/**
* 懒加载核心方法
*/
protected abstract void onLazyLoad();
}