SoFunction
Updated on 2025-03-01

ListView implements pull-down refresh to load more instance code (use it directly)

ListView Api bixu Take a look

();//Refresh ListView

Custom RefreashListView

package .dell_pc.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .dell_pc.;
import ;
import ;
/**
 * Author: Brother Xiaoguang on 2016/7/7.
 * Email: 1445037803@
 * Revised history:
 * describe:
 * ┏┓ ┏┓
 *  ┏┛┻━━━┛┻┓━━━━━┻┓
 *  ┃ ┃
 *  ┃ ━ ┃
 *  ┃ > < ┃
 *  ┃ ┃
 *  ┃... ⌒ ... ┃
 *  ┃ ┃
 *  ┗━┓ ┏━┛
 *  ┃ ┃Code is far away from bug with the animal protecting
 *  ┃ Shi ┃ Blessed by the divine beast, no bugs in the code
 *  ┃ Poetry ┃
 *  ┃  ┃
 *  ┃ Pet
 *  ┃ ┃
 *  ┃ ┗━━━┓
 *  ┃BUG natural enemy ┣┓┣┓┣┓┣┓┣┓┣┓┣┓
 *  ┃ ┏┛
 *  ┗┓┓┏━┳┓┏┛
 * ┃┫┫ ┫┫┫┫
 * ┗┻┛┛┗┻┛
 */
public class RefreshListView extends ListView implements  {
private static final int STATE_PULL_REFRESH = 0;// Pull down to refreshprivate static final int STATE_RELEASE_REFRESH = 1;// Release refreshprivate static final int STATE_REFRESHING = 2;// Refreshingprivate int mCurrrentState = STATE_PULL_REFRESH;// Current statusprivate View mHeaderView;
private TextView tvTitle;
private TextView tvTime;
private ProgressBar pbProgress;
private ImageView ivArrow;
private RotateAnimation animUp;
private RotateAnimation animDown;
private int startY = -1;// The y-coordinate of the sliding starting pointprivate int endY;
private int measuredHeight;
private int mFooterViewHeight;
private View footerView;
public RefreshListView(Context context) {
super(context);
initHeaderView();
initFooterView();
}
public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderView();
initFooterView();
}
public RefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initHeaderView();
initFooterView();
}
/**
 * Initialize header layout
 */
private void initHeaderView() {
mHeaderView = (getContext(), .refeeash_header, null);
(mHeaderView);
tvTitle = (TextView) (.tv_title);
tvTime = (TextView) (.tv_time);
pbProgress = (ProgressBar) (.pb_progress);
ivArrow = (ImageView) (.iv_arr);
(0, 0);//Measure it first and then get its heightmeasuredHeight = ();
(0, -measuredHeight, 0, 0);
initArrowAnim();
("Last refresh time:" + getCurrentTime());
}
/**
 * Initialize foot layout
 */
public void initFooterView() {
footerView = (getContext(), .refreash_listview_footer, null);
(footerView);
(0, 0);
mFooterViewHeight = ();
(0, -mFooterViewHeight, 0, 0);//Default hidden foot layout(this);
}
private boolean isLoadingMOre;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_FLING || scrollState == SCROLL_STATE_IDLE) {
if (getLastVisiblePosition() == getCount() - 1 && !isLoadingMOre) {//Slide to the end("The end...");
(0, 0, 0, 0);
setSelection(getCount() - 1);//Change the display position of ListViewisLoadingMOre = true;
if (mListener != null) {
();
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (()) {
case MotionEvent.ACTION_DOWN:
startY = (int) ();
break;
case MotionEvent.ACTION_MOVE:
if (startY == -1) {//Sometimes the MotionEvent.ACTION_DOWN event will not be responded, this is to re-get the startY coordinatestartY = (int) ();
}
//When refreshing, the loop jumps out and the following logic is no longer executedif (mCurrrentState == STATE_RELEASE_REFRESH) {
break;
}
endY = (int) ();
int dy = endY - startY;//Calculate the sliding distance of the fingerif (dy > 0 && getFirstVisiblePosition() == 0) {// Pull-down is allowed only if pull-down and is currently the first itemint padding = dy - measuredHeight;// Calculate padding(0, padding, 0, 0);//Set the current paddingif (padding > 0 && mCurrrentState != STATE_RELEASE_REFRESH) {
mCurrrentState = STATE_RELEASE_REFRESH;
refreshState();
} else if (padding < 0 && mCurrrentState != STATE_PULL_REFRESH) {// Change to pull down to refresh the statusmCurrrentState = STATE_PULL_REFRESH;
refreshState();
}
return true;
}
break;
case MotionEvent.ACTION_UP:
startY = -1;//Reset your fingers// When the state is to release refresh, my finger is lifting, refreshingif (mCurrrentState == STATE_RELEASE_REFRESH) {
mCurrrentState = STATE_REFRESHING;// Refreshing(0, 0, 0, 0);// showrefreshState();
} else if (mCurrrentState == STATE_PULL_REFRESH) {
(0, -measuredHeight, 0, 0);// Hide}
break;
default:
break;
}
return (ev);
}
private String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = (new Date());
return currentTime;
}
private void initArrowAnim() {
//Initialize arrow animationanimUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
(500);
(true);// Stay true//Arrow down animationanimDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
(500);
(true);
}
/**
 * Refresh the layout of the drop-down control
 */
