3

Now i should implement infinite scrolling to listview, for this i had found one easiest code that is

 @Override
 public void onScroll(AbsListView view, int firstVisibleItem,
    int visibleItemCount, int totalItemCount) {
//leave this empty
 }

  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
   if (scrollState == SCROLL_STATE_IDLE) {
    if (listView.getLastVisiblePosition() >= listView.getCount() - threshold) {
        currentPage++;
        //load more list items:
        loadElements(currentPage);
    }
     }
    }

but now i dont know what to include in the place if loadElements(current Page)

Steph Sharp
  • 10,659
  • 4
  • 42
  • 79
jack
  • 123
  • 1
  • 4
  • 12
  • i have tried http://stackoverflow.com/questions/1080811/android-endless-list n many but nt able to scroll the till end @Sumedh Tambat – jack Mar 07 '13 at 09:46

1 Answers1

1

Here is an example code for you. this list scrolls infinite and using thread, it scrolls automatically. (pauses for while and coninues ...)

When you touch the list, list scrolling pauses and when you release it continues to automatic scroll.

If you touch under 100 miliseconds then it will considered item on click.

You can also scroll down or up, it will eventually continue to auto scrolling.

Come to think of it, it has turn into web like list. shame. :(

///////////////////////////////////////////////////////////////////////

List Item XML layout : item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical" >
    <TextView 
        android:background="#00ff00"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:id="@+id/name"
        android:textColor="#000000"
        android:textSize="26sp"
        android:gravity="center"
        android:textStyle="bold"/>

</LinearLayout>

///////////////////////////////////////////////////////////////////////

activity layout : activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="25dp"
    android:gravity="center"
    tools:context=".MainActivity" >

   <ListView 
       android:id="@+id/list"
       android:layout_width="fill_parent"
       android:layout_height="450dp"
       android:background="#bbbbbb"
       android:divider="#000000"/>

</RelativeLayout>

///////////////////////////////////////////////////////////////////////

activiy code (includes adapter and runnable thread) : MainActivity.java

public class MainActivity extends Activity {
    ListView  list;
    long startTime;
    long endTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.list);
        List<String> mList = new ArrayList<String>();
        String str;
        for(int i=0;i<10;i++){
            str = new String("Data --- "+i);
            mList.add(str);
        }
        LAdapter adapter = new LAdapter(this,0,mList);
        list.setAdapter(adapter);
        final YourRunnable runy = new YourRunnable();



        list.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN ){

                    startTime= (new Date()).getTime();
                    runy.onPause();//pausing thread actually pauses scrolling
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    endTime= (new Date()).getTime();
                    if((endTime - startTime)<=100){//100 mill second limit for click


                        //Log.i("ITEM CLICK() ", "item : ");
                    }

                    runy.onResume(); //resume scrolling         
                }

                return false;
            }


        });


        new Thread(runy).start();


    }

    class YourRunnable implements Runnable {
        private Object mPauseLock;
        private boolean mPaused;
        private boolean mFinished;

        public YourRunnable() {
            mPauseLock = new Object();
            mPaused = false;
            mFinished = false;
        }

        public void run() {
            while (!mFinished) {
                //for loop is not infinite but enough as Integer.MAX_VALUE
                 for (int index = 0; index < list.getAdapter().getCount(); index++) {
                        list.smoothScrollToPositionFromTop(list.getLastVisiblePosition() + 1, 0, 10000);
                        try {
                            // it helps scrolling to stay smooth as possible (by experiment)
                            Thread.sleep(10000);
                            synchronized (mPauseLock) {
                                while (mPaused) {
                                    try {
                                        mPauseLock.wait();//putting thread in wait list of mPauseLock object
                                    } catch (InterruptedException e) {
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }


            }
        }

        /**
         * Call this method to pause thread.
         */
        public void onPause() {
            synchronized (mPauseLock) {
                mPaused = true;
            }
        }

        /**
         * Call this method to resume thread.
         */
        public void onResume() {
            synchronized (mPauseLock) {
                mPaused = false;
                mPauseLock.notifyAll();//notify all object that are waiting on the wait list of mPauseLock object
            }
        }

    }
    private class LAdapter extends ArrayAdapter{

        List<String> mlist;
        Context mContext;
        LayoutInflater inflater;
        public final int HALF_MAX_VALUE = Integer.MAX_VALUE/2;
        public final int MIDDLE;

        public LAdapter(Context ctx,int resId, List<String> objects){
            super(ctx, resId, objects);
            mContext = ctx;
            mlist = objects;
            inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            MIDDLE = HALF_MAX_VALUE - HALF_MAX_VALUE % mlist.size();

        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Integer.MAX_VALUE;
        }

        @Override
        public String getItem(int position) {
            // TODO Auto-generated method stub
            int relativePos = position %  mlist.size();
            Log.i("RELATIVE : "," POS:"+relativePos);
            return mlist.get(relativePos);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

//          Log.i("POSITION TAG", "POSITION : "+position);
//          if(position>(getCount()-1)){
//              return null;
//          }
            ViewHolder holder = null;

            if (convertView == null) 
            {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item, parent, false);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                convertView.setTag(holder);
            } 
            else 
            {
                holder = (ViewHolder) convertView.getTag();
            }

            String model = getItem(position);
            holder.name.setText(model);

            convertView.setOnClickListener(new ListenerT(model) {

                @Override
                public void onClick(View v) {
                    Log.i("CLICK", "ITEM---"+name );

                }
            });

            return convertView;

        }


    }

    //use your own listener to pass parameter
    private class ListenerT implements OnClickListener{

        String name;
        public ListenerT(String nm){
            name = nm;
        }
        @Override
        public void onClick(View v) {


        }

    }
    private class ViewHolder{
        TextView name;
    }


}
Engin OZTURK
  • 1,865
  • 1
  • 20
  • 12