This article describes the method of using ListView to implement pull-down refresh and pull-up display. Share it for your reference, as follows:
Today, I need to do listview + pull up and down to display progressdialog in the header and footer, but it does not affect user operations.
I have added comments directly to the code, so I can read it myself.
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * @author Stay * Dynamically load listview data, pull up, refresh, pull down More */ public class ListViewActivity extends Activity implements OnScrollListener { private static final int LOAD = 0; private static final int ERROR = 0; private static final int MEMBER = 1; private static final int LOADED = 2; private static final int DIALOG = 3; private static final int FULL = 4; private NearbyAdapter adapter; private ListView nearby_lv; private RelativeLayout nearby_lv_header; private Button list_bottom_btn; private LinearLayout list_bottom_linear; private TextView bottom_progress_text; private RelativeLayout nearby_lv_footer; private Button list_header_btn; private LinearLayout list_header_linear; private TextView heard_progress_text; private ArrayList<JSONObject> nearby_data = new ArrayList<JSONObject>(); private int lastItem; private HashMap<String, Drawable> imageCache; private myHandler; private ProgressDialog dialog; private int curPage = 1; private boolean isMember = false; private int firstItem; public int count; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); initView(); (LOAD); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { ("onScrollStateChanged"); // When the scrolling stops and the total number of scrolling is equal to the total number of data, go load if (lastItem == count && scrollState == SCROLL_STATE_IDLE) { ("onScrollStateChanged--------next"); if (curPage == 4 && !isMember) { (this, "You are not a formal member, please apply for a formal member."); list_bottom_linear.setVisibility(); } else { //Load data (LOAD); } return; } //Update the data when pulling up, clear the data and then reload it if (firstItem == 0 && scrollState == SCROLL_STATE_IDLE) { ("onScrollStateChanged--------refresh"); curPage = 0; (LOAD); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { ("firstVisibleItem=" + firstVisibleItem); ("visibleItemCount=" + visibleItemCount); ("totalItemCount=" + totalItemCount); //We need to subtract two here, because I added the header footer lastItem = firstVisibleItem + visibleItemCount - 2; firstItem = firstVisibleItem; } public int getData() { try { HttpURLConnection conn = (url//Written by yourself); ArrayList<JSONObject> temp = (()); if (curPage == 0 && nearby_data.size() > 0) { nearby_data.clear(); count = 0; } if (temp != null && () > 0) { count += (); nearby_data.addAll(temp); ("nearby_data.size()="+nearby_data.size()); } else { return FULL; } return LOADED; } catch (Exception e) { return ERROR; } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { (msg); switch () { case DIALOG: list_bottom_linear.setVisibility(); list_header_linear.setVisibility(); break; case LOADED: list_bottom_linear.setVisibility(); list_header_linear.setVisibility(); curPage++; (); break; case ERROR: ("error,missing data"); break; case MEMBER: ("you must regist formal member"); break; default: break; } } }; //Create child thread to load data and then update private class MyHandler extends Handler { private int status; public MyHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { synchronized (this) { switch () { case LOAD:// get data from server (DIALOG);//Show the waiting box status = getData(); (status, 1000); break; default: break; } } } } public void initView() { imageCache = new HashMap<String, Drawable>(); HandlerThread handlerThread = new HandlerThread("nearby"); // Before using the getLooper() method of HandlerThread, the start() of the class must be called; (); myHandler = new MyHandler(()); nearby_lv = (ListView) findViewById(.nearby_lv); nearby_lv_footer = (RelativeLayout) ().inflate(.nearby_lv_header, null); list_bottom_btn = (Button) nearby_lv_footer.findViewById(.list_bottom_btn); list_bottom_linear = (LinearLayout) nearby_lv_footer.findViewById(.list_bottom_linear); bottom_progress_text = (TextView) nearby_lv_footer.findViewById(.progress_text); nearby_lv_header = (RelativeLayout) ().inflate(.nearby_lv_header, null); list_header_btn = (Button) nearby_lv_header.findViewById(.list_bottom_btn); list_header_linear = (LinearLayout) nearby_lv_header.findViewById(.list_bottom_linear); heard_progress_text = (TextView) nearby_lv_header.findViewById(.progress_text); list_header_btn.setText("refresh"); list_bottom_btn.setText("More"); list_header_linear.setVisibility(); nearby_lv.addHeaderView(nearby_lv_header); nearby_lv.addFooterView(nearby_lv_footer); // list_header_btn.setOnClickListener(header_click); adapter = new NearbyAdapter(, nearby_data); nearby_lv.setAdapter(adapter); nearby_lv.setOnScrollListener(); } }
For more information about Android related content, please check out the topic of this site:Android Service Component Usage Tips Summary》、《Android programming activity operation skills summary》、《Android resource operation skills summary》、《Summary of Android data skills for operating json format》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.