private void refreshState() {
switch (mCurrrentState) {
case STATE_PULL_REFRESH:
("Drive down to refresh");
();
();
(animDown);
break;
case STATE_RELEASE_REFRESH:
("Release Refresh");
();
();
(animUp);
break;
case STATE_REFRESHING:
("Refreshing...");
();// The animation must be cleared before it can be hidden();
();
if (mListener != null) {
();
}
break;
default:
break;
}
}
OnRefreashListener mListener;
public void setOnRefreashListener(OnRefreashListener listener) {
mListener = listener;
}
public interface OnRefreashListener {
void onRefreash();
void onLoadMore();
}
/**
 * Collapse the control to refresh
 */
public void onRefreashComplete() {
if (isLoadingMOre) {
(0, -mFooterViewHeight, 0, 0);//Hidden foot layoutisLoadingMOre = false;
} else {
mCurrrentState = STATE_PULL_REFRESH;
("Drive down to refresh");
();
();
(0, -measuredHeight, 0, 0);//hide}
}
public void noFooterView() {
(0, mFooterViewHeight, 0, 0);//Hidden foot layout}
}

RefreashListView reference in code

package .dell_pc.;
import ;
import .;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .dell_pc.;
import .dell_pc.;
import .dell_pc.;
import .dell_pc.;
import .dell_pc.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
 * Author: Brother Xiao Guang on 2016/4/22.
 * Email: 1445037803@
 * Revised history:
 * describe:
 * ┏┓ ┏┓
 *  ┏┛┻━━━┛┻┓━━━━━┻┓
 *  ┃ ┃
 *  ┃ ━ ┃
 *  ┃ > < ┃
 *  ┃ ┃
 *  ┃... ⌒ ... ┃
 *  ┃ ┃
 *  ┗━┓ ┏━┛
 *  ┃ ┃Code is far away from bug with the animal protecting
 *  ┃ Shi ┃ Blessed by the divine beast, no bugs in the code
 *  ┃ Poetry ┃
 *  ┃  ┃
 *  ┃ Pet
 *  ┃ ┃
 *  ┃ ┗━━━┓
 *  ┃BUG natural enemy ┣┓┣┓┣┓┣┓┣┓┣┓┣┓
 *  ┃ ┏┛
 *  ┗┓┓┏━┳┓┏┛
 * ┃┫┫ ┫┫┫┫
 * ┗┻┛┛┗┻┛
 */
/**
 * Sign the details page
 */
public class TabDetailPager extends BaseMenuDetailPager implements  {
/**
* @param activity
* @param newsTabData
*/
 mTabdata;
private String mUrl;
private TabData mTabDetailData;
@ViewInject(.vp_news)
private ViewPager mViewPager;
@ViewInject(.lv_newslist)
private RefreshListView newsListView;//News List@ViewInject(.tv_title)
private TextView tvTitle;//News title@ViewInject()
private CirclePageIndicator indicator;//Top news indicatorprivate ArrayList<> mTopNewsList;//News Data Collectionprivate int[] images;
private ArrayList<> mNewsList;//News Data Collectionprivate String mMoreUrl;
private NewsAdapter mNewsAdapter;
public TabDetailPager(Activity activity,  newsTabData) {
super(activity);
mTabdata = newsTabData;
mUrl = GlobeContents.SERVER_URL + ;
}
@Override
public View initViews() {
images = new int[]{.topnews1, .topnews21, .topnews31, };
View view = (mActivity, .tab_detail_pager, null);
View headerView = (mActivity, .list_header_topnews, null);//Loading header layout// TextView tvTitle = (TextView) (.tv_title);
// Event Injection(this, view);
(this, headerView);
(this);
//Load fat headlines into listView in header layout form(headerView);
//Set pull-down refresh monitor(new () {
@Override
public void onRefreash() {
getDataFromServer();
}
@Override
public void onLoadMore() {
if (mMoreUrl != null) {
getMoreDataFromServer();
} else {
(mActivity, "No more data", Toast.LENGTH_SHORT).show();
();
// ();
}
}
});
return view;
}
@Override
public void initData() {
getDataFromServer();//This is an asynchronous,}
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
(, mUrl, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = (String) ;
// ("Tab details page returns the result:++++++++++++++++++++++" + result);praseData(result, false);
();
}
@Override
public void onFailure(HttpException e, String s) {
(mActivity, "Failed to obtain Tabdetaildata!", Toast.LENGTH_SHORT).show();
// ();
}
});
}
/**
 * Load the next page of data
 */
