【问题标题】:Get a reference to same view in multiple inflated layouts在多个膨胀布局中获取对同一视图的引用
【发布时间】:2012-07-02 22:10:11
【问题描述】:

我试图找到这个问题的答案,并且我认为我的代码可以按我的意愿工作,但是......它不是。

问题:我有一个“父”LinearLayout,我向其中添加了几个嵌套的膨胀“子”LinearLayout。这行得通。 每个 CHILD 布局都有两个视图,一个自定义 ChipView 和一个 TextView。在我为每个孩子充气后,我希望能够在我的活动期间“随时”修改每个孩子的 ChipView 和 TextView。

我创建了一个简单的项目来玩,但我只能设法访问 FIRST INFLATED 子布局的 ChipView 和 TextView。所有后续的都正确插入到父级中,但显然我无法获取变量来引用它们。

我之前是通过在运行时创建 ChipView 来做到这一点的,它完美地工作,但我想要一种更优雅的方法,使用我可以单独控制的 XML。

在我的活动中,我有一个按钮可以创建子项,还有一个按钮应该调用当前 ChipView 中的方法(即最后一次充气或我点击的那个)。

活动:

public class SandboxActivity extends Activity {
    private Button okbtn;
    private Button add;
    private EditText count;
    private ChipsView chips;
    private LinearLayout pots;
    private TextView amount;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

       okbtn = (Button) findViewById(R.id.ok); //calls the method in customviews
       add = (Button) findViewById(R.id.add);  //adds a child
       count = (EditText) findViewById(R.id.count); //the value to call methods with
       pots = (LinearLayout) findViewById(R.id.pots); //the PARENT layout

       add.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.pottemplate, pots);
            chips = (ChipsView) ll.findViewById(R.id.chips);
            amount = (TextView) ll.findViewById(R.id.amount);

               //this should allow me to set the activity.chips variable to the last clicked custom view
            chips.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    chips = (ChipsView) v;
                }
            });         
            }

        });


        okbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                chips.setCount(Double.parseDouble(count.getText().toString()));         
            }

        });

    }
}

膨胀的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.ded.sandbox.ChipsView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:id="@+id/chips">    

    </com.ded.sandbox.ChipsView>

    <TextView
        android:id="@+id/amount"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="TextView" android:gravity="center_horizontal" android:textSize="12dp" android:textStyle="bold"/>

</LinearLayout>

【问题讨论】:

  • 为了更清楚,我认为问题在于无论变量芯片和数量总是指向 FIRST CHILD 布局中的视图,即使我在每次膨胀后调用 ll.findViewById()。
  • 我想通了。而不是 LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.pottemplate, pots);我叫:LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.pottemplate, null);后来 pots.addView(ll);

标签: android layout view reference inflate


【解决方案1】:
View oneHappyRow = mInflater.inflate(R.layout.one_happy_hour_row,null);
TextView dayOfWeekTextView = (TextView) oneHappyRow.findViewById(R.id.dayOfWeekTextView);
TextView hoursOfDayTextView = (TextView) oneHappyRow.findViewById(R.id.hoursOfDayTextView);

在此处获取每个膨胀布局的参考,然后设置文本等:

this.addView(oneHappyRow);

在我的例子中,该类扩展了 LinearLayout。

每个参考现在都在工作,你不会总是得到最后一个参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多