我必须将一些文本视图的可见性转移到其他一些文本视图,以及其他一些文本视图的可见性,并且我试图为此设置动画。
这是我写过的代码:
public class SearchBookTest extends AppCompatActivity {
private final static int INTRO_VIEW = 0;
private final static int OFFLINE_VIEW = 1;
private boolean[] currentView = {true, false};
private FloatingSearchView mSearchView;
private TextView mIntroTextViewTop;
private TextView mIntroTextViewBottom;
private TextView mNoConnectionTextViewTop;
private TextView mNoConnectionTextViewBottom;
private AppCompatButton mNoConnectionButton;
private int mShortAnimationDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_test);
mSearchView = findViewById(R.id.floating_search_view);
mIntroTextViewTop = findViewById(R.id.search_book_intro_top);
mIntroTextViewBottom = findViewById(R.id.search_book_intro_bottom);
mNoConnectionTextViewTop = findViewById(R.id.search_book_no_connection_top);
mNoConnectionTextViewBottom = findViewById(R.id.search_book_no_connection_bottom);
mNoConnectionButton = findViewById(R.id.search_book_no_connection_try_again);
mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
setupQueryChangeListener();
}
private void setupQueryChangeListener() {
mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
@Override
public void onSearchTextChanged(String oldQuery, String newQuery) {
switch (newQuery.length()) {
case 1:
showIntroView();
break;
case 2:
showOfflineView();
break;
default:
break;
}
}
});
}
private void showOfflineView() {
if (currentView[INTRO_VIEW]) {
currentView[INTRO_VIEW] = false;
fadeOutViews(mIntroTextViewTop, mIntroTextViewBottom, null, mNoConnectionTextViewTop, mNoConnectionTextViewBottom, mNoConnectionButton);
}
currentView[OFFLINE_VIEW] = true;
}
private void showIntroView() {
if (currentView[OFFLINE_VIEW]) {
currentView[OFFLINE_VIEW] = false;
fadeOutViews(mNoConnectionTextViewTop, mNoConnectionTextViewBottom, mNoConnectionButton, mIntroTextViewTop, mIntroTextViewBottom, null);
}
currentView[INTRO_VIEW] = true;
}
private void fadeOutViews(final View vOut1, final View vOut2, final View vOut3, final View vIn1, final View vIn2, final View vIn3) {
vOut1.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
if (vOut2 != null) {
vOut2.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
vOut2.setVisibility(View.GONE);
if (vOut3 == null)
fadeInViews(vIn1, vIn2, vIn3);
}
});
}
if (vOut3 != null) {
vOut3.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
vOut3.setVisibility(View.GONE);
fadeInViews(vIn1, vIn2, vIn3);
}
});
}
}
@Override
public void onAnimationEnd(Animator animation) {
vOut1.setVisibility(View.GONE);
if (vOut2 == null)
fadeInViews(vIn1, vIn2, vIn3);
}
});
}
private void fadeInViews(final View v1, final View v2, final View v3) {
v1.setAlpha(0f);
v1.setVisibility(View.VISIBLE);
v1.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
if (v2 != null) {
v2.setAlpha(0f);
v2.setVisibility(View.VISIBLE);
v2.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
if (v3 != null) {
v3.setAlpha(0f);
v3.setVisibility(View.VISIBLE);
v3.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration);
}
}
});
}
}
});
}
}
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/grey"
tools:context=".ui.searchbooks.SearchBookActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/search_book_intro_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:text="@string/search_intro_top"
android:textColor="@color/grayed_button"
android:fontFamily="sans-serif-light"
android:textSize="22sp"/>
<TextView
android:id="@+id/search_book_intro_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:text="@string/search_intro_botttom"
android:textColor="@color/grayed_button"
android:fontFamily="sans-serif-light"
android:textSize="16sp"/>
<TextView
android:id="@+id/search_book_no_connection_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:text="@string/search_no_connection_1"
android:textColor="@color/black"
android:fontFamily="sans-serif-bold"
android:textSize="18sp"
android:visibility="gone"/>
<TextView
android:id="@+id/search_book_no_connection_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="@string/search_no_connection_2"
android:textColor="@color/black"
android:fontFamily="sans-serif-light"
android:textSize="14sp"
android:visibility="gone"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/search_book_no_connection_try_again"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginTop="20dp"
android:elevation="2dp"
android:text="@string/try_again"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:background="@color/colorSecondaryAccent"
android:visibility="gone"/>
</LinearLayout>
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/floating_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:floatingSearch_close_search_on_keyboard_dismiss="true"
app:floatingSearch_dismissOnOutsideTouch="true"
app:floatingSearch_leftActionMode="showHome"
app:floatingSearch_menu="@menu/menu_search_books"
app:floatingSearch_dimBackground="false"
app:floatingSearch_searchBarMarginLeft="5dp"
app:floatingSearch_searchBarMarginRight="5dp"
app:floatingSearch_searchBarMarginTop="5dp"
app:floatingSearch_searchHint="@string/search_books_hint"
app:floatingSearch_showSearchKey="true"/>
</RelativeLayout>
第一次写第二个字符时,视图切换正常,使得3&#34; noConnection&#34;观看动画并显示正常
然后我删除了一个字符,并且介绍视图再次显示正常(在没有连接视图消失之后),但是当我再次写入第二个字符时,视图将变得混乱,使得整个介绍视图与无连接视图一起显示(按钮除外)。
我发现了什么问题,但我无法理解为什么会发生这种情况:
第二次写第二个字符时#34; vOut3&#34;变量不为null,但包含mNoConnectionButton,使其消失并在两个介绍视图中调用fadeInView。我试图记录值,vOut3为null,但它仍然在if语句中输入!
有人可以帮我摆脱这个烂摊子吗?
答案 0 :(得分:1)
您可以使用Transition
框架来获得所需的结果。以下代码适用于API级别19+。
基本上,您致电TransitionManager.beginDelayedTransition(sceneRoot, transitionSet);
,然后立即设置View
的所需状态。
sceneRoot应该是ViewGroup
,其中包含要转换的所有View
个。在你的情况下,我认为LinearLayout
是一个不错的选择。所以我让它有一个属性android:id="@+id/sceneRoot"
并在private ViewGroup sceneRoot;
中声明了变量Activity
,并在LinearLayout
中为onCreate()
分配了Transition
。
对于TransitionSet
我使用的Fade
包含ChangeBounds
转换以及View.VISIBLE
转换,因为您更改了Alpha值以及整体布局(在View.GONE
和private void fadeOutViews(final View vOut1, final View vOut2, final View vOut3, final View vIn1, final View vIn2, final View vIn3) {
// if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)return;
TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(new Fade());
transitionSet.addTransition(new ChangeBounds());
TransitionManager.beginDelayedTransition(sceneRoot, transitionSet);
vOut1.setVisibility(View.GONE);
if(vOut2 != null) {
vOut2.setVisibility(View.GONE);
}
if(vOut3 != null) {
vOut3.setVisibility(View.GONE);
}
vIn1.setVisibility(View.VISIBLE);
if(vIn2 != null) {
vIn2.setVisibility(View.VISIBLE);
}
if(vIn3 != null) {
vIn3.setVisibility(View.VISIBLE);
}
}
之间交换。
bundle exec rails server