【问题标题】:Horizontalscrollview: imageView disappear when coming back from another ActivityHorizo​​ntalscrollview:从另一个Activity返回时imageView消失
【发布时间】:2019-11-11 12:18:03
【问题描述】:

我正在开发我的第一个应用程序,但我遇到了 ImageViews 和 Horizo​​ntalscrollview 的问题:我想收集用户在 Horizo​​ntalscrollview 中添加图像所达到的所有目标(达到目标 --> 显示新图像)。

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


【解决方案1】:

当你去另一个活动并回来时,你应该在 onResume 中调用addNewImage 再次显示图像:

@Override
    protected void onResume() {
        super.onResume();
        addNewImage(id);
    }  

如果活动中的目标数量有限,为什么不在 XML 中创建这些 ImageView 并在 java 中设置它们的 Visibility?

【讨论】:

  • 这主意不错但目标不限,addNewImage(id) 一个一个添加
【解决方案2】:

由于我很固执,我设法找到了另一种方法来做到这一点,这就是我所做的:使用我发布的代码我添加了一个计数器来跟踪用户达到了多少目标和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);
        }
    }

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2014-05-31
    • 1970-01-01
    相关资源
    最近更新 更多