Tab*_*ter 2 java xml android visibility parse-platform
我有一个试图使其可见的布局,但目前无法正常工作。我要使其可见的布局下面具有ID“ goal_reminder”。可见性在xml中设置为“ GONE”。
这是XML
<?xml version="1.0" encoding="utf-8"?>
<com.View.pages.ActivityPage
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/activity_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/activitylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e7e6ee"
android:clipToPadding="false"
android:divider="@null"
android:paddingBottom="80dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<RelativeLayout
android:id="@+id/goal_reminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent_color"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:layout_width="match_parent"
android:layout_height="145dp"
android:src="@drawable/sunsetforgoal" />
<ImageView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/goal_reminder_title"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="35dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/goal_reminder_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="No goal in progress"
android:textColor="@color/text_color"
android:textSize="26sp" />
<TextView
android:id="@+id/goal_reminder_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/goal_reminder_title"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="Create a new goal in your \nprofile."
android:textColor="@color/text_color"
android:textSize="18sp" />
</RelativeLayout>
<TextView
android:id="@+id/playground_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:autoLink="all"
android:gravity="center_horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="@string/welcome_string"
android:textSize="18sp"
android:visibility="gone" />
</com.View.pages.ActivityPage>
Run Code Online (Sandbox Code Playgroud)
这是onFinishInflate(我使用的是Screenplay / Flow库,因此已取代onCreate)。如您所见,我正在尝试设置GoalReminderLayout.setVisibility(View.Visible),但实际上并没有使其可见。我已经在if语句之外测试了同一行代码,并且工作正常。我还进行了测试,以确保它已到达if语句中的那一行代码,并且该部分工作正常,lastTriggerDate已正确保存在Parse中。我不知道为什么它在onFinishInflate中的if语句之外仍然可以正常工作。我还用Log.d(“ TestVisiblity”,GoalReminderLayout.getVisibility())进行了测试;它返回0(可见),因此看起来像可见的,但实际上并未显示在屏幕上。
Date lastTriggerDate = new Date();
Boolean noDateInParse = false;
if (currentUser.getLastTriggerDate() != null) {
lastTriggerDate = currentUser.getLastTriggerDate();
} else {
noDateInParse = true;
}
Boolean inToday = DateUtils.isToday(lastTriggerDate.getTime());
if (!inToday) {
currentUser.setLastTriggerDate(currentTime);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
goalReminderLayout.setVisibility(View.VISIBLE);
fireGoalReminderAlert();
} else {
e.printStackTrace();
}
}
});
} else if (noDateInParse) {
currentUser.setLastTriggerDate(currentTime);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
goalReminderLayout.setVisibility(View.VISIBLE);
fireGoalReminderAlert();
} else {
e.printStackTrace();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是fireGoalReminderAlert()的代码;只是将可见性设置为在10秒后消失。我在测试时对这一行进行了评论,但仍然没有运气,因此我认为这不是造成此问题的原因。
public void fireGoalReminderAlert() {
Runnable mRunnable;
Handler mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
goalReminderLayout.setVisibility(View.GONE);
}
};
mHandler.postDelayed(mRunnable, 10 * 1000);
}
Run Code Online (Sandbox Code Playgroud)
将布局设置为不可见时,最好使视图无效以允许在布局上重绘。
GoalReminderLayout.setVisibility(View.VISIBLE);
GoalReminderLayout.invalidate();
查看android文档了解更多信息
http://developer.android.com/reference/android/view/View.html