3

现在我应该实现无限滚动到列表视图,为此我找到了一个最简单的代码,即

 @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);
    }
     }
    }

但现在我不知道如果 loadElements(current Page) 包含什么内容

4

1 回答 1

1

这是给您的示例代码。此列表无限滚动并使用线程,它会自动滚动。(停顿片刻,然后继续……)

当您触摸列表时,列表滚动会暂停,当您松开它时会继续自动滚动。

如果您触摸不到 100 毫秒,那么它会被视为点击项目。

您也可以向下或向上滚动,它最终会继续自动滚动。

想想看,它已经变成了类似网络的列表。耻辱。:(

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

列表项 XML 布局: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_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>

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

活动代码(包括适配器和可运行线程):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;
    }


}
于 2013-05-22T09:11:55.560 回答