Example usage for android.view View setSelected

List of usage examples for android.view View setSelected

Introduction

In this page you can find the example usage for android.view View setSelected.

Prototype

public void setSelected(boolean selected) 

Source Link

Document

Changes the selection state of this view.

Usage

From source file:com.appunite.list.HorizontalListView.java

/**
 * When selection changes, it is possible that the previously selected or the
 * next selected item will change its size.  If so, we need to offset some folks,
 * and re-layout the items as appropriate.
 *
 * @param selectedView The currently selected view (before changing selection).
 *   should be <code>null</code> if there was no previous selection.
 * @param direction Either {@link android.view.View#FOCUS_UP} or
 *        {@link android.view.View#FOCUS_DOWN}.
 * @param newSelectedPosition The position of the next selection.
 * @param newFocusAssigned whether new focus was assigned.  This matters because
 *        when something has focus, we don't want to show selection (ugh).
 *///from   ww w .ja v  a 2s . co  m
private void handleNewSelectionChange(View selectedView, int direction, int newSelectedPosition,
        boolean newFocusAssigned) {
    if (newSelectedPosition == INVALID_POSITION) {
        throw new IllegalArgumentException("newSelectedPosition needs to be valid");
    }

    // whether or not we are moving down or up, we want to preserve the
    // top of whatever view is on top:
    // - moving down: the view that had selection
    // - moving up: the view that is getting selection
    View leftView;
    View rightView;
    int leftViewIndex, rightViewIndex;
    boolean leftSelected = false;
    final int selectedIndex = mSelectedPosition - mFirstPosition;
    final int nextSelectedIndex = newSelectedPosition - mFirstPosition;
    if (direction == View.FOCUS_LEFT) {
        leftViewIndex = nextSelectedIndex;
        rightViewIndex = selectedIndex;
        leftView = getChildAt(leftViewIndex);
        rightView = selectedView;
        leftSelected = true;
    } else {
        leftViewIndex = selectedIndex;
        rightViewIndex = nextSelectedIndex;
        leftView = selectedView;
        rightView = getChildAt(rightViewIndex);
    }

    final int numChildren = getChildCount();

    // start with top view: is it changing size?
    if (leftView != null) {
        leftView.setSelected(!newFocusAssigned && leftSelected);
        measureAndAdjustRight(leftView, leftViewIndex, numChildren);
    }

    // is the bottom view changing size?
    if (rightView != null) {
        rightView.setSelected(!newFocusAssigned && !leftSelected);
        measureAndAdjustRight(rightView, rightViewIndex, numChildren);
    }
}

From source file:com.appunite.list.ListView.java

/**
 * When selection changes, it is possible that the previously selected or the
 * next selected item will change its size.  If so, we need to offset some folks,
 * and re-layout the items as appropriate.
 *
 * @param selectedView The currently selected view (before changing selection).
 *   should be <code>null</code> if there was no previous selection.
 * @param direction Either {@link android.view.View#FOCUS_UP} or
 *        {@link android.view.View#FOCUS_DOWN}.
 * @param newSelectedPosition The position of the next selection.
 * @param newFocusAssigned whether new focus was assigned.  This matters because
 *        when something has focus, we don't want to show selection (ugh).
 *///from   ww  w.  j a  va2s .  com
private void handleNewSelectionChange(View selectedView, int direction, int newSelectedPosition,
        boolean newFocusAssigned) {
    if (newSelectedPosition == INVALID_POSITION) {
        throw new IllegalArgumentException("newSelectedPosition needs to be valid");
    }

    // whether or not we are moving down or up, we want to preserve the
    // top of whatever view is on top:
    // - moving down: the view that had selection
    // - moving up: the view that is getting selection
    View topView;
    View bottomView;
    int topViewIndex, bottomViewIndex;
    boolean topSelected = false;
    final int selectedIndex = mSelectedPosition - mFirstPosition;
    final int nextSelectedIndex = newSelectedPosition - mFirstPosition;
    if (direction == View.FOCUS_UP) {
        topViewIndex = nextSelectedIndex;
        bottomViewIndex = selectedIndex;
        topView = getChildAt(topViewIndex);
        bottomView = selectedView;
        topSelected = true;
    } else {
        topViewIndex = selectedIndex;
        bottomViewIndex = nextSelectedIndex;
        topView = selectedView;
        bottomView = getChildAt(bottomViewIndex);
    }

    final int numChildren = getChildCount();

    // start with top view: is it changing size?
    if (topView != null) {
        topView.setSelected(!newFocusAssigned && topSelected);
        measureAndAdjustDown(topView, topViewIndex, numChildren);
    }

    // is the bottom view changing size?
    if (bottomView != null) {
        bottomView.setSelected(!newFocusAssigned && !topSelected);
        measureAndAdjustDown(bottomView, bottomViewIndex, numChildren);
    }
}

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

