【发布时间】:2019-11-11 12:18:03
【问题描述】:
我正在开发我的第一个应用程序,但我遇到了 ImageViews 和 Horizontalscrollview 的问题:我想收集用户在 Horizontalscrollview 中添加图像所达到的所有目标(达到目标 --> 显示新图像)。
how 4 completed goals are displayed
它们正确显示,一切似乎都正常,但是当我打开另一个活动然后我回来时,它们消失了。我试图使布局无效,但什么也没有,我错过了什么吗?
提前感谢您!
这是我的 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);
}
【问题讨论】:
-
你好,你为什么不用recyclerview来代替呢?
-
嗨 Miriana,谢谢你的建议,但我选择的 API 级别不支持它,所以我试图找出另一种方法。
-
我能知道你选择了什么API级别吗?因为据我所知,recyclerview 是向后兼容的
-
API 级别 24
-
是的,你是对的,我找到了!非常感谢
标签: java android imageview android-linearlayout horizontalscrollview