Reputation: 35141
I want to have two or more Fragments which will be processed similarly. Can I reuse ids in each Fragment?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Fragment
android:id="@+id/fragment_1"
class="com.example.DoesSomethingToTitle"
<TextView android:id="@+id/title" />
</Fragment>
<Fragment
android:id="@+id/fragment_2"
class="com.example.DoesSomethingToTitle"
<TextView android:id="@id/title" /> <!-- same id -->
</Fragment>
</FrameLayout>
And use it like this:
public class DoesSomethingToTitle {
private String titletext;
public DoesSomethingToTitle(String titletext) {
this.titletext = titletext;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.feed, container, false);
// find the title TextView and mutate it
((TextView) v.findViewById(R.id.title)).setText(titletext);
return v;
}
}
(I realize for something as simple as this, I could just specify the title string in the XML; my actual usage is more complex.)
Upvotes: 2
Views: 808