开发者

Same fragments in one activity problem

开发者 https://www.devze.com 2023-03-22 13:10 出处:网络
I\'m coding my first android app with fragments and here is my problem. I\'ve got main activity and 3 fragments in it. All 3 fr开发者_如何学JAVAagments are the same class so all 3 fragments has the sa

I'm coding my first android app with fragments and here is my problem. I've got main activity and 3 fragments in it. All 3 fr开发者_如何学JAVAagments are the same class so all 3 fragments has the same layout. But I need every fragment to has different title in that layout. But I can't select the TextView wich I need because all these fragmtents TextViews has the same ID because of the same layout. Is there some easy way to do this.

Thanx


Given your Fragments f1, f2, f3, you could try f1.getView().findViewById(id), f2.getView().findViewById(id), f3.getView().findViewById(id) to reduce the scope of the search to only that particular fragment each time.

However, I think the point of using Fragments is to improve modularity and to avoid things like having to expose all of the views to the Activity. It can be controlled by the Fragment instead. You could have the views be resolved inside the Fragment and then give a setTitle() method on the Fragment for the Activity to use.


The ID should not matter since each class will be inflating its own instance of the layout. Try it using the same ID.


If under 'fragment' you mean an UI component you should specify different IDs for each and every component in your layout!

<LinearLayout
    android:id="@+id/LinearLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000"
        android:text="Header"
        />

    <TextView
        android:id="@+id/body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00ff00"
        android:text="Body"
        />

     <TextView
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0000ff"
        android:text="Footer"
        />

</LinearLayout>

If you mean other xml fragments inside a main xml layout, you can use same ID, but in different xml fragments!

0

精彩评论

暂无评论...
验证码 换一张
取 消