Back to project page RequestListView.
The source code is released under:
Apache License
If you think the Android project RequestListView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright (c) 2013, Yoson Hao ???? (haoyuexing@gmail.com). */*from w w w. j a va 2 s .c o m*/ * 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. */ package com.hao.requestlistview; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.widget.ImageView; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.androidquery.AQuery; import com.androidquery.callback.AjaxCallback; import com.androidquery.callback.AjaxStatus; import com.hao.requestlistviewlib.R; public class RequestListView extends ListView { private Context mContext; private LayoutInflater mInflater; private RelativeLayout mFooterView;// footer ?? private TextView mFooterTextView; // footer ?? textview private ImageView mFooterProgress; private int mFooterBackgroundResource; // footer ?? resource private int mFooterProgressDrawableResource;// footer ????? resource private String mFooterTextLoading = "Loading..."; // ????????? private String mFooterTextMore = "More..."; // ????????? public static final int TYPE_GET = 0; // get???? public static final int TYPE_POST = 1; // post???? public static final int ERROR_NETWORK = AjaxStatus.NETWORK_ERROR; public static final int ERROR_TRANSFORM = AjaxStatus.TRANSFORM_ERROR; public static final int ERROR_AUTH = AjaxStatus.AUTH_ERROR; private int mRequestType; // ???? private String mPageName = "page"; // ??????? private int mPageCount; // ?? private OnCompleteListener mOnCompleteListener; // ???????? private String mUrl; // ?????? private HashMap<String, String> mParams; // ?????? private static AQuery aq; // aq?? private static AQuery getAQueryInstance(Context context) { if (aq == null) { aq = new AQuery(context); return aq; } else { return aq; } } public RequestListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mContext = context; init(); } public RequestListView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; init(); } public RequestListView(Context context) { super(context); this.mContext = context; init(); } /** * * @Title: init * @Description: ??????? * @param * @return void * @throws */ private void init() { mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // ??footer?? mFooterView = (RelativeLayout) mInflater.inflate(R.layout.footer, this, false); // ?listview??footer this.addFooterView(mFooterView); // ??footer????? mFooterTextView = (TextView) mFooterView.findViewById(R.id.footer_text); // ??footer???????? mFooterProgress = (ImageView) mFooterView.findViewById(R.id.footer_progress); // ?footer??onclick???? mFooterView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // ???????????? mFooterTextView.setText(mFooterTextLoading); // ????????? mFooterView.setClickable(false); if (mFooterProgressDrawableResource != 0) { // ???????id??????? mFooterProgress.setVisibility(View.VISIBLE); mFooterProgress.startAnimation(getAnim()); } // ?? ajax(); } }); } /** * * @Title: resetPageCount * @Description: ??????? * @param * @return void * @throws */ public void resetPageCount() { mPageCount = 0; } /** * * @Title: setFooterHint * @Description: ??footer??????? * @param @param loading * @param @param more * @return void * @throws */ public void setFooterHint(String loading, String more) { this.mFooterTextLoading = loading; this.mFooterTextMore = more; } /** * * @Title: setFooterText * @Description: ??????footer??????? * @param @param hint * @return void * @throws */ public void setFooterText(String hint) { mFooterTextView.setText(hint); } /** * * @Title: setFooterAble * @Description: footer???????? * @param @param able * @return void * @throws */ public void setFooterAble(Boolean able) { if (!able) this.removeFooterView(mFooterView); } /** * * @Title: setRequestType * @Description: ?????? * @param @param type * @return void * @throws */ public void setRequestType(int type) { this.mRequestType = type; } /** * * @Title: setPageName * @Description: ????????????? * @param @param pageName * @return void * @throws */ public void setPageName(String pageName) { this.mPageName = pageName; } /** * * @Title: setFooterBackgroundResource * @Description: ??footer???resource * @param @param id * @return void * @throws */ public void setFooterBackgroundResource(int id) { this.mFooterBackgroundResource = id; } /** * * @Title: setFooterProgressDrawableResource * @Description: ??footer??????resource * @param @param id * @return void * @throws */ public void setFooterProgressDrawableResource(int id) { this.mFooterProgressDrawableResource = id; } /** * * @Title: showResult * @Description: ???????? * @param @param url * @return void * @throws */ public void showResult(String url) { this.mUrl = url; this.mParams = new HashMap<String, String>(); getException(); showFooterView(); ajax(); } /** * * @Title: showResult * @Description: ???????? * @param @param url * @param @param params * @return void * @throws */ public void showResult(String url, HashMap<String, String> params) { this.mUrl = url; this.mParams = params; getException(); showFooterView(); ajax(); } /** * * @Title: getException * @Description: ????????????? * @param * @return void * @throws */ private void getException() { if (mUrl == null || mUrl == "") { // ??url???????? throw new RuntimeException("Url is null or empty. ???Url?????"); } } /** * * @Title: showFooterView * @Description: ???????footer * @param * @return void * @throws */ @SuppressWarnings("deprecation") private void showFooterView() { if (mFooterProgressDrawableResource != 0) { // ???????id?????????? mFooterProgress.setVisibility(View.VISIBLE); Drawable drawable = getResources().getDrawable(mFooterProgressDrawableResource); mFooterProgress.setBackgroundDrawable(drawable); mFooterProgress.startAnimation(getAnim()); } if (mFooterBackgroundResource != 0) { // ??footer???id??????footer?? mFooterView.setBackgroundResource(mFooterBackgroundResource); } // ???????????? mFooterTextView.setText(mFooterTextLoading); } /** * * @Title: ajax * @Description: ????type?????????? * @param * @return void * @throws */ private void ajax() { switch (mRequestType) { case TYPE_GET: doGet(); break; case TYPE_POST: doPost(); break; } } /** * * @Title: doGet * @Description: get * @param * @return void * @throws */ private void doGet() { // ?????? String url = mUrl + "?" + mPageName + "=" + ++mPageCount; if (mParams.size() != 0) { Set<Map.Entry<String, String>> mapEntrySet = mParams.entrySet(); Iterator<Map.Entry<String, String>> mapEntryIterator = mapEntrySet.iterator(); while (mapEntryIterator.hasNext()) { Map.Entry<String, String> entry = mapEntryIterator.next(); url = url + "&" + entry.getKey() + "=" + entry.getValue(); } } // ?? getAQueryInstance(mContext).ajax(url, String.class, 15 * 60 * 1000, new AjaxCallback<String>() { @Override public void callback(String url, String str, AjaxStatus status) { if (str != null) { // ??? onComplete(true, str); } else { // ?? onComplete(false, status.getCode() + ""); } super.callback(url, str, status); } }); } /** * * @Title: doPost * @Description: post * @param * @return void * @throws */ private void doPost() { // ?????? mParams.put(mPageName, ++mPageCount + ""); // ?? getAQueryInstance(mContext).ajax(mUrl, mParams, String.class, new AjaxCallback<String>() { @Override public void callback(String url, String str, AjaxStatus status) { if (str != null) { // ??? onComplete(true, str); } else { // ?? onComplete(false, status.getCode() + ""); } super.callback(url, str, status); } }); } /** * * @Title: onComplete * @Description: ??????????? * @param @param isSuccess * @param @param res * @return void * @throws */ public void onComplete(boolean isSuccess, String res) { mFooterTextView.setText(mFooterTextMore); mFooterView.setClickable(true); mFooterProgress.setAnimation(null); mFooterProgress.setVisibility(View.GONE); if (mOnCompleteListener != null) { if (isSuccess) { mOnCompleteListener.onSuccess(res); } else { mPageCount = mPageCount - 1; mOnCompleteListener.onFail(Integer.valueOf(res)); } } } /** * * @Title: setOnCompleteListener * @Description: ?????? * @param @param l * @return void * @throws */ public void setOnCompleteListener(OnCompleteListener l) { this.mOnCompleteListener = l; } /** * * @Title: ReListView.java * @Package com.hao.requestlistview * @Description: ???????? * @author YosonHao * @E-mail haoyuexing@gmail.com * @date 2014-4-28 ????11:39:38 */ public interface OnCompleteListener { public void onSuccess(String str); public void onFail(int errorCode); } /** * * @Title: getAnim * @Description: 360????? * @param @param context * @param @param progressDrawable * @param @param progress * @param @return * @return RotateAnimation * @throws */ public RotateAnimation getAnim() { RotateAnimation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); LinearInterpolator lin = new LinearInterpolator(); anim.setInterpolator(lin); anim.setDuration(1000); anim.setRepeatCount(Animation.INFINITE); return anim; } }