Android cyclic ListView

Source: Internet
Author: User

Android cyclic ListView was previously implemented in iOS, which is complicated to implement. Later, I found an example on the Internet, which is easy to implement and has a good idea. I will sort it out.

Code download: http://pan.baidu.com/s/1bnEKxd1

This class is a ListView Adapter with nothing special.

ListAdapter. java

Package com. packet.com. infinitescrollview; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. textView; public class ListAdapter extends BaseAdapter {private List
 
  
List; private LayoutInflater mInflater; public ListAdapter (Context context, List
  
   
List) {this. list = list; this. mInflater = LayoutInflater. from (context) ;}@ Overridepublic int getCount () {// TODO Auto-generated method stubif (list! = Null) {// The returned list is three times the displayed content. size () * 3;} return 0;} @ Overridepublic Object getItem (int arg0) {// TODO Auto-generated method stubreturn list. get (arg0% list. size () ;}@ Overridepublic long getItemId (int arg0) {// TODO Auto-generated method stubreturn arg0 % list. size () ;}@ Overridepublic View getView (int postition, View converView, ViewGroup arg2) {// TODO Auto-generated method stubViewHoler viewHoler = null; if (converView = null) {viewHoler = new ViewHoler (); converView = mInflater. inflate (R. layout. adapter_list_layout, null); viewHoler. tvText = (TextView) converView. findViewById (R. id. adapter_list_layout_ TV); converView. setTag (viewHoler);} else {viewHoler = (ViewHoler) converView. getTag ();} viewHoler. tvText. setText (list. get (postition % list. size (); return converView;} static class ViewHoler {TextView tvText ;}}
  
 

MainActivity. java

package com.pactera.infinitescrollview;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.widget.AbsListView;import android.widget.ListView;import android.widget.AbsListView.OnScrollListener;public class MainActivity extends Activity implements OnScrollListener {private ListView listView;private List
 
   list;private ListAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.listView1);list = getList();adapter = new ListAdapter(this, list);listView.setAdapter(adapter);listView.setOnScrollListener(this);listView.setSelection(list.size());}public List
  
    getList() {List
   
     list = new ArrayList
    
     ();for (int i = 0; i < 10; i++) {list.add(String.valueOf(i));}return list;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {// TODO Auto-generated method stubLog.d("ff", ""+ firstVisibleItem);     if (firstVisibleItem <= 2) {listView.setSelection(list.size() + 2);}else if (firstVisibleItem + visibleItemCount > adapter.getCount() - 2) {listView.setSelection(firstVisibleItem - list.size());}}@Overridepublic void onScrollStateChanged(AbsListView arg0, int arg1) {// TODO Auto-generated method stub}}
    
   
  
 


Focus on the if statement of the onScroll method above



Activity_main.xml

     
      
  
 

Adapter_list_layout.xml

 
       
      
 




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.