BaseLoadFragment.java
5.72 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
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.cnlive.goldenline.ui.base;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.ui.widget.pull2refresh.PullToRefreshLayout;
import com.cnlive.goldenline.util.LogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit.Callback;
/**
* Created by zuoxian on 15/9/7.
*/
public abstract class BaseLoadFragment<T> extends BaseFragment implements Callback<T> {
private static final String TAG = "BaseLoadFragment";
protected T mPageData;
protected boolean isLoading;
protected boolean mIsPullToRefresh = false;
protected boolean enableLoadMore = false;
@Nullable
@BindView(R.id.empty_load)
protected LinearLayout mEmptyProgressbar;
@Nullable
@BindView(R.id.empty_button)
protected Button mEmptyButton;
@Nullable
@BindView(R.id.pull_to_refresh)
protected PullToRefreshLayout mPullToRefreshLayout;
@Nullable
@BindView(R.id.empty)
public RelativeLayout mEmptyView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LogUtils.LOGE(TAG + " onActivityCreated ");
if (mPageData == null) {
mIsPullToRefresh = false;
onLoadData();
} else {
onInitLoadData(mPageData);
}
// if (mPullToRefreshLayout != null) {
// LogUtils.LOGE(TAG + " mPullToRefreshLayout = " + mPullToRefreshLayout);
//// if (!enableLoadMore) {
//// LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(
//// Context.LAYOUT_INFLATER_SERVICE);
//// mPullToRefreshLayout.setCustomLoadmoreView(inflater.inflate(R.layout.empty_view, null));
//// }
// mPullToRefreshLayout.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
// @Override
// public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
// LogUtils.LOGE(TAG + " onRefresh ");
// mIsPullToRefresh = true;
// onLoadData();
// }
//
// @Override
// public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
// LogUtils.LOGE(TAG + " onLoadMore ");
// mPullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
// onLoadData();
// }
// });
// }
}
protected abstract void onLoadData();
protected abstract void onInitLoadData(T pageData);
// protected abstract void onLoadMoreData();
protected void setPageData(T page_data) {
this.mPageData = page_data;
onInitLoadData(page_data);
}
protected void showLoad() {
if (getAttchActivity() == null) return;
if (mPageData != null) return;
if (mEmptyView != null) mEmptyView.setVisibility(View.VISIBLE);
if (mEmptyProgressbar != null) mEmptyProgressbar.setVisibility(View.VISIBLE);
if (mEmptyButton != null) mEmptyButton.setVisibility(View.GONE);
}
protected void showConnectionRetry() {
if (getAttchActivity() == null) return;
if (mPageData != null) return;
if (mEmptyView != null) mEmptyView.setVisibility(View.VISIBLE);
if (mEmptyProgressbar != null) mEmptyProgressbar.setVisibility(View.GONE);
if (mEmptyButton != null) {
mEmptyButton.setText(getAttchActivity().getString(R.string.load_retry));
mEmptyButton.setVisibility(View.VISIBLE);
mEmptyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIsPullToRefresh = false;
showLoad();
onLoadData();
}
});
}
}
protected void hideEmptyView() {
if (mEmptyView != null)
mEmptyView.setVisibility(View.GONE);
}
protected void onRefreshCallback(boolean succeed) {
// LogUtils.LOGE(TAG + " onRefreshCallback succeed = " + succeed + " mEmptyView = " + mEmptyView + " mPullToRefreshLayout = " + mPullToRefreshLayout);
if (succeed && mEmptyView != null) mEmptyView.setVisibility(View.GONE);
if (mIsPullToRefresh && mPullToRefreshLayout != null) {
if (succeed) {
mPullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
} else {
mPullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
}
}
}
protected void onLoadMoreCallback(boolean succeed) {
if (mPullToRefreshLayout != null) {
if (succeed) {
mPullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
} else {
mPullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
}
}
}
public void setEnableRefresh() {
if (mPullToRefreshLayout != null)
mPullToRefreshLayout.setPullDownEnable(false);
}
public void setEnableLoadMore(boolean flag) {
enableLoadMore = flag;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
View rootView = super.onCreateView(inflater, container, savedInstanceState);
ButterKnife.bind(this, rootView);
return rootView;
}
}