private void getMoreDataFromServer() {
HttpUtils utils = new HttpUtils();
(, mMoreUrl, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = (String) ;
// ("Tab details page returns the result:++++++++++++++++++++++" + result);praseData(result, true);
();
}
@Override
public void onFailure(HttpException e, String s) {
(mActivity, "Failed to obtain Tabdetaildata!", Toast.LENGTH_SHORT).show();
// ();
}
});
}
/**
 * Parsing json string into json object
 *
 * @param result json string to parse
 */
private void praseData(String result, boolean isLoadMore) {
Gson gson = new Gson();
mTabDetailData = (result, );//Return the TabData object("Tab Details Page:++++++++++++++++++++++++++++++" + mTabdata);
//Dispose of more page logicfinal String more = ;
if (!(more)) {
mMoreUrl = GlobeContents.SERVER_URL + more;
} else {
mMoreUrl = null;
}
if (!isLoadMore) {
mTopNewsList = ;
mNewsList = ;
if (mTopNewsList != null) {
(new TopNewsAdapter());
(this);
(mViewPager);//After setting the adapter, set the indicator(true);//Set the snapshot display(0);//Indicator will be smart and will record the current location when leaving((0).title);
}
mNewsAdapter = new NewsAdapter();
//Fill in the news listif (mNewsList != null) {
(mNewsAdapter);
}
} else {//If it is loading the next page, you need to append the data to the original collectionArrayList<> news = ;
(news);
();//Refresh ListView}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {//Update headlines topNewsData = (position);
();
}
@Override
public void onPageScrollStateChanged(int state) {
}
/**
 * ListView adapter
 */
class NewsAdapter extends BaseAdapter {
private final BitmapUtils utils;
NewsAdapter() {
utils = new BitmapUtils(mActivity);
(.pic_item_list_default);
}
@Override
public int getCount() {
return ();
}
@Override
public Object getItem(int position) {
return (position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = (mActivity, .list_news_item, null);
holder = new ViewHolder();
 = (ImageView) (.iv_pic);
 = (TextView) (.tv_title);
 = (TextView) (.tv_date);
(holder);
} else {
holder = (ViewHolder) ();
}
 item = () getItem(position);
();
();
(, );
return convertView;
}
}
static class ViewHolder {
public TextView tvTitle;
public TextView tvDate;
public ImageView ivPic;
}
/**
 * Viewpager adapter
 */
class TopNewsAdapter extends PagerAdapter {
private BitmapUtils bitmapUtils;
TopNewsAdapter() {
bitmapUtils = new BitmapUtils(mActivity);
(.topnews_item_default);//Set default pictures}
@Override
public int getCount() {
return ();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(mActivity);
(images[position]);
(.FIT_XY);//Fill the picture based on the control size//  topNewsData = (position);
// (imageView,(position).url);
(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((View) object);
}
}
}

The above is the full description of ListView that I introduced to you to implement pull-down refresh and load more example codes (used directly). I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!