Pull and refresh Android listView (load more)
Previously, it was also implemented. The ListView was pulled up and refreshed, but the open source code was used. Because I was too lazy and I didn't study the source code, I am not so fluent in writing it now. Fortunately, I found my own characteristics, so I wrote a Demo, basically implementing the new feature of ListView. As a practice, it may not be so perfect, based on the research of open source code, we will write a better and easy-to-understand drop-down and release a new Demo.
Implementation:
Go directly to the Code:
MainActivity:
Package com. listdemo; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. linearLayout; import android. widget. listView; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private ListView listView; private MyAdapter adapter; private List
List = new ArrayList
(); Private List
ListAll = new ArrayList
(); Private Map
Map = new HashMap
(); Private boolean is_divPage; // whether to perform paging operations private MyAdapter mAdatper; private TextView TV _msg; private LinearLayout loading; private LinearLayout list_footer; // public static int pageNum = 1; // set the initialization value of pageNo to 1, that is, the first page of data is obtained by default. @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. listView1);/*** Add the footer of ListView */list_footer = (LinearLayout) getLayoutInflater (). inflate (R. layout. list_footer, null); list_footer.setBackgroundColor (Color. parseColor (# ffffff); TV _msg = (TextView) list_footer.findViewById (R. Id. TV _msg); // more loading = (LinearLayout) list_footer.findViewById (R. id. loading); // read + progressBartv_msg.setVisibility (View. GONE); loading. setVisibility (View. VISIBLE); listView. addFooterView (list_footer);/*** add data to listView */initData ();/*** add data to each page (the data stored in each list) add to ListAll (store all data. */ListAll. addAll (list); adapter = new MyAdapter (this, listAll); listView. setAdapter (adapter);/*** listview listener event */listView. setOnScrollListener (new OnScrollListener () {@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {/*** if you wait until this page (is_divPage = true, and slide to stop (at this time the hand has left the screen) and automatically load more. */If (is_divPage & scrollState = OnScrollListener. SCROLL_STATE_IDLE) {Toast. makeText (MainActivity. this, getting more data ..., toast. LENGTH_SHORT ). show (); Log. d (zhongyao, call onScrollStateChanged !!!!); InitData (); listAll. addAll (list); adapter. yydatasetchanged (); TV _msg.setVisibility (View. INVISIBLE); loading. setVisibility (View. VISIBLE);} else if (! Is_divPage & scrollState = OnScrollListener. SCROLL_STATE_IDLE) {TV _msg.setVisibility (View. VISIBLE); loading. setVisibility (View. INVISIBLE) ;}@ Overridepublic void onScroll (AbsListView view, int detail, int visibleItemCount, int totalItemCount) {is_divPage = (detail + visibleItemCount = totalItemCount );}});} private void initData () {list. clear (); for (int I = 0; I <10; I ++) {list. add (zhongyao + I );}}}
Adapter:
MyAdapter:
Package com. listdemo; import java. util. arrayList; import java. util. list; import java. util. map; import android. content. context; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. textView; import android. widget. toast;/*** adapter * @ author Administrator **/public class MyAdapter extends BaseAdapter {private Context context Context; private List
List = new ArrayList
(); Public MyAdapter (Context contex, List
List) {this. context = contex; this. list = list;} @ Overridepublic int getCount () {return list. size () ;}@ Overridepublic Object getItem (int position) {return list. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (final int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {holder = new ViewHolder (); convertView = LayoutInflater. from (context ). inflate (R. layout. list_item, null); holder. TV _name = (TextView) convertView. findViewById (R. id. textView1); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. TV _name.setText (list. get (position); convertView. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (context, your choice is + position, 1 ). show () ;}}); return convertView;} class ViewHolder {private TextView TV _name, TV _age ;}}
Layout file:
Activity_main:
List_item;
List_footer: