为什么一个进度条的ID必须是唯一的? [英] Why does a ProgressBar's id need to be unique?

查看:195
本文介绍了为什么一个进度条的ID必须是唯一的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布一个空的LinearLayout到我的onCreate方法我称之为有一个循环,重复五次膨胀另一个布局,并将其添加到我的LinearLayout。功能

I declared an empty LinearLayout to which in my onCreate method I call a function that has a loop that repeats five times to inflate another layout and add it to my LinearLayout.

private void setupList() {
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout itemList = (LinearLayout) findViewById( R.id.itemList );
    itemList.removeAllViews();
    for ( Category category :: categories ) {
        View rowView = layoutInflater.inflate(R.layout.category_row, null);
        initializeRow( rowView, category.percentComplete );
        itemList.addView( rowView );
    }
}

然后在我的initializeRow方法我初始化一个TextView和进度是在我刚刚膨胀的看法。

Then in my initializeRow method I initialize a TextView and ProgressBar that are in the view that I just inflated.

private void initializeRow( final View view, final int percentComplete ) {
    TextView topicView = ((TextView) view.findViewById(R.id.topic));
    topicView.setText( category.title );

    ProgressBar progressBar = (ProgressBar) view.findViewById( R.id.progressBar );
    progressBar.setMax( 100 );
    progressBar.setProgress( percentComplete );

    TextView textView = (TextView) view.findViewById( R.id.progressText );
    textView.setText( percentComplete "% Complete" ); 
}

本活动被创建的进度条和文本视图第一次显示有正确的价值观。但是,如果我旋转我的设备,所述TextViews得到显示具有适当的值,但是所有的ProgressBars表明对应于最后进度的进度。为什么这项工作时,得到的onCreate开始而不是当设备旋转后onCreate方法被调用叫什么名字?

The first time this activity gets created the progress bar and text view are displayed with the proper values. However if I rotate my device, the TextViews get displayed with the proper values, but all the ProgressBars show the progress that corresponds to the last ProgressBar. Why does this work when onCreate gets called initially but not when the onCreate method gets called after the device has been rotated?

我认识到,所有的ProgressBars有相同的ID。但在我的code,我用我虚增视图的findViewById获得参照具体进度。我可以通过给每个进度一个唯一的ID通过调用来得到这个工作。

I realize that all the ProgressBars have the same id. But in my code I am getting the reference to the specific ProgressBar by using the findViewById of the view that I inflated. I was able to get this working by giving each ProgressBar a unique id by calling

    progressBar.setId( progressBarIds[ position ] );

在我initializeRow方法。我很好奇,如果这种行为是在进度或者一些bug的结果有大约layoutInflaters或ProgressBars,我有些不明白的规则。

in my initializeRow method. I am curious if this behavior is a result of some bug in ProgressBar or if there is some rule about layoutInflaters or ProgressBars that I do not understand.

推荐答案

的onCreate 之间和旋转事件不同的是,在的onCreate 您secuentially膨胀的只有1个进度的看法。当你调用 view.findViewById(R.id.progressBar); 里只有1,可以发现可能的进度。然后将其值设置为PERCENTCOMPLETE。

The difference between onCreate and the rotation event is that in onCreate you secuentially inflate the view that has only 1 progressbar. When you call view.findViewById( R.id.progressBar ); there's only 1 possible progressbar that can be found. Then you set its value to percentComplete.

当你旋转手机,Android的破坏并创建活动。它还然后尝试恢复的活性的控制/次的值。它保存视图的价值观和IDS的捆绑,然后试图从那里恢复它们。我怀疑,因为所有的progressbars有相同的ID,从包读取值时,有只分配给该ID值。即每一个控制它检查其身份证,并试图发现在包中的相应值。因为他们都有相同的id,所有的人都得到相同的值。

When you rotate the phone, Android destroys and creates the activity. It also then tries to restore the values of the controls/views of the activity. It saves the view's values and ids in a bundle and then tries to restore them from there. I'm suspecting that since all the progressbars have the same id, when reading the value from the bundle there's only value assigned to that id. I.e. for every control it checks its id, and tries to find the corresponding value in the bundle. Since all of them have the same id, all of them get the same value.

认为这是code保存到一个包:包<一个href=\"http://developer.android.com/reference/android/os/Bundle.html#putInt%28java.lang.String,%20int%29\"相对=nofollow> putInt (valueKey,整数),大概所有的progressbars最终有相同的密钥。

Consider that this is the code to save to a bundle: bundle.putInt("valueKey", integer), probably all the progressbars end up having the same key.

但不论价值如何存储,在 ID 是非常想告诉从其他每个进度APPART。如何系统知道哪个是哪个?

But regardless of how the value is stored, the id is the very mean to tell each progressbar appart from the others. How is the system to know which one is which?

由于progressbars的数量取决于类别的数量,你可能将不得不处理的活动周期对自己。可能将实现自己的<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29\"相对=nofollow> 的onSaveInstanceState() 和<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29\"相对=nofollow> onRestoreInstanceState()功能>,这样您可以在每个类别的百分比存储的方式,你可以告诉他们APPART当您需要恢复它们。

Since the number of progressbars is dependant on the number of categories, you'll probably going to have to handle the activity lifecycle on your own. Probably going to implement your own onSaveInstanceState() and onRestoreInstanceState() functions so that you can store each of the category percentages in a way that you can tell them appart when you need to restore them.

这篇关于为什么一个进度条的ID必须是唯一的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