How to switch between these twelve fragments



Hello everyone I'm trying to make a very simple app for a project demonstration that can switch through twelve different fragments. I want to start off with one fragment and then use an ontouch listener to switch to the next one. Here's my main xml:



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.demoww.MainActivity" >

<fragment class="com.example.demoww.suggestedfriend"
android:id="@+id/suggestedfriendLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

<fragment class="com.example.demoww.profile"
android:id="@+id/ProfileLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>

<fragment class="com.example.demoww.messages"
android:id="@+id/messagesLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>
<fragment class="com.exmaple.demoww.friendslist"
android:id ="@+id/friendslistlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>

<fragment class="com.example.demoww.raul_message"
android:id="@+id/examplemessagelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>

<fragment class="com.example.demoww.raul_profile"
android:id="@+id/raulprofileLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>

<fragment class="com.example.demoww.requests"
android:id="@+id/requestsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>

<fragment class="com.example.demoww.search"
android:id="@+id/searchlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>

</LinearLayout>


and here's the class of the first activity:



public class suggestedfriends extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.search,
container, false);
view.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

return true;
}
});
return view;
}




}


I'm trying to find a way to switch fragments in the main activity without altering the man xml. I'm worried that using a fragment manager might somehow change the main xml. And I'ma also worried that setting visibility to something else will keep the fragments all running in the background making my emulator slow.


Any help would be appreciated since I'm just a simple business major and don't really know much about CS/android design.


No comments:

Post a Comment