java - Horizontalscrollview:从另一个Activity返回时imageView消失
问题描述
我正在开发我的第一个应用程序,但 ImageViews 和 Horizontalscrollview 存在问题:我想收集用户在 Horizontalscrollview 中添加图像所达到的所有目标(达到目标 --> 显示新图像)。
它们显示正确,一切似乎都正常,但是当我打开另一个活动然后我回来时,它们消失了。我试图使布局无效,但什么也没有,我错过了什么吗?
先感谢您!
这是我的 XML 文件
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main3Activity">
<HorizontalScrollView
android:id="@+id/horizontal_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp">
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是我的 Java 代码 我用来检查目标是否完成的函数:
<!-- language: lang-java -->
protected void checkGoal(){
int progress = calculateProgress();
if(goalPref.getInt("Goal",0)!=0){
if(progress >= goalPref.getInt("Goal", 0)) {
int id = layout.getChildCount();
addNewImage(id); // <-- here I add the image
question.setText(R.string.setgoal);
updateGoalPref(0);
if (list.size() != 0) {
list = new ArrayList<>();
}
updateProgress();
}
}
}
我用来添加图像的功能:
<!-- language: lang-java -->
protected void addNewImage(int id){
ImageView imageView = new ImageView(this);
imageView.setId(id);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundResource(R.drawable.check);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
layout.addView(imageView, id);
}
解决方案
由于我很固执,我设法找到了另一种方法,这就是我所做的:使用我发布的代码,我添加了一个计数器来跟踪用户达到了多少目标,并onResume()
显示尽可能多的图像计数器值。
这样,用户可以立即看到达到目标的效果,并且活动不会丢失任何内容。
然后,我实现了它来显示目标的描述,用这些图像作为背景更改 TextViews 中的 ImageViews。我没有使用计数器,而是将要显示的字符串保存在 ArrayList 中,并将其大小用作计数器。
希望它对某人有用!
<!-- language: lang-java -->
protected void checkGoal(){
int progress = calculateProgress();
if(goalPref.getInt("Goal",0)!=0){
if(progress >= goalPref.getInt("Goal", 0)) {
String newString = goalPref.getInt("Goal", 0) + "€, " + MainActivity.currentDate();
goalsCompleted.add(newString);
updateGoalCompletedList();
//Here I clean the layout and display all the TextViews
layout.removeAllViews();
for(int i=goalsCompleted.size()-1; i>0; i--){
addNewTextView(i);
}
question.setText(R.string.setgoal);
updateGoalPref(0);
if (list.size() != 0) {
list = new ArrayList<>();
}
updateProgress();
}
}
}
//The function used to display the TextViews
protected void addNewTextView(int id){
TextView textView = new TextView(this);
textView.setBackgroundResource(R.drawable.check1);
textView.setId(id);
textView.setPadding(25,260,0,0);
textView.setTextSize(12);
textView.setText(goalsCompleted.get(id));
layout.addView(textView);
}
//The function used to update and save the array
public void updateGoalCompletedList(){
Gson gson3 = new Gson();
jsonItems3 = gson3.toJson(goalsCompleted);
SharedPreferences.Editor listEd = goalsCompletedPref.edit();
listEd.putString("GoalsCompleted", jsonItems3).apply();
}
//The function used to resume the array, you can call it on onResume() or onCreate()
public void resumeGoalsCompleted(){
Gson gson3 = new Gson();
String savedList = goalsCompletedPref.getString("GoalsCompleted", "");
goalsCompleted = gson3.fromJson(savedList, new TypeToken<ArrayList<String>>() {
}.getType());
if(goalsCompleted==null)
goalsCompleted = new ArrayList<>();
//From the most recent to the least recent
for(int i=goalsCompleted.size()-1; i>0; i--){
addNewTextView(i);
}
}
推荐阅读
- exchangewebservices - 尽管被 API 成功接受,通过 EWS API 写入的事件是否不能发送到邮箱?
- python - 如何将文本文件转换为要在 Python 中使用的字典?
- spring - 在 @PreAuthorize 中使用请求标头值
- architecture - 如何找出地址总线宽度有多宽?
- javascript - 如果单击提交按钮时字段未填写信息,则前端输入字段警告消息
- php - 管理面板页面阻止
- html - 为什么我的引导卡长度不一致?
- keras - InvalidArgumentError: indices[13,9] = 197491 不在 [0, 189722) [[nodesequential_7
- mysql - MariaDB 按位与十六进制字符串文字
- python - TypeError:product() 得到了一个意外的关键字参数“product_id”