Code example for Adapter

Methods: getItemViewTypegetView

0
		detachViewFromParent(v);
	} 
 
	protected View getRecycledView(int position)
	{ 
		int type = this.mAdapter.getItemViewType(position);
 
		return (mRecycledViews[type].isEmpty() ? null : mRecycledViews[type].remove(0));
	} 
 
	@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; SIDE_BUFFER - 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 - SIDE_BUFFER); i < Math.min(mAdapter.getCount(), mCurrentAdapterIndex + SIDE_BUFFER + 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 > SIDE_BUFFER)
			{ 
				recycleView(mLoadedViews.removeFirst());
				mCurrentBufferIndex--;
			} 
 
			// Add new view to buffer 
			int newBufferIndex = mCurrentAdapterIndex + SIDE_BUFFER;
			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 > SIDE_BUFFER)
			{ 
				recycleView(mLoadedViews.removeLast());
			} 
 
			// Add new view to buffer 
			int newBufferIndex = mCurrentAdapterIndex - SIDE_BUFFER;
			if (newBufferIndex > -1)
			{ 
				mLoadedViews.addFirst(makeAndAddView(newBufferIndex, false));
				mCurrentBufferIndex++;
			} 
		} 
 
		requestLayout();
		setVisibleView(mCurrentBufferIndex, true);
		if (mIndicator != null && mLoadedViews.size() > mCurrentBufferIndex)
		{ 
			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)
	{ 
		return makeAndAddView(position, addToEnd, getRecycledView(position));
	} 
 
	private View makeAndAddView(int position, boolean addToEnd, View convertView)
	{ 
		View view = mAdapter.getView(position, convertView, this);
 
		if (view != convertView && convertView != null)
		{ 
			int type = (Integer) convertView.getTag(VIEWTYPE_TAG);
			mRecycledViews[type].add(convertView);
		} 
		else 
		{ 
			view.setTag(VIEWTYPE_TAG, mAdapter.getItemViewType(position));
		} 
 
		return setupChild(view, addToEnd, view == convertView);
	}