动态向LinearLayout添加视图,但只有一个没有设置文本

3
我有一个线性布局,希望能够动态添加变量数量的列表项。(不能使用Recycler View,因为这个列表已经在Recycler View中了,且假设这是一个小列表。)但是出现了一个问题,除了最后一项之外,文本没有保存我设置的内容。如果我添加另一个标题不同的项目,那么最后添加的那个将会显示,其他的则会显示“默认”。有谁知道这是怎么回事吗?这真的很奇怪。谢谢!
以下是代码:
settings_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:text="Default"
        android:layout_weight="1"/>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end|center_vertical"
        android:layout_weight="0"/>

</LinearLayout>

root_settings_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginEnd="5dp">
</LinearLayout>

SettingsView.java:

public class SettingsView extends LinearLayout {

    //Views
    private View view;

    /**
     * Constructor for android tools to use.
     */
    public SettingsView(Context context) {
        super(context);
    }

    /**
     *
     * @param parent
     */
    public SettingsView(ViewGroup parent) {
        super(parent.getContext());

        //Get the inflated layout
        LinearLayout view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);


        View itemView = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView.findViewById(R.id.title)).setText("Hi");
        ((CheckBox) itemView.findViewById(R.id.checkbox)).setChecked(true);


        View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
        ((CheckBox) itemView2.findViewById(R.id.checkbox)).setChecked(false);

        View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
        ((CheckBox) itemView3.findViewById(R.id.checkbox)).setChecked(true);

        View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView4.findViewById(R.id.title)).setText("Test");
        ((CheckBox) itemView4.findViewById(R.id.checkbox)).setChecked(false);

        addView(view);
    }

Result


1
你不能在为每个项目充气时使用相同的ID,你需要做的是不要充气,而是将列表项addView到父view - Rod_Algonquin
2个回答

3

感谢 @Rod_Algonquin 的评论,让我意识到我必须为此创建一个小部件。幸运的是,我以前做过小部件,所以知道如何做。我只是试图避免这样做,但我认为如果我们不使用小部件,它会出现重复ID的问题。

以下代码使其正常工作:

添加新类 SettingItemView.java:

/**
 * A item view with the title and a checkbox.
 */
public class SettingItemView extends LinearLayout {

    private TextView mTitle;
    private CheckBox mCheckBox;

    public SettingItemView(Context context) {
        super(context);
        inflate(context, R.layout.settings_item, this);
        mTitle = (TextView) findViewById(R.id.title);
        mCheckBox = (CheckBox) findViewById(R.id.checkbox);
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public void setCheckbox(boolean checked) {
        mCheckBox.setChecked(checked);
    }
}

修改了这个构造方法:

/**
 *
 * @param parent
 */
public SettingsView(ViewGroup parent) {
        super(parent.getContext());

        //Get the inflated layout.
        LinearLayout view = LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);


        SettingItemView itemView = new SettingItemView(parent.getContext());
        itemView.setTitle("Hi");
        itemView.setCheckbox(true);
        view.addView(itemView);

        SettingItemView itemView2 = new SettingItemView(parent.getContext());
        itemView2.setTitle("Hello");
        itemView2.setCheckbox(true);
        view.addView(itemView2);

        SettingItemView itemView3 = new SettingItemView(parent.getContext());
        itemView3.setTitle("Bye");
        itemView3.setCheckbox(true);
        view.addView(itemView3);

        addView(view);
    }

1
你基本上是在遍历它并只添加最后一个,这段代码应该可以工作: public SettingsView(ViewGroup parent) { super(parent.getContext());
    //Get the inflated layout
    LinearLayout view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);


    View itemView = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
    ((TextView) itemView.findViewById(R.id.title)).setText("Hi");
    ((CheckBox) itemView.findViewById(R.id.checkbox)).setChecked(true);
    addView(view);
    View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
    ((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
    ((CheckBox) itemView2.findViewById(R.id.checkbox)).setChecked(false);
    addView(view);
    View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
    ((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
    ((CheckBox) itemView3.findViewById(R.id.checkbox)).setChecked(true);
    addView(view);
    View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
    ((TextView) itemView4.findViewById(R.id.title)).setText("Test");
    ((CheckBox) itemView4.findViewById(R.id.checkbox)).setChecked(false);

    addView(view);

这在LogCat中给了我无限递归,一遍又一遍地显示这行代码:at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6713) - Rock Lee
我也有同样的想法,除了addView(itemView)等。然而,这并不能解释为什么会显示任何内容 - 在我看来,只有root_settings_view被添加到ViewGroup中。 - Micha
我认为yanivtwin误解了我的代码,因为实际上我是将根视图(其中我填充了4个项目视图)添加到父LinearLayout中,而不是添加任何项目。它们只是通过填充方法添加的。当我尝试手动添加项目时,它给了我一个递归问题。 - Rock Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接