This article describes the method of Android programming to implement infinite loop display of ListView content. Share it for your reference, as follows:
In fact, to achieve infinite loop display, the main thing is to implement the class that inherits Adapter.
I'm using BaseAdapter here
private class MyAdapter extends BaseAdapter{ private Context context; private String[] strs = null; LayoutInflater inflater = null; public MyAdapter(Context context){ = context; //The data displayed in the listview strs = new String[]{"0","1","2","3","4","5","6","7","8","9"}; inflater = (context); } public MyAdapter(){ } @Override public int getCount() { // TODO Auto-generated method stub //Returns the maximum value that int can save, this value is 2147483647 return Integer.MAX_VALUE; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View view, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if(view == null){ holder = new ViewHolder(); view = (, null); = (TextView) (); (holder); }else{ holder = (ViewHolder) (); } //strs[position%] implements the loop of data in listview (strs[position%]); return view; } } class ViewHolder{ public TextView text; }
Assign adapter to listview in onCreate method
@Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); listView = (ListView) findViewById(); MyAdapter adapter = new MyAdapter(this); (adapter); (Integer.MAX_VALUE/2+1); //Set the default selection after listview is initialized, otherwise the listview can only be dragged upwards but not downwards after listview is initialized.}
In fact, strictly speaking, this program is not an infinite loop, but there is too much data in the listview, reaching more than 2 billion, so it can also be regarded as an infinite loop.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android layout layout tips summary》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android operating json format data skills》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.