大家好我想在焦点更改时在片段中使用可折叠/可扩展的EditText创建搜索选项。以下是我班级的样子:
@ViewById(R.id.search_text)
EditText mSearchText;
@ViewById(R.id.img_clear)
ImageButton mClearButton;
@ViewById(R.id.search_toolbar_text_container)
protected RelativeLayout editTextContainer;
SearchToolbarListener mListener;
private ValueAnimator mAnimator;
@Override
public void onFocusChange(View v, boolean hasFocus) {
//the event here never gets executed
if (mListener != null) {
mListener.onFocusChange(hasFocus);
}
mAnimator = slideAnimator(0,editTextContainer.getHeight());
if (hasFocus && editTextContainer.getVisibility() == View.GONE) {
expandSearch();
} else {
collapseSearch();
}
}
public interface SearchToolbarListener {
void onTextChanged(String newText);
void onFocusChange(boolean isFocused);
}
public SearchToolbarView(Context context) {
super(context);
}
public SearchToolbarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchToolbarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SearchToolbarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@AfterViews
public void init() {...}
public void setListener(@Nullable SearchToolbarListener mListener) {
this.mListener = mListener;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
mClearButton.setVisibility(charSequence.length() >= 1 ? View.VISIBLE : View.INVISIBLE);
if (mListener != null) {
mListener.onTextChanged(charSequence.toString());
}
}
@Override
public void afterTextChanged(Editable editable) {
}
private void expandSearch () {
editTextContainer.setVisibility(VISIBLE);
mAnimator.start();
}
private void collapseSearch () {
int finalHeight = editTextContainer.getHeight();
mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
// Height=0, but it set visibility to GONE
editTextContainer.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
mAnimator.start();
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// Update Height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = editTextContainer
.getLayoutParams();
layoutParams.height = value;
editTextContainer.setLayoutParams(layoutParams);
}
});
return animator;
}
}
然后我在一个像这样的片段中实现接口:
@EFragment(R.layout.fragment_product_search)
public class SearchableSearchFragment extends Fragment implements SearchToolbarView.SearchToolbarListener {
private static final String TAG = SearchableSearchFragment.class.getSimpleName();
@SystemService
protected LayoutInflater mLayoutInflater;
@ViewById(R.id.search_toolbar)
protected Toolbar mSearchToolbar;
protected SearchToolbarView mSearchToolbarView;
@Bean
protected ViewManager mViewManager;
private SearchFragment mSearchFragment;
@Bean
protected BottomNavigationHandler mBottomNavigationHandler;
@AfterViews
public void initAfterViews() {
mSearchToolbarView = SearchToolbarView_.build(getContext());
}
@Override
public void onTextChanged(String newText) {
boolean isNumber = NumberUtils.isNumber(newText);
if ((newText.length() >= 3 && !isNumber) || (isNumber && newText.length() == 7)) {
this.setToolbarElevation(0);
mSearchFragment.setSearchQuery(newText);
} else {
float elevationInPx = getResources().getDimension(R.dimen.search_bar_elevation);
this.setToolbarElevation(elevationInPx);
mSearchFragment.clear();
}
}
@Override
public void onFocusChange(boolean isFocused) {
//gets called only once
boolean ggwp = isFocused;
}
此处的问题是焦点更改侦听器无法正常工作。片段启动时仅触发一次。点击左右也不会触发它。我做错了什么?