English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
1.Add loading more layout
1_Initialization and Hidden Code
Called in the RefreshListView constructor
private void initFooterView(Context context) { View footerView = View.inflate(context, R.layout.refresh_listview_footer, null); //Hidden Code footerView.measure(0, 0); int footerViewHeight = footerView.getMeasuredHeight(); footerView.setPadding(0, -footerViewHeight, 0, 0); this.addFooterView(footerView); }
2_layout_file_refresh_listview_footer.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <ProgressBar android:layout_margin="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/custom_progressbar" /> <TextView android:layout_marginLeft="10dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading more..." android:textColor="#ff0000" android:textSize="25sp" /> </LinearLayout>
2.When dragging to the bottom
/** * News page corresponding to the menu page * Total12个 * @author Administrator * */ public class TabMenuDetailPager extends MenuDetailBasePager implements OnPageChangeListener { /** * News Center-Data corresponding to the tag of the news menu */ private NewCenterTag newCenterTag; ..................... /** * URL for loading more data */ private String moreUrl; /** * Is loading more data */ protected boolean isLoadingMore = false; ................... @Override public View initView() { View view = View.inflate(mActivity, R.layout.tab_detail, null); //Inject View into XUtils framework ViewUtils.inject(this, view); .......................... //Set the pull-down refresh listener mListView.setOnRefreshListener(new OnRefreshListener() { @Override public void onPullDownRefresh() { isPullDownRefreshing = true; getDataFromNet(); } @Override public void onLoadingMore() { if(TextUtils.isEmpty(moreUrl)){ Toast.makeText(mActivity, "No more data available", 1).show(); mListView.onRefreshFinish(false); }else{ //There is more data, you need to load more data now getMoreDataFromNet(); } } }); return view; } /** * Load more data */ protected void getMoreDataFromNet() { HttpUtils httpUtils = new HttpUtils(); httpUtils.send(HttpMethod.GET, moreUrl, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { System.out.println("Successfully loaded more data:");+responseInfo.result); mListView.onRefreshFinish(false); isLoadingMore = true; processData(responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { mListView.onRefreshFinish(false); System.out.println("Failed to load more data:");+ msg); } }); } /** * Process and parse json data * @param json */ protected void processData(String json) { TabDetailBean bean = parserJson(json); if(!isLoadingMore){ System.out.println(bean.data.news.get(0).title); topnews = bean.data.topnews; //Set adapter for ViewPager TabDetailAdapter adapter = new TabDetailAdapter(); mViewPager.setAdapter(adapter); // Clear all Views ll_point_group.removeAllViews(); for(int i=0;i<topnews.size();i++{ View point = new View(mActivity); LayoutParams params = new LayoutParams(5, 5); point.setBackgroundResource(R.drawable.tab_detail_point_bg); if(i!=0){ params.leftMargin = 10; } point.setEnabled(false); point.setLayoutParams(params); ll_point_group.addView(point); } previousPointPosition = 0; //Set default image description and indicator points mtv_title_description.setText(topnews.get(previousPointPosition).title); ll_point_group.getChildAt(previousPointPosition).setEnabled(true); //Set the listener for page changes mViewPager.setOnPageChangeListener(this); //Set the adapter and corresponding data newsLists = bean.data.news; listViewAdapter = new ListViewAdapter(); mListView.setAdapter(listViewAdapter); // mListView.addHeaderView(v) ;//Add a view in the form of a head to the ListView }else{ //Extract the list of news, load it into the previous collection, and refresh the data isLoadingMore = false; List<News> moreDataNews = bean.data.news; newsLists.addAll(moreDataNews); listViewAdapter.notifyDataSetChanged();//Refresh data } } ................ /** * Parsing json using the Gson open-source project * @param json */ private TabDetailBean parserJson(String json) { Gson gson = new Gson(); TabDetailBean bean = gson.fromJson(json, TabDetailBean.class); moreUrl = bean.data.more; if(TextUtils.isEmpty(moreUrl)){ moreUrl = null; }else{ moreUrl = ConstantUtils.server_url+moreUrl; } return bean; } @Override public void onPageScrollStateChanged(int argument0) { // TODO Auto-generated method placeholder } @Override public void onPageScrolled(int argument0, float argument)1, int argument2) { // TODO Auto-generated method placeholder } ........................................................................... }
3.complete code
package com.atguigu.refreshlistview; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.AbsListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Date; /** * Purpose: Custom pull-down refresh ListView */ public class RefreshListview extends ListView { /** * Pull-down refresh and top scroll view */ private LinearLayout headerView; /** * Pull-down refresh control */ private View ll_pull_down_refresh; private ImageView iv_arrow; private ProgressBar pb_status; private TextView tv_status; private TextView tv_time; /** * The height of the pull-down refresh control */ private int pullDownRefreshHeight; /** * Pull down to refresh */ public static final int PULL_DOWN_REFRESH = 0; /** * Release to refresh */ public static final int RELEASE_REFRESH = 1; /** * Refreshing */ public static final int REFRESHING = 2; /** * Current status */ private int currentStatus = PULL_DOWN_REFRESH; private Animation upAnimation; private Animation downAnimation; /** * The control for loading more */ private View footerView; /** * The height of the load more control */ private int footerViewHeight; /** * Whether more has been loaded */ private boolean isLoadMore = false; /** * The part of the top scroll view */ private View topNewsView; /** * The Y-axis coordinate of ListView */ private int listViewOnScreenY = -1; public RefreshListview(Context context) { this(context, null); } public RefreshListview(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RefreshListview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initHeaderView(context); initAnimation(); initFooterView(context); } private void initFooterView(Context context) { footerView = View.inflate(context, R.layout.refresh_footer, null); footerView.measure(0, 0); footerViewHeight = footerView.getMeasuredHeight(); footerView.setPadding(0, -footerViewHeight, 0, 0); //Add footer to ListView addFooterView(footerView); //Listen to the scrolling of the ListView setOnScrollListener(new MyOnScrollListener()); } /** * Add top banner carousel * @param topNewsView */ public void addTopNewsView(View topNewsView) { if (topNewsView != null) { this.topNewsView = topNewsView; headerView.addView(topNewsView); } } class MyOnScrollListener implements OnScrollListener{ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { //when the list view is idle or flinging if (scrollState == OnScrollListener.SCROLL_STATE_IDLE || scrollState == OnScrollListener.SCROLL_STATE_FLING) { //and it is the last visible item if (getLastVisiblePosition() >= getCount())-1{ //1.show the load more layout footerView.setPadding(8,8,8,8); //2.status change isLoadMore = true; //3.callback interface if(mOnRefreshListener != null){ mOnRefreshListener.onLoadMore(); } } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } } private void initAnimation() { upAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); upAnimation.setDuration(500); upAnimation.setFillAfter(true); downAnimation = new RotateAnimation(-180, -360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); downAnimation.setDuration(500); downAnimation.setFillAfter(true); } private void initHeaderView(Context context) { headerView = (LinearLayout) View.inflate(context, R.layout.refresh_header, null); //Pull-down refresh control ll_pull_down_refresh = headerView.findViewById(R.id.ll_pull_down_refresh); iv_arrow = (ImageView) headerView.findViewById(R.id.iv_arrow); pb_status = (ProgressBar) headerView.findViewById(R.id.pb_status); tv_status = (TextView) headerView.findViewById(R.id.tv_status); tv_time = (TextView) headerView.findViewById(R.id.tv_time); //Measurement ll_pull_down_refresh.measure(0, 0); pullDownRefreshHeight = ll_pull_down_refresh.getMeasuredHeight(); //Default hide the pull-to-refresh control // View.setPadding(0,-control height, 0, 0);//fully hidden //View.setPadding(0, 0, 0, 0);//fully displayed ll_pull_down_refresh.setPadding(0, -pullDownRefreshHeight, 0, 0); //Add the ListView header addHeaderView(headerView); } private float startY = -1; @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: //1.record the starting coordinate startY = ev.getY(); break; case MotionEvent.ACTION_MOVE: if (startY == -1) { startY = ev.getY(); } //Determine if the top banner is fully displayed, only when it is fully displayed will there be pull-to-refresh boolean isDisplayTopNews = isDisplayTopNews(); if(!isDisplayTopNews){ //Load more break; } //If it is currently refreshing, do not allow it to refresh again if (currentStatus == REFRESHING) { break; } //2.arrive at the new coordinate float endY = ev.getY(); //3.record the sliding distance float distanceY = endY - startY; if (distanceY > 0) {//pull down //int paddingTop = -control height + distanceY; int paddingTop = (int) (-pullDownRefreshHeight + distanceY); if (paddingTop < 0 && currentStatus != PULL_DOWN_REFRESH) { //Pull down refresh status currentStatus = PULL_DOWN_REFRESH; //update status refreshViewState(); else if (paddingTop > 0 && currentStatus != RELEASE_REFRESH) { //Release to refresh status currentStatus = RELEASE_REFRESH; //update status refreshViewState(); } ll_pull_down_refresh.setPadding(0, paddingTop, 0, 0); //View.setPadding(0, paddingTop, 0, 0);//Dynamically display the pull-down refresh control } break; case MotionEvent.ACTION_UP: startY = -1; if (currentStatus == PULL_DOWN_REFRESH) { // View.setPadding(0,-control height, 0, 0);//fully hidden ll_pull_down_refresh.setPadding(0, -pullDownRefreshHeight, 0, 0); } else if (currentStatus == RELEASE_REFRESH) { //Set the status to refreshing currentStatus = REFRESHING; refreshViewState(); // View.setPadding(0, 0, 0, 0);//fully displayed ll_pull_down_refresh.setPadding(0, 0, 0, 0); //callback interface if (mOnRefreshListener != null) { mOnRefreshListener.onPullDownRefresh(); } } break; } return super.onTouchEvent(ev); } /** * Determine whether the top carousel is fully displayed * When the Y-axis coordinate of the ListView on the screen is less than or equal to the Y-axis coordinate of the top carousel, the top carousel is fully displayed * @return */ private boolean isDisplayTopNews() { if (topNewsView != null) { //1.get the coordinates of the ListView on the screen int[] location = new int[2; if (listViewOnScreenY === -1{ getLocationOnScreen(location); listViewOnScreenY = location[1; } //2.get the coordinates of the top carousel on the screen topNewsView.getLocationOnScreen(location); int topNewsViewOnScreenY = location[1; // if (listViewOnScreenY <= topNewsViewOnScreenY) { // return true; // }else{ // return false; // } return listViewOnScreenY <= topNewsViewOnScreenY; }else{ return true; } } private void refreshViewState() { switch (currentStatus) { case PULL_DOWN_REFRESH://Pull down refresh status iv_arrow.startAnimation(downAnimation); tv_status.setText("Pull down to refresh..."); break; case RELEASE_REFRESH://Release to refresh status iv_arrow.startAnimation(upAnimation); tv_status.setText("Release to refresh..."); break; case REFRESHING://Refreshing status tv_status.setText("Refreshing..."); pb_status.setVisibility(VISIBLE); iv_arrow.clearAnimation(); iv_arrow.setVisibility(GONE); break; } } /** * Callback this method when network connection is successful or fails * Restore user refresh status * * @param sucess */ public void onRefreshFinish(boolean sucess) { if(isLoadMore){ //Load more isLoadMore = false; //Hide the load more layout footerView.setPadding(0,-footerViewHeight,0,0); }else{ //Pull down to refresh tv_status.setText("Pull down to refresh..."); currentStatus = PULL_DOWN_REFRESH; iv_arrow.clearAnimation(); pb_status.setVisibility(GONE); iv_arrow.setVisibility(VISIBLE); //Hide the pull-down refresh control ll_pull_down_refresh.setPadding(0, -pullDownRefreshHeight, 0, 0); if (sucess) { //Set the latest update time tv_time.setText("Last update time:") + getSystemTime()); } } } /** * Get the current Android system time * * @return */ private String getSystemTime() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(new Date()); } /** * Listen to the refresh of the control */ public interface OnRefreshListener { /** * Callback this method when pulling down to refresh */ public void onPullDownRefresh(); /** Callback this method when loading more */ public void onLoadMore(); } private OnRefreshListener mOnRefreshListener; /** * Set the refresh listener, set by the outside world */ public void setOnRefreshListener(OnRefreshListener l) { this.mOnRefreshListener = l; } }
The above is what the editor introduces to everyone about the Android仿硅谷news pull-down refresh/Pull up to load more, hoping to be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Here we also thank everyone for their support of the Yelling tutorial website!
Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)