簡體   English   中英

在多個展開的布局中獲取對同一視圖的引用

[英]Get a reference to same view in multiple inflated layouts

我試圖找到這個問題的答案,我以為我的代碼可以按我的意願工作,但事實並非如此。

問題:我有一個“父級” LinearLayout,向其中添加了幾個嵌套的膨脹“子級” LinearLayout。 這可行。 每個CHILD布局都有兩個視圖,一個自定義ChipView和一個TextView。 給每個孩子充氣后,我希望能夠在活動期間“隨時隨地”修改每個孩子的ChipView和TextView。

我創建了一個簡單的項目來玩,但是我只能管理訪問FIRST INFLATED子布局的ChipView和TextView。 所有后續的都正確地插入了Parent中,但是顯然我無法獲得變量來引用它們。

我之前是通過在運行時創建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>
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。

每個參考現在都在工作,您將不會總是得到最后一個參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM