我试图在linearlayout中模拟一个listview,以显示具有相同布局的三行。这个视图由一个分级栏和一个内容组成。这很奇怪,但是所有的ratingbar都会收到最后一个赋值。首先,这是我的自定义组件,它扩展了linearlayout,只需添加以下两种方法:

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;
}

这是我正在膨胀的布局:
    <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>

我在片段的onActivityCreated()中调用setElements方法。片段启动时效果很好,但当我尝试旋转手机时,项目内容会正确更改,但ratingbar会得到最后一个值(如果有3个元素的评级分别为1、2和3,则所有ratingbar都有3个星)。是虫子吗?
编辑:这是我的onSaveInstanceMethod:
    @Override
public void onSaveInstanceState(Bundle outState) {

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

    super.onSaveInstanceState(outState);
}

谢谢

最佳答案

我认为您使用的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
}

07-26 09:42
查看更多