fix compoundbutton's checkable,and add tint api support

This commit is contained in:
houzh 2023-08-04 12:55:32 +08:00
parent bfc0681b6f
commit 4ccdf55577
5 changed files with 82 additions and 28 deletions

View File

@ -1538,7 +1538,7 @@ bool AbsListView::canScrollList(int direction) {
}
bool AbsListView::trackMotionScroll(int deltaY, int incrementalDeltaY) {
int childCount = getChildCount();
const int childCount = getChildCount();
if (childCount == 0) {
return true;
}
@ -3765,7 +3765,7 @@ void AbsListView::FlingRunnable::start(int initialVelocity) {
mLV->mTouchMode = TOUCH_MODE_FLING;
mSuppressIdleStateChangeCall = false;
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
if (PROFILE_FLINGING) {
if (!mLV->mFlingProfilingStarted) {
mLV->mFlingProfilingStarted = true;
@ -3781,7 +3781,7 @@ void AbsListView::FlingRunnable::startSpringback() {
if (mScroller->springBack(0, mLV->mScrollY, 0, 0, 0, 0)) {
mLV->mTouchMode = TOUCH_MODE_OVERFLING;
mLV->invalidate();
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
} else {
mLV->mTouchMode = TOUCH_MODE_REST;
mLV->reportScrollStateChange(OnScrollListener::SCROLL_STATE_IDLE);
@ -3794,7 +3794,7 @@ void AbsListView::FlingRunnable::startOverfling(int initialVelocity) {
mLV->mTouchMode = TOUCH_MODE_OVERFLING;
mSuppressIdleStateChangeCall = false;
mLV->invalidate();
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
}
void AbsListView::FlingRunnable::edgeReached(int delta) {
@ -3811,7 +3811,7 @@ void AbsListView::FlingRunnable::edgeReached(int delta) {
mLV->mPositionScroller->stop();
}
mLV->invalidate();
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
}
void AbsListView::FlingRunnable::startScroll(int distance, int duration, bool linear, bool suppressEndFlingStateChangeCall) {
@ -3821,13 +3821,13 @@ void AbsListView::FlingRunnable::startScroll(int distance, int duration, bool li
mScroller->startScroll(0, initialY, 0, distance, duration);
mLV->mTouchMode = TOUCH_MODE_FLING;
mSuppressIdleStateChangeCall = suppressEndFlingStateChangeCall;
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
}
void AbsListView::FlingRunnable::endFling() {
mLV->mTouchMode = TOUCH_MODE_REST;
mLV->removeCallbacks(*this);//mFlingRunnable);
mLV->removeCallbacks(*this);
mLV->removeCallbacks(mCheckFlywheel);
if (!mSuppressIdleStateChangeCall)
@ -3872,7 +3872,7 @@ void AbsListView::FlingRunnable::run() {
View* firstView = mLV->getChildAt(0);
mLV->mMotionViewOriginalTop = firstView->getTop();
// Don't fling more than 1 screen
delta = std::min(mLV->getHeight() - mLV->mPaddingBottom - mLV->mPaddingTop - 1, delta);
delta = std::min(mLV->getHeight() - mLV->mPaddingTop - mLV->mPaddingBottom - 1, delta);
} else {
// List is moving towards the bottom. Use last view as mMotionPosition
int offsetToLast = mLV->getChildCount() - 1;
@ -3880,7 +3880,7 @@ void AbsListView::FlingRunnable::run() {
View* lastView = mLV->getChildAt(offsetToLast);
mLV->mMotionViewOriginalTop = lastView->getTop();
// Don't fling more than 1 screen
delta = std::max(-(mLV->getHeight() - mLV->mPaddingBottom - mLV->mPaddingTop - 1), delta);
delta = std::max(-(mLV->getHeight() - mLV->mPaddingTop - mLV->mPaddingBottom - 1), delta);
}
// Check to see if we have bumped into the scroll limit
@ -3908,7 +3908,7 @@ void AbsListView::FlingRunnable::run() {
if (more && !atEnd) {
if (atEdge) mLV->invalidate();
mLastFlingY = y;
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
} else {
endFling();
if (PROFILE_FLINGING) {
@ -3942,7 +3942,7 @@ void AbsListView::FlingRunnable::run() {
}
} else {
mLV->invalidate();
mLV->postOnAnimation(*this);//mFlingRunnable);
mLV->postOnAnimation(*this);
}
} else {
endFling();

View File

@ -2,13 +2,13 @@
#define __CHECKABLE_H__
#include <core/callbackbase.h>
namespace cdroid{
//#define FUNCTION_AS_CHECKABLE
//#define FUNCTION_AS_CHECKABLE 1
class Checkable{
public:
#ifdef FUNCTION_AS_CHECKABLE
CallbackBase<void,bool>setChecked;//virtual void setChecked(bool checked)=0;
CallbackBase<bool>isChecked;//virtual bool isChecked()const=0;
CallbackBase<void>toggle;//virtual void toggle()=0;
CallbackBase<void,bool>setChecked;
CallbackBase<bool>isChecked;
CallbackBase<void>toggle;
#else
virtual void setChecked(bool checked)=0;
virtual bool isChecked()const=0;

View File

@ -18,25 +18,28 @@ CompoundButton::CompoundButton(const std::string&txt,int width,int height)
}
void CompoundButton::initCompoundButton(){
mChecked=false;
mBroadcasting=false;
mCheckedFromResource=false;
mButtonDrawable=nullptr;
mOnCheckedChangeListener=nullptr;
mOnCheckedChangeWidgetListener=nullptr;
mChecked = false;
mBroadcasting = false;
mCheckedFromResource = false;
mButtonDrawable = nullptr;
mOnCheckedChangeListener = nullptr;
mOnCheckedChangeWidgetListener = nullptr;
mButtonTintMode = TintMode::NONOP;
mButtonTintList = nullptr;
#if FUNCTION_AS_CHECKABLE
isChecked = [this]()->bool{
return mChecked;
};
toggle = [this](){
setChecked(!mChecked);
doSetChecked(!mChecked);
};
setChecked = std::bind(&CompoundButton::doSetChecked,this,std::placeholders::_1);
#endif
}
#ifndef FUNCTION_AS_CHECKABLE
void CompoundButton::setChecked(bool checked){
CompoundButton::doSetChecked(checked);
doSetChecked(checked);
}
bool CompoundButton::isChecked()const{
@ -44,8 +47,9 @@ bool CompoundButton::isChecked()const{
}
void CompoundButton::toggle(){
setChecked(!mChecked);
doSetChecked(!mChecked);
}
#endif
CompoundButton::~CompoundButton(){
delete mButtonDrawable;
@ -73,7 +77,7 @@ void CompoundButton::drawableHotspotChanged(float x,float y){
}
bool CompoundButton::performClick(){
toggle();
doSetChecked(!mChecked);//toggle();
const bool handled = Button::performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
@ -104,7 +108,7 @@ void CompoundButton::setButtonDrawable(Drawable*drawable){
drawable->setVisible(getVisibility() == VISIBLE, false);
setMinHeight(drawable->getIntrinsicHeight());
//applyButtonTint();
applyButtonTint();
}
}
}
@ -122,6 +126,50 @@ void CompoundButton::jumpDrawablesToCurrentState(){
if (mButtonDrawable ) mButtonDrawable->jumpToCurrentState();
}
void CompoundButton::setButtonTintList(ColorStateList* tint) {
mButtonTintList = tint;
applyButtonTint();
}
/**
* @return the tint applied to the button drawable
* @attr ref android.R.styleable#CompoundButton_buttonTint
* @see #setButtonTintList(ColorStateList)
*/
ColorStateList* CompoundButton::getButtonTintList() const{
return mButtonTintList;
}
void CompoundButton::setButtonTintMode(PorterDuffMode tintMode){
mButtonTintMode = tintMode;
//mHasButtonTintMode = true;
applyButtonTint();
}
PorterDuffMode CompoundButton::getButtonTintMode() const {
return (PorterDuffMode)mButtonTintMode;
}
void CompoundButton::applyButtonTint() {
if (mButtonDrawable && (mButtonTintList || mButtonTintMode!=TintMode::NONOP)) {
mButtonDrawable = mButtonDrawable->mutate();
if (mButtonTintList) {
mButtonDrawable->setTintList(mButtonTintList);
}
if (mButtonTintMode!=TintMode::NONOP) {
mButtonDrawable->setTintMode(mButtonTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (mButtonDrawable->isStateful()) {
mButtonDrawable->setState(getDrawableState());
}
}
}
void CompoundButton::doSetChecked(bool checked){
if (mChecked != checked) {
mCheckedFromResource = false;

View File

@ -10,10 +10,13 @@ private:
bool mChecked;
bool mBroadcasting;
bool mCheckedFromResource;
int mButtonTintMode;
Drawable* mButtonDrawable;
ColorStateList*mButtonTintList;
OnCheckedChangeListener mOnCheckedChangeListener;
OnCheckedChangeListener mOnCheckedChangeWidgetListener;
void initCompoundButton();
void applyButtonTint();
protected:
std::vector<int>onCreateDrawableState()override;
int getHorizontalOffsetForDrawables()const override;
@ -30,7 +33,10 @@ public:
bool performClick()override;
Drawable* getButtonDrawable()const;
void jumpDrawablesToCurrentState()override;
void setButtonTintList(ColorStateList* tint);
ColorStateList* getButtonTintList()const;
void setButtonTintMode(PorterDuffMode tintMode);
PorterDuffMode getButtonTintMode()const;
int getCompoundPaddingLeft()override;
int getCompoundPaddingRight()override;
//inerited from Checkable

View File

@ -1538,7 +1538,7 @@ int ListView::lookForSelectablePositionAfter(int current, int position, bool loo
}
void ListView::setSelectionAfterHeaderView() {
int count = getHeaderViewsCount();
const int count = getHeaderViewsCount();
if (count > 0) {
mNextSelectedPosition = 0;
return;