Code example for LinkedList

Methods: add, isEmpty, remove

0
		mRecycledViews.addFirst(v);
		detachViewFromParent(v);
	} 
 
	protected View getRecycledView() {
		return (mRecycledViews.isEmpty() ? null : mRecycledViews.remove());
	} 
 
	@Override 
	public void setSelection(int position) {
		mNextScreen = INVALID_SCREEN;
		mScroller.forceFinished(true);
		if (mAdapter == null)
			return; 
		 
		position = Math.max(position, 0);
		position = Math.min(position, mAdapter.getCount()-1);
 
		recycleViews(); 
 
		View currentView = makeAndAddView(position, true);
		mLoadedViews.addLast(currentView);
 
		if (mViewInitializeListener != null)
			mViewInitializeListener.onViewLazyInitialize(currentView, position);
 
		for(int offset = 1; mSideBuffer - offset >= 0; offset++) {
			int leftIndex = position - offset;
			int rightIndex = position + offset;
			if(leftIndex >= 0)
				mLoadedViews.addFirst(makeAndAddView(leftIndex, false));
			if(rightIndex < mAdapter.getCount())
				mLoadedViews.addLast(makeAndAddView(rightIndex, true));
		} 
 
		mCurrentBufferIndex = mLoadedViews.indexOf(currentView);
		mCurrentAdapterIndex = position;
 
		requestLayout();
		setVisibleView(mCurrentBufferIndex, false);
		if (mIndicator != null) {
			mIndicator.onSwitched(currentView, mCurrentAdapterIndex);
		} 
		if (mViewSwitchListener != null) {
			mViewSwitchListener.onSwitched(currentView, mCurrentAdapterIndex);
		} 
	} 
 
	private void resetFocus() { 
		logBuffer(); 
		recycleViews(); 
		removeAllViewsInLayout();
		mLazyInit.addAll(EnumSet.allOf(LazyInit.class));
 
		for (int i = Math.max(0, mCurrentAdapterIndex - mSideBuffer); i < Math
				.min(mAdapter.getCount(), mCurrentAdapterIndex + mSideBuffer
						+ 1); i++) {
			mLoadedViews.addLast(makeAndAddView(i, true));
			if (i == mCurrentAdapterIndex) {
				mCurrentBufferIndex = mLoadedViews.size() - 1;
				if (mViewInitializeListener != null)
					mViewInitializeListener.onViewLazyInitialize(mLoadedViews.getLast(), mCurrentAdapterIndex);
			} 
		} 
		logBuffer(); 
		requestLayout();
	} 
 
	private void postViewSwitched(int direction) {
		if (direction == 0)
			return; 
 
		if (direction > 0) { // to the right
			mCurrentAdapterIndex++;
			mCurrentBufferIndex++;
			mLazyInit.remove(LazyInit.LEFT);
			mLazyInit.add(LazyInit.RIGHT);
 
			// Recycle view outside buffer range 
			if (mCurrentAdapterIndex > mSideBuffer) {
				recycleView(mLoadedViews.removeFirst());
				mCurrentBufferIndex--;
			} 
 
			// Add new view to buffer 
			int newBufferIndex = mCurrentAdapterIndex + mSideBuffer;
			if (newBufferIndex < mAdapter.getCount())
				mLoadedViews.addLast(makeAndAddView(newBufferIndex, true));
 
		} else { // to the left 
			mCurrentAdapterIndex--;
			mCurrentBufferIndex--;
			mLazyInit.add(LazyInit.LEFT);
			mLazyInit.remove(LazyInit.RIGHT);
 
			// Recycle view outside buffer range 
			if (mAdapter.getCount() - 1 - mCurrentAdapterIndex > mSideBuffer) {
				recycleView(mLoadedViews.removeLast());
			} 
 
			// Add new view to buffer 
			int newBufferIndex = mCurrentAdapterIndex - mSideBuffer;
			if (newBufferIndex > -1) {
				mLoadedViews.addFirst(makeAndAddView(newBufferIndex, false));
				mCurrentBufferIndex++;
			} 
 
		} 
 
		requestLayout();
		setVisibleView(mCurrentBufferIndex, true);
		if (mIndicator != null) {
			mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex),
					mCurrentAdapterIndex);
		} 
		if (mViewSwitchListener != null) {
			mViewSwitchListener
					.onSwitched(mLoadedViews.get(mCurrentBufferIndex),
							mCurrentAdapterIndex);
		} 
		logBuffer(); 
	} 
 
	@Override protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
		LayoutParams lp = child.getLayoutParams();
		final int childWidthSpec = getChildMeasureSpec(parentWidthMeasureSpec, getWidthPadding(), lp.width);
		final int childHeightSpec = getChildMeasureSpec(parentHeightMeasureSpec, getHeightPadding(), lp.height);
		child.measure(childWidthSpec, childHeightSpec);
	} 
 
	private View setupChild(View child, boolean addToEnd, boolean recycle) {
		final LayoutParams lp = child.getLayoutParams();
		child.measure(MeasureSpec.makeMeasureSpec(getChildWidth(), MeasureSpec.EXACTLY),
				MeasureSpec.makeMeasureSpec(getChildHeight(), MeasureSpec.EXACTLY));
		if (recycle)
			attachViewToParent(child, (addToEnd ? -1 : 0), lp);
		else 
			addViewInLayout(child, (addToEnd ? -1 : 0), lp, true);
		return child;
	} 
 
	private View makeAndAddView(int position, boolean addToEnd) {
		View view = obtainView(position);
		return setupChild(view, addToEnd, mLastObtainedViewWasRecycled);
	} 
 
	private View obtainView(int position) {
		View convertView = getRecycledView();
		View view = mAdapter.getView(position, convertView, this);
		if(view != convertView && convertView != null)
			mRecycledViews.add(convertView);
		mLastObtainedViewWasRecycled = (view == convertView);
		ViewGroup.LayoutParams p = view.getLayoutParams();
		if (p == null) {
			p = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			view.setLayoutParams(p);