下面列出了android.view.View#isSelected ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_pause:
if (v.isSelected()) {
AudioBookPlayService.pause(this);
} else {
AudioBookPlayService.resume(this);
}
break;
case R.id.btn_previous:
AudioBookPlayService.previous(this);
break;
case R.id.btn_next:
AudioBookPlayService.next(this);
break;
case R.id.btn_catalog:
audioChapterPop.showAtLocation(ivBlurCover, Gravity.BOTTOM, 0, 0);
break;
case R.id.btn_timer:
audioTimerPop.showAtLocation(ivBlurCover, Gravity.BOTTOM, 0, 0);
break;
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
int count = mDotLayout.getChildCount();
if (0 == count) {
return;
}
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
int position = manager.findFirstCompletelyVisibleItemPosition() % count;
for (int i = 0; i < count; i++) {
View view = mDotLayout.getChildAt(i);
if (i == position) {
if (!view.isSelected()) {
view.setSelected(true);
}
} else {
if (view.isSelected()) {
view.setSelected(false);
}
}
}
}
}
public void onOptionClick(View view) {
if (view.isSelected()) {
view.setSelected(false);
} else {
view.setSelected(true);
}
}
/**
* 拿到当前选中的view
* 适合单选的时候
*
* @return
*/
public View getSelectedView() {
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
if (view.isSelected()) {
return view;
}
}
return null;
}
/**
* 拿到选中数据
*
* @return
*/
public List<Integer> getSelecteds() {
List<Integer> indexs = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
if (view.isSelected()) {
indexs.add(i);
}
}
return indexs;
}
/**
* 获取 View 是否选中
* @param view {@link View}
* @return {@code true} 选中, {@code false} 非选中
*/
public static boolean isSelected(final View view) {
if (view != null) {
return view.isSelected();
}
return false;
}
static int getViewAttributeFlags(Object content) {
int flags = 0;
if (content instanceof View) {
final View view = (View) content;
if (view.isClickable()) {
flags |= FLAG_VIEW_CLICKABLE;
}
if (view.isLongClickable()) {
flags |= FLAG_VIEW_LONG_CLICKABLE;
}
if (view.isFocusable()) {
flags |= FLAG_VIEW_FOCUSABLE;
}
if (view.isEnabled()) {
flags |= FLAG_VIEW_ENABLED;
}
if (view.isSelected()) {
flags |= FLAG_VIEW_SELECTED;
}
}
return flags;
}
@Override
protected void onMultiClick(View view) {
int position = mViewHolder.getAdapterPosition();
if (position < 0) {
return;
}
int choiceMode = choiceMode();
T selectedData = getItem(position);
if (choiceMode == SINGLE_CHOICE_MODE) {
mSelectedItems.put(position, selectedData);
if (isValidPosition(mLastSelected) && mLastSelected != position) {
mSelectedItems.remove(mLastSelected);
}
notifyItemChanged(mLastSelected);
notifyItemChanged(position);
mLastSelected = position;
} else if (choiceMode == MULTI_CHOICE_MODE) {
boolean isClicked = !view.isSelected();
if (isClicked) {
mSelectedItems.put(position, selectedData);
} else {
mSelectedItems.remove(position);
}
notifyItemChanged(position);
}
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(BaseRecyclerAdapter.this, view, position);
}
}
private void onFabClick(View v) {
int x = (v.getLeft() + v.getRight()) / 2;
int y = (v.getTop() + v.getBottom()) / 2;
float radiusOfFab = 1f * v.getWidth() / 2f;
float radiusFromFabToRoot = (float) Math.hypot(
Math.max(x, rootLayout.getWidth() - x),
Math.max(y, rootLayout.getHeight() - y));
if (v.isSelected()) {
hideMenu(x, y, radiusFromFabToRoot, radiusOfFab);
} else {
showMenu(x, y, radiusOfFab, radiusFromFabToRoot);
}
v.setSelected(!v.isSelected());
}
private void onFabClick(View v) {
if (v.isSelected()) {
hideMenu();
} else {
showMenu();
}
v.setSelected(!v.isSelected());
}
@Override
public void onClick(View v) {
String path = (String) v.getTag();
if (v.isSelected()) {
v.setSelected(false);
clickLists.remove(path);
} else {
v.setSelected(true);
clickLists.add(path);
}
itemClickListener.itemOnClick(clickLists);
}
/**
* Add a view as a child and make sure it is measured (if necessary) and positioned properly.
*
* @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 flowDown
* If true, align left edge to x. If false, align right edge to x.
* @param childrenTop
* Top 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 flowDown, int childrenTop, 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
AbsHListView.LayoutParams p = (AbsHListView.LayoutParams) child.getLayoutParams();
if ( p == null ) {
p = (AbsHListView.LayoutParams) generateDefaultLayoutParams();
}
p.viewType = mAdapter.getItemViewType( position );
if ( ( recycled && !p.forceAdd ) || ( p.recycledHeaderFooter && p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER ) ) {
attachViewToParent( child, flowDown ? -1 : 0, p );
} else {
p.forceAdd = false;
if ( p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER ) {
p.recycledHeaderFooter = true;
}
addViewInLayout( child, flowDown ? -1 : 0, p, true );
}
if ( updateChildSelected ) {
child.setSelected( isSelected );
}
if ( updateChildPressed ) {
child.setPressed( isPressed );
}
if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mCheckStates != null ) {
if ( child instanceof Checkable ) {
( (Checkable) child ).setChecked( mCheckStates.get( position ) );
} else if ( android.os.Build.VERSION.SDK_INT >= 11 ) {
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 = flowDown ? x : x - w;
if ( needToMeasure ) {
final int childBottom = childrenTop + h;
final int childRight = childLeft + w;
child.layout( childLeft, childrenTop, childRight, childBottom );
} else {
child.offsetLeftAndRight( childLeft - child.getLeft() );
child.offsetTopAndBottom( childrenTop - child.getTop() );
}
if ( mCachingStarted && !child.isDrawingCacheEnabled() ) {
child.setDrawingCacheEnabled( true );
}
if( android.os.Build.VERSION.SDK_INT >= 11 ) {
if ( recycled && ( ( (AbsHListView.LayoutParams) child.getLayoutParams() ).scrappedFromPosition ) != position ) {
child.jumpDrawablesToCurrentState();
}
}
}
@Override
public void onClick(View v) {
if (!v.isSelected()) {
setWidgetState(v.getId());
}
}
@Override
public void onClick(View v) {
if (!v.isSelected()) {
setWidgetState(v.getId());
}
}
/**
* Add a view as a child and make sure it is measured (if necessary) and
* positioned properly.
*
* @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 childrenLeft 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 y, boolean flowDown, int childrenLeft,
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 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}
p.viewType = mAdapter.getItemViewType(position);
p.scrappedFromPosition = position;
if ((recycled && !p.forceAdd) || (p.recycledHeaderFooter &&
p.viewType == PLAAdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
attachViewToParent(child, flowDown ? -1 : 0, p);
} else {
p.forceAdd = false;
if (p.viewType == PLAAdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
p.recycledHeaderFooter = true;
}
addViewInLayout(child, flowDown ? -1 : 0, p, true);
}
if (updateChildSelected) {
child.setSelected(isSelected);
}
if (updateChildPressed) {
child.setPressed(isPressed);
}
if (needToMeasure) {
int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
mListPadding.left + mListPadding.right, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight, View.MeasureSpec.EXACTLY);
} else {
childHeightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
onMeasureChild( child, position, childWidthSpec, childHeightSpec );
//child.measure(childWidthSpec, childHeightSpec);
} else {
cleanupLayoutState(child);
}
final int w = child.getMeasuredWidth();
final int h = child.getMeasuredHeight();
final int childTop = flowDown ? y : y - h;
if (needToMeasure) {
final int childRight = childrenLeft + w;
final int childBottom = childTop + h;
//child.layout(childrenLeft, childTop, childRight, childBottom);
onLayoutChild(child, position, childrenLeft, childTop, childRight, childBottom);
} else {
final int offsetLeft = childrenLeft - child.getLeft();
final int offsetTop = childTop - child.getTop();
onOffsetChild(child, position, offsetLeft, offsetTop);
}
if (mCachingStarted && !child.isDrawingCacheEnabled()) {
child.setDrawingCacheEnabled(true);
}
}
private void likeOrUnlike(View view) {
boolean checked = view.isSelected();
view.setSelected(!checked);
likeOrUnlikeShot(!checked);
}
/**
* Add a view as a child and make sure it is measured (if necessary) and
* positioned properly.
*
* @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);
}
}
public void onMuteButtonClick(View view) {
RongCallClient.getInstance().setEnableLocalAudio(view.isSelected());
view.setSelected(!view.isSelected());
muted = view.isSelected();
}
@Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
int i = v.getId();
BasePanelEvent state = null;
if (i == R.id.iv_font) {
if (mFontDatas.size() == 0) {
PanelFactory.createFontPanel(mFontDatas);
mPanelDatas.addAll(mFontDatas);
mPanelAdapter.notifyDataSetChanged();
}
mPanel.setVisibility(v.isSelected() ? VISIBLE : GONE);
state = new FontChangeEvent(v.isSelected());
} else if (i == R.id.iv_link) {
String url = "www.baidu.com";
FontParam param = new FontParam();
param.url = url;
param.fontColor = Color.parseColor("#3194D0");
state = new LinkChangeEvent(v.isSelected(), param, "百度");
} else if (i == R.id.iv_refer) {
state = new ParagraphChangeEvent(v.isSelected());
((ParagraphChangeEvent)state).pType = Const.PARAGRAPH_REFER;
} else if (i == R.id.tv_h1) {
((TextView)v).setTextColor(v.isSelected()? Color.parseColor("#7dc5eb"):Color.parseColor("#bfbfbf"));
state = new ParagraphChangeEvent(v.isSelected());
((ParagraphChangeEvent)state).pType = Const.PARAGRAPH_T1;
} else if (i == R.id.tv_h2) {
((TextView)v).setTextColor(v.isSelected()? Color.parseColor("#7dc5eb"):Color.parseColor("#bfbfbf"));
state = new ParagraphChangeEvent(v.isSelected());
((ParagraphChangeEvent)state).pType = Const.PARAGRAPH_T2;
} else if (i == R.id.tv_h3) {
((TextView)v).setTextColor(v.isSelected()? Color.parseColor("#7dc5eb"):Color.parseColor("#bfbfbf"));
state = new ParagraphChangeEvent(v.isSelected());
((ParagraphChangeEvent)state).pType = Const.PARAGRAPH_T3;
} else if (i == R.id.tv_h4) {
((TextView)v).setTextColor(v.isSelected()? Color.parseColor("#7dc5eb"):Color.parseColor("#bfbfbf"));
state = new ParagraphChangeEvent(v.isSelected());
((ParagraphChangeEvent)state).pType = Const.PARAGRAPH_T4;
}
if (mStateChange != null) {
mStateChange.onStateChanged(state);
}
}
/**
* Add a view as a child and make sure it is measured (if necessary) and
* positioned properly.
*
* @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 childrenLeft 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 y, boolean flowDown, int childrenLeft,
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
PLA_AbsListView.LayoutParams p = (PLA_AbsListView.LayoutParams) child.getLayoutParams();
if (p == null) {
p = new PLA_AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}
p.viewType = mAdapter.getItemViewType(position);
if ((recycled && !p.forceAdd) || (p.recycledHeaderFooter &&
p.viewType == PLA_AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
attachViewToParent(child, flowDown ? -1 : 0, p);
} else {
p.forceAdd = false;
if (p.viewType == PLA_AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
p.recycledHeaderFooter = true;
}
addViewInLayout(child, flowDown ? -1 : 0, p, true);
}
if (updateChildSelected) {
child.setSelected(isSelected);
}
if (updateChildPressed) {
child.setPressed(isPressed);
}
if (needToMeasure) {
int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
mListPadding.left + mListPadding.right, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
onMeasureChild( child, position, childWidthSpec, childHeightSpec );
//child.measure(childWidthSpec, childHeightSpec);
} else {
cleanupLayoutState(child);
}
final int w = child.getMeasuredWidth();
final int h = child.getMeasuredHeight();
final int childTop = flowDown ? y : y - h;
if (needToMeasure) {
final int childRight = childrenLeft + w;
final int childBottom = childTop + h;
//child.layout(childrenLeft, childTop, childRight, childBottom);
onLayoutChild(child, position, childrenLeft, childTop, childRight, childBottom);
} else {
final int offsetLeft = childrenLeft - child.getLeft();
final int offsetTop = childTop - child.getTop();
onOffsetChild(child, position, offsetLeft, offsetTop);
}
if (mCachingStarted && !child.isDrawingCacheEnabled()) {
child.setDrawingCacheEnabled(true);
}
}