【问题标题】:Wrong view added to scrollview错误的视图添加到滚动视图
【发布时间】:2014-03-06 23:08:48
【问题描述】:

我正在尝试设置此活动“ChoiceList”,以便每次单击保存按钮时,都会在 ChoiceList 活动的 scrollView 内的 linearLayout 中添加一个新视图。添加的视图“choiceView”应该只是一个包含在 frameLayout 中的 textView。然而,出于某种原因,每次单击保存按钮时,都会将一个新的 ChoiceList 布局添加到 linearLayout 而不是 ChoiceView。我是一个业余的android开发者,刚开始自学如何开发,所以我不知道问题出在哪里。

这是我的 java 和 xml 文件:

public class ChoiceList extends android.app.Activity implements android.view.View.OnClickListener {

    private ArrayList<ChoiceView> choiceList;

    private Button button;
    private LinearLayout choiceListLayout;
    private ViewGroup parentGroup;

    public void onCreate(Bundle savedInstanceState) {//, List<ChoiceView> list) {
        super.onCreate(savedInstanceState);
        choiceList = new ArrayList<ChoiceView>();
        this.getActionBar().setTitle("");
        this.getActionBar().setDisplayHomeAsUpEnabled(true); // adds a left-facing caret alongside the app icon and enables it as an action button
                                                             // such that when the user presses it, the activity receives a call to
                                                             // onOptionsItemSelected(). The ID for the action is android.R.id.home.
        this.initLayout();
    }

    public void initLayout() {
        this.setContentView(R.layout.activity_choice_list); // set up initial layout
        choiceListLayout = (LinearLayout) findViewById(R.id.choiceListLayout);
        parentGroup = (ViewGroup) choiceListLayout;

        button = (android.widget.Button) findViewById(R.id.saveButton);
        button.setOnClickListener(new android.view.View.OnClickListener() { // when "Save" button is clicked

            @Override
            public void onClick(View v) {
                newChoiceView(); // Makes new ChoiceView with text from EditText
            }
        });
    }

    /**
     * Makes and displays new ChoiceView with text from EditText
     * Called within InitLayout from clicking Save button
     */
    public void newChoiceView() {
        ChoiceView cv = new ChoiceView(this.getApplicationContext());
        choiceList.add(cv); // add ChoiceView to list of ChoiceViews

        choiceListLayout.addView(cv); // display ChoiceView
    }
    ...
}

以及相关的xml...

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

    <ScrollView
        android:id="@+id/choiceScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/choiceListLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>

            <EditText
                android:id="@+id/enterChoice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/enterChoiceText" />

            <Button
                android:id="@+id/saveButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/save"
                android:textColor="@color/white" />
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/spinButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/spin" />

</RelativeLayout>

以及我尝试添加到滚动条的视图...

public class ChoiceView extends RelativeLayout {

    protected TextView tv;
    private String choiceString; // choice text
    private EditText et; // editText from which text comes from

    /**
     * @param context
     */
    public ChoiceView(Context context) {
        super(context);

        LayoutInflater choiceViewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View choiceView = choiceViewInflater.inflate(R.layout.choice_view, this, true);

        LayoutInflater choiceListInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View choiceList = choiceListInflater.inflate(R.layout.activity_choice_list, this, true);

        et = (EditText) choiceList.findViewById(R.id.enterChoice); // finds EditText from which string heeds
        tv = (TextView) choiceView.findViewById(R.id.choiceViewTextView); // finds TextView to display text
        tv.setText(et.getText().toString());

        requestLayout();
    }
}

及其关联的xml...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/whatever" >

    <TextView
        android:id="@+id/choiceViewTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/spin"
        android:padding="5dp" />

</FrameLayout>

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 你为什么要在你的 ChoiceView 类中增加一个choiceList 布局?
  • 老实说,我在 ChoiceView 类中为 ChoiceList 布局添加了一个充气器,因为有人告诉我需要这样做才能获得 editText 信息。这是一个糟糕的提示吗?
  • 抱歉回复晚了。是的,因为当你膨胀它时,它会呈现一个全新的选择列表布局,它与原始选择列表完全分开。这就是你遇到问题的原因。尽管如此,您可能应该像其他人所说的那样使用 ListView,因为它更加优化。尽管在某些情况下您可能希望这样做。
  • 这是有道理的。但是在不增加布局的情况下,如何找到 EditText id?如果我摆脱 ChoiceList 布局充气器,我的 EditText 变为空(请参阅我以前的论坛问题)。
  • 修改 ChoiceView 的构造函数以将 EditText 作为参数。然后,在 ChoiceList 类中,使用 EditText et = (EditText) findViewById(R.id.enterChoice); 并在创建时将其传递给任何选择视图。

标签: android android-linearlayout


【解决方案1】:

您似乎正在尝试将元素动态添加到可滚动列表中,然后可以选择该列表。

Android 已经提供了一个 UI 组件来执行此操作,ListView,它由一个适配器支持,该适配器将您的模型(即数据)与 UI 连接起来。在这种情况下,您的模型是字符串列表。适配器负责根据需要创建视图(在本例中为 TextView)。 Check out this excellent tutorial.

【讨论】:

  • 感谢您的回复。我实际上知道 ListView 存在,我想我只是认为必须添加一个适配器和所有这些都是多余的,所以我试图将它硬编码到主类中。我会将其更改为 ListView ,因为这似乎是一种愚蠢的方式,但出于好奇,你是不是我的代码哪里出错了?在我看来,使用 ScrollView 应该仍然可行。
  • 抱歉耽搁了——我想你在上面得到了答案,对吧?无论如何,ListView + Adapter 是正确的方法,因为 Android 会自动重用布局以节省膨胀/对象创建的开销。只需确保检查并查看适配器的 getView 方法的“covertView”是否不为空,如果是,请使用它而不是自己充气。您可以使用此方法获得(实际上)无限数量的行。
  • 明白了。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 2016-08-18
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多