简体   繁体   中英

How to access inflated views in a linear layout

I use layout inflater, to inflate a linear layout with some buttons and some textviews in it. How can i access the widgets in the inflated layout?

For example : I have a button and i want to set onclick listener for it. Normally you declare the button with 'findViewById' and then you set the listener. But the inflated buttons aren't present in the activity xml (the linear layout is in a different xml rather than the activity xml) to find their view with 'findViewById'.

My inflater code:

 LinearLayout item = (LinearLayout )findViewById(R.id.linear_layout_duel_basic_easy);
            View child_1 = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);
            item.addView(child_1);

The layout i want to inlfate :

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>

Thanks for any help.

It should work. There is nothing special in here.

   LinearLayout item = findViewById(R.id.linear_layout_duel_basic_easy);
    for (int i = 0 ; i < 10; i++) {
        View child = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);
        item.addView(child);
        final int finalI = i;
        child.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "clicked = " + finalI, Toast.LENGTH_LONG).show();
            }
        });
    }

According to the comment:

In the activity xml i have a vertical linear layout (created for scroll view) and in the xml that i use to inflate a have a horizontal linear layout where a button and a textview are

Here is how you can achieve it.

LinearLayout item = (LinearLayout)findViewById(R.id.linear_layout_duel_basic_easy); // Your vertical linearLayout
View child_1 = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);// Your Horizontal LinearLayout
item.addView(child_1);

To access the view inside horizontal LinearLayout:-

TextView tv = child_1.findViewById(R.id.textView);
Button btn = child_1.findViewById(R.id.button);

Have you tried overriding onFinishInflate() ?

Inside you can look up for the inflated button, you can simply iterate all your children to find the view you need.

//iterate children
for (int index = 0; index < getChildCount(); index+=1){
            getChildAt(index);
//check if that view is what you need
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM