Code example for LinkedList

Methods: addFirst, addLast, get, removeFirst, removeLast, size

0
 
			View recycleView = null;
 
			// Remove view outside buffer range 
			if (mCurrentAdapterIndex > mSideBuffer) {
				recycleView = mLoadedViews.removeFirst();
				detachViewFromParent(recycleView);
				// removeView(recycleView); 
				mCurrentBufferIndex--;
			} 
 
			// Add new view to buffer 
			int newBufferIndex = mCurrentAdapterIndex + mSideBuffer;
			if (newBufferIndex < mAdapter.getCount())
				mLoadedViews.addLast(makeAndAddView(newBufferIndex, true,
						recycleView));
 
		} else { // to the left 
			mCurrentAdapterIndex--;
			mCurrentBufferIndex--;
			View recycleView = null;
 
			// Remove view outside buffer range 
			if (mAdapter.getCount() - 1 - mCurrentAdapterIndex > mSideBuffer) {
				recycleView = mLoadedViews.removeLast();
				detachViewFromParent(recycleView);
			} 
 
			// Add new view to buffer 
			int newBufferIndex = mCurrentAdapterIndex - mSideBuffer;
			if (newBufferIndex > -1) {
				mLoadedViews.addFirst(makeAndAddView(newBufferIndex, false,
						recycleView));
				mCurrentBufferIndex++;
			} 
 
		} 
 
		requestLayout();
		setVisibleView(mCurrentBufferIndex, true);
		if (mIndicator != null) {
			mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex),
					mCurrentAdapterIndex);
		} 
		if (mViewSwitchListener != null) {
			mViewSwitchListener
					.onSwitched(mLoadedViews.get(mCurrentBufferIndex),
							mCurrentAdapterIndex);
		} 
		logBuffer(); 
	} 
 
	private View setupChild(View child, boolean addToEnd, boolean recycle) {
		ViewGroup.LayoutParams p = (ViewGroup.LayoutParams) child
				.getLayoutParams();
		if (p == null) {
			p = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT, 0);
		} 
		if (recycle)
			attachViewToParent(child, (addToEnd ? -1 : 0), p);
		else 
			addViewInLayout(child, (addToEnd ? -1 : 0), p, true);
		return child;
	} 
 
	private View makeAndAddView(int position, boolean addToEnd, View convertView) {
		View view = mAdapter.getView(position, convertView, this);
		return setupChild(view, addToEnd, convertView != null);
	} 
 
	class AdapterDataSetObserver extends DataSetObserver {
 
		@Override 
		public void onChanged() { 
			View v = getChildAt(mCurrentBufferIndex);
			if (v != null) {
				for (int index = 0; index < mAdapter.getCount(); index++) {
					if (v.equals(mAdapter.getItem(index))) {
						mCurrentAdapterIndex = index;
						break; 
					} 
				} 
			} 
			resetFocus(); 
		} 
 
		@Override 
		public void onInvalidated() { 
			// Not yet implemented! 
		} 
 
	} 
 
	private void logBuffer() { 
 
		Log.d("viewflow", "Size of mLoadedViews: " + mLoadedViews.size() +
				"X: " + mScroller.getCurrX() + ", Y: " + mScroller.getCurrY());
		Log.d("viewflow", "IndexInAdapter: " + mCurrentAdapterIndex
				+ ", IndexInBuffer: " + mCurrentBufferIndex);
	} 
}