/**
 * When selection changes, it is possible that the previously selected or the next selected item will change its size. If so, we
 * need to offset some folks, and re-layout the items as appropriate.
 * /*  w w w.j  a  v  a2s.  c om*/
 * @param selectedView
 *           The currently selected view (before changing selection). should be <code>null</code> if there was no previous
 *           selection.
 * @param direction
 *           Either {@link android.view.View#FOCUS_UP} or {@link android.view.View#FOCUS_DOWN}.
 * @param newSelectedPosition
 *           The position of the next selection.
 * @param newFocusAssigned
 *           whether new focus was assigned. This matters because when something has focus, we don't want to show selection
 *           (ugh).
 */
private void handleNewSelectionChange(View selectedView, int direction, int newSelectedPosition,
        boolean newFocusAssigned) {
    if (newSelectedPosition == INVALID_POSITION) {
        throw new IllegalArgumentException("newSelectedPosition needs to be valid");
    }

    // whether or not we are moving down or up, we want to preserve the
    // top of whatever view is on top:
    // - moving down: the view that had selection
    // - moving up: the view that is getting selection
    View leftView;
    View rightView;
    int leftViewIndex, rightViewIndex;
    boolean leftSelected = false;
    final int selectedIndex = mSelectedPosition - mFirstPosition;
    final int nextSelectedIndex = newSelectedPosition - mFirstPosition;

    if (direction == View.FOCUS_UP) {
        leftViewIndex = nextSelectedIndex;
        rightViewIndex = selectedIndex;
        leftView = getChildAt(leftViewIndex);
        rightView = selectedView;
        leftSelected = true;
    } else {
        leftViewIndex = selectedIndex;
        rightViewIndex = nextSelectedIndex;
        leftView = selectedView;
        rightView = getChildAt(rightViewIndex);
    }

    final int numChildren = getChildCount();

    // start with top view: is it changing size?
    if (leftView != null) {
        leftView.setSelected(!newFocusAssigned && leftSelected);
        measureAndAdjustRight(leftView, leftViewIndex, numChildren);
    }

    // is the bottom view changing size?
    if (rightView != null) {
        rightView.setSelected(!newFocusAssigned && !leftSelected);
        measureAndAdjustRight(rightView, rightViewIndex, numChildren);
    }
}

From source file:com.example.android.contactslist.ui.groupsEditor.GroupsEditorFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mActionModeCallback = new ActionMode.Callback() {

        // Called when the action mode is created; startActionMode() was called
        @Override/*from  ww  w.j  ava  2  s.c om*/
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            //MenuInflater inflater = mode.getMenuInflater();
            //inflater.inflate(R.menu.contextual_actionbar, menu);
            mode.getMenuInflater().inflate(R.menu.contextual_actionbar, menu);
            return true;
        }

        // Called each time the action mode is shown. Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {

            case R.id.menu_remove_contact:
                GoogleGroupMaker googleGroupMaker = new GoogleGroupMaker(getActivity());
                googleGroupMaker.removeGoogleGroup(mDeleteGroupID);

                // method to remove group from app DB
                final ContactStatsContract statsDb = new ContactStatsContract(getActivity());
                final GroupStatsHelper groupStatsHelper = new GroupStatsHelper(getActivity());

                // delete group from the app Database
                final int records_updated = groupStatsHelper.removeGroupFromDB(mDeleteGroupID, statsDb);

                Toast.makeText(getActivity(), Long.toString(records_updated) + " Record(s) Updated",
                        Toast.LENGTH_SHORT).show();

                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                mode.finish(); // Action picked, so close the CAB
                Toast.makeText(getActivity(), "End Action Mode", Toast.LENGTH_SHORT).show();
                return false;
            }
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
        }
    };

    getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id) {
            if (mActionMode != null) {
                return false;
            }

            // Gets the Cursor object currently bound to the ListView
            final Cursor cursor = mAdapter.getCursor();

            // Moves to the Cursor row corresponding to the ListView item that was clicked
            cursor.moveToPosition(position);

            // Using the lookupKey because the wrong ID was being returned
            mDeleteGroupID = cursor.getLong(GroupsListStatsQuery.GROUP_ID);
            // Start the CAB using the ActionMode.Callback defined above
            mActionMode = getActivity().startActionMode(mActionModeCallback);
            view.setSelected(true);
            return true;
        }
    });

}

