代码拉取完成,页面将自动刷新
这是一个可面向所有Android项目的支持库框架,提供一些基础、便捷的服务
Request可以用GsonRequest,如果需要自定义请求就需要自己去把服务器返回数据解析成对应的模型类,然后把Responces.Listener和ErrorListener设置成this,如果是RequestFragment则设置成getRequestActivity(),因为RequestFragment也是用RequestActivity代理请求.
一定要给RequestActiviy的mRequestHolderFragment赋值加载视图和出错视图等自动处理才会生效,mRequestHolderFragment可以作为一个独立的外部类方便重复利用,并且这个mRequestHolderFragment和RequestFragment是同处于RequestActivity中的。
onRequestStart和onRequestFailure在RequestActivity父类中作了处理,无需特别之处不需要再做处理,只需要在onRequestSuccess方法里处理数据。
public class TestRequestActivity extends RequestActivity<TestRequestActivity.TestModel>{
String url;
Map<String, Object> params;
@Override
public void onCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
parserIntent();
initData();
initViews();
startRequest();
}
@Override
public void parserIntent() {
super.parserIntent();
url = getIntent().getStringExtra("/...");
params.put(key, value)
}
@Override
public void initData() {
super.initData();
}
@Override
public void initViews() {
super.initViews();
//请求占位Fragment
mRequestHolderFragment = new AbstractRPFragment() {
@Override
public void initViews(View rootView) {
mLoadingFrame = rootView.findViewById(id)
mEmptyFrame = rootView.findViewById(id)
mErrorFrame = rootView.findViewById(id)
tvEmptyTips = rootView.findViewById(id)
tvErrorSceneTips = rootView.findViewById(id)
tvLoadTip = = rootView.findViewById(id)
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_list_item, container);
initViews(v);
return v;
}
};
}
@Override
public void onRequestStart(Request<TestModel> resquest) {
super.onRequestStart(resquest);
}
@Override
public void onRequestSuccess(TestModel result) {
super.onRequestSuccess(result);
}
@Override
public void onRequestFailure(VolleyError error) {
super.onRequestFailure(error);
}
private void startRequest() {
GsonRequest<TestModel> request = new GsonRequest<>(url, params, TestModel.class, this, this);
addRequest(request);
}
//请求返回结果的模型
public class TestModel{
}
}
**
*
* @ClassName: RequestHolderFragment
* @Description: 继承AbstractRPFragment,只需要继承实现initViews()方法和onCreateView,
* 只需要在子类加载布局和绑定控件
* @author: Bvin
* @date: 2015年2月2日 上午10:53:53
*/
public class RequestHolderFragment extends AbstractRPFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.univ_request_place_holder, container, false);
initViews(rootView);
return rootView;
}
@Override
public void initViews(View rootView) {
mLoadingFrame = rootView.findViewById(R.id.loading_frame);
mEmptyFrame = rootView.findViewById(R.id.empty_frame);
mErrorFrame = rootView.findViewById(R.id.error_frame);
//错误视图是可以去点击重新请求的
mErrorFrame.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getRequestActivity().reloadRequest();
}
});
tvLoadTip = (TextView) mLoadingFrame.findViewById(R.id.tvLoadTip);
tvEmptyTips = (TextView) mEmptyFrame.findViewById(R.id.tvEmptyTips);
tvErrorSceneTips = (TextView) mErrorFrame.findViewById(R.id.tvErrorSceneTips);
defaultLoadTips = ResourceFinder.findString(getActivity(), R.string.loading_tips);
defaultEmptyTips = ResourceFinder.findString(getActivity(), R.string.empty_tips);
defaultSceneError = ResourceFinder.findString(getActivity(), R.string.error_scene_tips);
defaultErrorSolution = ResourceFinder.findString(getActivity(), R.string.error_solution_click_retry);
}
}
##License
Copyright 2014-2015 Bvin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型