封闭或圆形垂直ListView Android

时间:2014-07-09 12:02:24

标签: android listview circular-list

我有一个垂直列表视图我希望listview应该关闭。例如,如果在Listview中到达最后一个项目,则显示最后一个项目下面的第一个项目。这意味着项目应采用循环格式。如果我从第一个项目滚动它应该显示第一个项目之前的最后一个项目。我想要双方滚动。

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {
ListView list;
long startTime;
long endTime;


List<String> mList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.list);
    downloadDetails();

     String str;
     for (int i = 0; i < 10; i++) {
     str = new String("Data --- " + i);
     mList.add(str);
     }
     CircularAdapter adapter = new CircularAdapter(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;
    }

    @SuppressLint("NewApi")
    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(3000);
                    synchronized (mPauseLock) {
                        while (mPaused) {
                            try {
                                mPauseLock.wait();// putting thread in wait
                                                    // list of mPauseLock
                                                    // object
                            } catch (InterruptedException e) {
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
    }

    // to pause list
    public void onPause() {
        synchronized (mPauseLock) {
            mPaused = true;
        }
    }

    // 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 CircularAdapter extends ArrayAdapter {

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

    @SuppressWarnings("unchecked")
    public CircularAdapter(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) {

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

}