3

I am trying to simulate a ListView in a LinearLayout in order to show three rows with identical layout. This single View is composed by a ratingbar and a content. It is very strange, but all ratingBars receive the last assigned value. First of all this is my custom Component that extends LinearLayout just adding these two methods:

public void setElements(List<Item> elements) {
    removeAllViews();
    for (int i = 0; i < elements.size() && i < 3; i++) {
        View vi = buildElementView(elements.get(i));
        vi.setId(i);
        addView(vi);
    }
}

private View buildElementView(Item itemElement) {
    View view = inflater.inflate(R.layout.element_list_item, null, true);
    // set values
    View header = view.findViewById(R.id.header);
    RatingBar ratingInItem = (RatingBar) header.findViewById(R.id.ratingBarInItem);
    TextView content = (TextView) view.findViewById(R.id.content);

    content.setText(itemElement.getContent());

    ratingInItem.setRating(itemElement.getRating());
    return view;
}

and this is the layout I am inflating:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.weorder.client"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    android:minHeight="60.0dp"
    android:orientation="vertical"
    android:paddingBottom="8.0dp"
    android:paddingLeft="5.0dp"
    android:paddingRight="5.0dp"
    android:paddingTop="8.0dp" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RatingBar
            android:id="@+id/ratingBarInItem"
            style="@style/RatingBarSm"
            android:isIndicator="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:numStars="5"
            android:stepSize="0.1" />

    </RelativeLayout>

    <TextView
        android:id="@+id/content"
        style="@style/Content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"

        android:textColor="@color/dark_brown"
        android:textSize="@dimen/text_size_small" />

 </LinearLayout>

I call the setElements method inside onActivityCreated() in the fragment. It works well when the fragment start, but when I try to rotate the phone, the content of items changes properly but the ratingbar gets the last value (if there are 3 elements with rating 1,2 and 3, all ratingbars have 3 stars). Is it a bug?

EDIT: this is my onSaveInstanceMethod:

    @Override
public void onSaveInstanceState(Bundle outState) {

    if (elements != null) {
        outState.putSerializable("elements", elements);
    }

    super.onSaveInstanceState(outState);
}

Thanks

4

2 回答 2

0

我认为您正在使用 findViewById() 的错误视图,这会导致问题。

这是一个基于您所做的对我有用的示例。

LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
ViewGroup parentGroup = parentGroup = (ViewGroup)ll;

for (int i = 0; i < 3; i++)
{
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.element_list_item, null, true);
    RatingBar rBar = (RatingBar)view.findViewById(R.id.ratingBar1);
    rBar.setProgress(i);
    parentGroup.addView(view);
}

结果应显示三个评分栏,分别为 0、1 和 2 星,具体取决于您设置评分栏的方式。我将新视图附加到视图父级三次。父级是我的线性布局,新视图是评分栏(或您选择的任何内容)。

为了保存片段使用

@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    getFragmentManager().putFragment(outState, Content.class.getName(), this);
}

然后您可以在 onCreateView 中获取您的参数并恢复您的数据(如有必要)

Bundle extras = this.getArguments();
if(extras != null)
{
    //set arguments here
}
于 2012-04-06T16:14:28.710 回答
0

片段启动时效果很好,但是当我尝试旋转手机时,项目的内容会正确更改,但评分栏会获得最后一个值(如果有 3 个元素的评分为 1,2 和 3,则所有评分栏都有 3 颗星) . 它是一个错误吗?

这不是错误 - 这就是 Android 的工作方式。

为什么会这样?

默认情况下,系统将保存和恢复具有 ID 的视图的状态,并使用这些 ID 来维护状态的内部映射。因为您在 中使用相同的布局,所以LinearLayout每个孩子都有一个RatingBar带有 ID ratingBarInItem。当系统保存视图的状态时,它会保存第一个栏的状态,然后用第二个栏覆盖它,然后用第三个栏覆盖。然后,当您恢复状态时,它会将带有 ID 的每个视图恢复ratingBarInItem到它在状态图中的状态 - 这将是最后保存的值,即第三项的值。

你如何解决这个问题?

你有两个选择。

  1. 自己手动保存和恢复视图的状态。

您表明您已经在保存视图中的“元素”。好吧,您可以更新onRestoreInstanceState以读取这些元素,然后使用它们重新更新子视图(即,setElements再次调用)。

  1. 给每个评分栏一个唯一的 ID。

膨胀子视图以添加到布局后,使用generateViewID创建一个新的唯一 ID,您可以在每个RatingBar.

RatingBar ratingInItem = (RatingBar) header.findViewById(R.id.ratingBarInItem);
ratingInItem.setID(View.generateViewID());

然后您不必实现保存或恢复状态,因为现在每个视图都有一个唯一的 ID,默认的系统行为将起作用。

希望有帮助!

于 2019-07-22T03:25:42.307 回答