From source file:caesar.feng.framework.widget.StaggeredGrid.ExtendableListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly.//from  w  ww .  j a va2 s  .  c  o  m
 *
 * @param child    The view to add
 * @param position The position of this child
 * @param y        The y position relative to which this view will be positioned
 * @param flowDown If true, align top edge to y. If false, align bottom
 *                 edge to y.
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *                 does not need to be remeasured.
 */
private void setupChild(View child, int position, int y, boolean flowDown, boolean selected, boolean recycled) {
    final boolean isSelected = false; // TODO : selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLLING
            && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    int itemViewType = mAdapter.getItemViewType(position);

    LayoutParams layoutParams;
    if (itemViewType == ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
        layoutParams = generateWrapperLayoutParams(child);
    } else {
        layoutParams = generateChildLayoutParams(child);
    }

    layoutParams.viewType = itemViewType;
    layoutParams.position = position;

    if (recycled || (layoutParams.recycledHeaderFooter
            && layoutParams.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        if (DBG)
            Log.d(TAG, "setupChild attachViewToParent position:" + position);
        attachViewToParent(child, flowDown ? -1 : 0, layoutParams);
    } else {
        if (DBG)
            Log.d(TAG, "setupChild addViewInLayout position:" + position);
        if (layoutParams.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            layoutParams.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowDown ? -1 : 0, layoutParams, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (needToMeasure) {
        if (DBG)
            Log.d(TAG, "setupChild onMeasureChild position:" + position);
        onMeasureChild(child, layoutParams);
    } else {
        if (DBG)
            Log.d(TAG, "setupChild cleanupLayoutState position:" + position);
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childTop = flowDown ? y : y - h;

    if (DBG) {
        Log.d(TAG, "setupChild position:" + position + " h:" + h + " w:" + w);
    }

    final int childrenLeft = getChildLeft(position);

    if (needToMeasure) {
        final int childRight = childrenLeft + w;
        final int childBottom = childTop + h;
        onLayoutChild(child, position, flowDown, childrenLeft, childTop, childRight, childBottom);
    } else {
        onOffsetChild(child, position, flowDown, childrenLeft, childTop);
    }

}

From source file:com.lcr.widget.ExtendableListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly./*from  w w w . j  a va  2s  .c om*/
 *
 * @param child    The view to add
 * @param position The position of this child
 * @param y        The y position relative to which this view will be positioned
 * @param flowDown If true, align top edge to y. If false, align bottom
 *                 edge to y.
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *                 does not need to be remeasured.
 */
private void setupChild(View child, int position, int y, boolean flowDown, boolean selected, boolean recycled) {
    final boolean isSelected = false; // TODO : selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLLING
            && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    int itemViewType = mAdapter.getItemViewType(position);

    LayoutParams layoutParams;
    if (itemViewType == ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
        layoutParams = generateWrapperLayoutParams(child);
    } else {
        layoutParams = generateChildLayoutParams(child);
    }

    layoutParams.viewType = itemViewType;
    layoutParams.position = position;

    if (recycled || (layoutParams.recycledHeaderFooter
            && layoutParams.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        if (DBG)
            Log.i(TAG, "setupChild attachViewToParent position:" + position);
        attachViewToParent(child, flowDown ? -1 : 0, layoutParams);
    } else {
        if (DBG)
            Log.i(TAG, "setupChild addViewInLayout position:" + position);
        if (layoutParams.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            layoutParams.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowDown ? -1 : 0, layoutParams, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (needToMeasure) {
        if (DBG)
            Log.i(TAG, "setupChild onMeasureChild position:" + position);
        onMeasureChild(child, layoutParams);
    } else {
        if (DBG)
            Log.i(TAG, "setupChild cleanupLayoutState position:" + position);
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childTop = flowDown ? y : y - h;

    if (DBG) {
        Log.i(TAG, "setupChild position:" + position + " h:" + h + " w:" + w);
    }

    final int childrenLeft = getChildLeft(position);

    if (needToMeasure) {
        final int childRight = childrenLeft + w;
        final int childBottom = childTop + h;
        onLayoutChild(child, position, flowDown, childrenLeft, childTop, childRight, childBottom);
    } else {
        onOffsetChild(child, position, flowDown, childrenLeft, childTop);
    }

}

From source file:com.sintn.hera.client.widget.view.stagegered.ExtendableListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly./*from  w  ww  .j  a va 2  s .  c om*/
 * 
 * @param child
 *            The view to add
 * @param position
 *            The position of this child
 * @param y
 *            The y position relative to which this view will be positioned
 * @param flowDown
 *            If true, align top edge to y. If false, align bottom edge to
 *            y.
 * @param selected
 *            Is this position selected?
 * @param recycled
 *            Has this view been pulled from the recycle bin? If so it does
 *            not need to be remeasured.
 */
private void setupChild(View child, int position, int y, boolean flowDown, boolean selected, boolean recycled) {
    final boolean isSelected = false; // TODO : selected &&
    // shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLLING
            && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    int itemViewType = mAdapter.getItemViewType(position);

    LayoutParams layoutParams;
    if (itemViewType == ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
        layoutParams = generateWrapperLayoutParams(child);
    } else {
        layoutParams = generateChildLayoutParams(child);
    }

    layoutParams.viewType = itemViewType;
    layoutParams.position = position;

    if (recycled || (layoutParams.recycledHeaderFooter
            && layoutParams.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        if (DBG)
            Log.d(TAG, "setupChild attachViewToParent position:" + position);
        attachViewToParent(child, flowDown ? -1 : 0, layoutParams);
    } else {
        if (DBG)
            Log.d(TAG, "setupChild addViewInLayout position:" + position);
        if (layoutParams.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            layoutParams.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowDown ? -1 : 0, layoutParams, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (needToMeasure) {
        if (DBG)
            Log.d(TAG, "setupChild onMeasureChild position:" + position);
        onMeasureChild(child, layoutParams);
    } else {
        if (DBG)
            Log.d(TAG, "setupChild cleanupLayoutState position:" + position);
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childTop = flowDown ? y : y - h;

    if (DBG) {
        Log.d(TAG, "setupChild position:" + position + " h:" + h + " w:" + w);
    }

    final int childrenLeft = getChildLeft(position);

    if (needToMeasure) {
        final int childRight = childrenLeft + w;
        final int childBottom = childTop + h;
        onLayoutChild(child, position, flowDown, childrenLeft, childTop, childRight, childBottom);
    } else {
        onOffsetChild(child, position, flowDown, childrenLeft, childTop);
    }

}

From source file:com.appunite.list.HorizontalListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly.//from ww  w.  j  a va 2 s .  c om
 *
 * @param child The view to add
 * @param position The position of this child
 * @param x The x position relative to which this view will be positioned
 * @param flowRight If true, align top edge to x. If false, align bottom
 *        edge to x.
 * @param childTop Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 */
private void setupChild(View child, int position, int x, boolean flowRight, int childTop, boolean selected,
        boolean recycled) {
    final boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make some up...
    // noinspection unchecked
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if ((recycled && !p.forceAdd)
            || (p.recycledHeaderFooter && p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        attachViewToParent(child, flowRight ? -1 : 0, p);
    } else {
        p.forceAdd = false;
        if (p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            p.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowRight ? -1 : 0, p, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            Compat.setActivated(child, mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
                mListPadding.top + mListPadding.bottom, p.height);
        int lpWidth = p.width;
        int childWidthSpec;
        if (lpWidth > 0) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childLeft = flowRight ? x : x - w;

    if (needToMeasure) {
        final int childBottom = childTop + h;
        final int childRight = childLeft + w;
        child.layout(childLeft, childTop, childRight, childBottom);
    } else {
        child.offsetTopAndBottom(childTop - child.getTop());
        child.offsetLeftAndRight(childLeft - child.getLeft());
    }

    if (mCachingStarted && !child.isDrawingCacheEnabled()) {
        child.setDrawingCacheEnabled(true);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && recycled
            && (((LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
        Compat.jumpDrawablesToCurrentState(child);
    }
}

From source file:com.common.widget.hzlib.HorizontalListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly.//from  ww  w.java  2  s .  c om
 *
 * @param child The view to add
 * @param position The position of this child
 * @param x The x position relative to which this view will be positioned
 * @param flowRight If true, align top edge to x. If false, align bottom
 *        edge to x.
 * @param childTop Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 */
@SuppressLint("NewApi")
private void setupChild(View child, int position, int x, boolean flowRight, int childTop, boolean selected,
        boolean recycled) {
    final boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make some up...
    // noinspection unchecked
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if ((recycled && !p.forceAdd)
            || (p.recycledHeaderFooter && p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        attachViewToParent(child, flowRight ? -1 : 0, p);
    } else {
        p.forceAdd = false;
        if (p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            p.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowRight ? -1 : 0, p, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            child.setActivated(mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
                mListPadding.top + mListPadding.bottom, p.height);
        int lpWidth = p.width;
        int childWidthSpec;
        if (lpWidth > 0) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childLeft = flowRight ? x : x - w;

    if (needToMeasure) {
        final int childBottom = childTop + h;
        final int childRight = childLeft + w;
        child.layout(childLeft, childTop, childRight, childBottom);
    } else {
        child.offsetTopAndBottom(childTop - child.getTop());
        child.offsetLeftAndRight(childLeft - child.getLeft());
    }

    if (mCachingStarted && !child.isDrawingCacheEnabled()) {
        child.setDrawingCacheEnabled(true);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && recycled
            && (((LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
        Compat.jumpDrawablesToCurrentState(child);
    }
}

From source file:com.liu.hz.view.HorizontalListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly./* w  w w .java 2 s  .  c  om*/
 *
 * @param child The view to add
 * @param position The position of this child
 * @param x The x position relative to which this view will be positioned
 * @param flowRight If true, align top edge to x. If false, align bottom
 *        edge to x.
 * @param childTop Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 */
private void setupChild(View child, int position, int x, boolean flowRight, int childTop, boolean selected,
        boolean recycled) {
    final boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make some up...
    // noinspection unchecked
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if ((recycled && !p.forceAdd)
            || (p.recycledHeaderFooter && p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        attachViewToParent(child, flowRight ? -1 : 0, p);
    } else {
        p.forceAdd = false;
        if (p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            p.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowRight ? -1 : 0, p, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            child.setActivated(mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
                mListPadding.top + mListPadding.bottom, p.height);
        int lpWidth = p.width;
        int childWidthSpec;
        if (lpWidth > 0) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childLeft = flowRight ? x : x - w;

    if (needToMeasure) {
        final int childBottom = childTop + h;
        final int childRight = childLeft + w;
        child.layout(childLeft, childTop, childRight, childBottom);
    } else {
        child.offsetTopAndBottom(childTop - child.getTop());
        child.offsetLeftAndRight(childLeft - child.getLeft());
    }

    if (mCachingStarted && !child.isDrawingCacheEnabled()) {
        child.setDrawingCacheEnabled(true);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && recycled
            && (((LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
        Compat.jumpDrawablesToCurrentState(child);
    }
}