android commonly used listview, scrollview and other pull-up refresh

code directly.

In fact, the main thing is to judge whether the listview has reached the last one in the onScrollStateChanged method by inheriting the OnScrollListener interface.

Then load the data, the following part of the code.

public class MainActivity extends Activity implements OnScrollListener {
	private ArrayAdapter   adapter;
	private ListView listView;
    private TextView loadText;
    private ProgressBar pg;
    private List<String> list;
   
    // ListView bottom View
    private View moreView;
    private Handler handler = new Handler();
    
    private int MaxNum = 22;// Set a maximum number of data, if it exceeds, it will not be loaded
    // index of last visible entry
    private int lastVisibleIndex;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.ll);
      
        moreView = getLayoutInflater().inflate(R.layout.moredata, null);
        loadText = (TextView) moreView.findViewById(R.id.bt_load);
        pg = (ProgressBar) moreView.findViewById(R.id.pg);
        listView.addFooterView(moreView);

        
        // Use map to load data, initialize 10 pieces of data
        list = new ArrayList<String>();
      
        // instantiate SimpleAdapter
        adapter=  new ArrayAdapter(this, R.layout.item,  R.id.tv_title, list);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);
        
        loadDate();

        /** Manual loading**/     
//        loadText.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
// pg.setVisibility(View.VISIBLE);// make the progress bar visible
// loadText.setVisibility(View.GONE);// The button is invisible
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        loadDate();
//                        loadText.setVisibility(View.VISIBLE);
//                        pg.setVisibility(View.GONE);
// adapter.notifyDataSetChanged();// Notify listView to refresh data
//                    }
//                }, 2000);
//            }
//        });

    }

    
    /** Explain    
     * onScroll is triggered after sliding
     * onScrollStateChanged is triggered by dragging and sliding
     * */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // Calculate the index of the last visible item
        lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;

        // All entries are equal to the maximum number, then remove the bottom View
        if (totalItemCount == MaxNum + 1) {
        	listView.removeFooterView(moreView);
            Toast.makeText(this, "All data is loaded", Toast.LENGTH_LONG).show();
        }
    }

    
    //When sliding, determine whether it has reached the bottom.
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    	//OnScrollListener.SCROLL_STATE_TOUCH_SCROLL
    	//OnScrollListener.SCROLL_STATE_FLING
    	//OnScrollListener.SCROLL_STATE_IDLE
    	
    	if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                && lastVisibleIndex == adapter.getCount()) {
            // Automatically load when swiped to the bottom
             pg.setVisibility(View.VISIBLE);
             loadText.setVisibility(View.GONE);
             handler.postDelayed(new Runnable() {
	             @Override
	             public void run() {
		                 loadDate();
		                 loadText.setVisibility(View.VISIBLE);
		                 pg.setVisibility(View.GONE);
		                 adapter.notifyDataSetChanged();
		             }
	          }, 2000);
        }
    }

    
    // load data here
	private void loadDate(){
		
	        int count = adapter.getCount();

//	        if (count + 5 < MaxNum) {
//	            for (int i = count; i < count + 5; i++) {
// list.add("Add new line" + i + "Line");
//	            }    
//	        } else {
//	            for (int i = count; i < MaxNum; i++) {
// list.add("Add new line" + i + "Line");
//	            }
//	        }

	        //load 10 at a time
	        for (int i = count; i < count + 20; i++) {
                list.add("Add new line" + i + "Line");
            }
	        
	     
	    }
	}

Several projects:

1. The above simple pull-up loads more examples

http://download.csdn.net/detail/kongbaidepao/6884945


2. An example of an online pull-up to load more pull-down refresh

http://download.csdn.net/detail/kongbaidepao/6884933


3. There is also a very complete listview scrollview gridview to load more examples

http://download.csdn.net/detail/kongbaidepao/6884999


In addition, there is an open source project attached, if you are interested, you can go and see it.

https://github.com/chrisbanes/Android-PullToRefresh

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325921110&siteId=291194637