Android 如何在这十二个片段之间切换
大家好,我正在尝试为一个项目演示制作一个非常简单的应用程序,可以在12个不同的片段之间切换。我想从一个片段开始,然后使用ontouch侦听器切换到下一个片段。以下是我的主要xml:Android 如何在这十二个片段之间切换,android,xml,android-fragments,android-fragmentactivity,Android,Xml,Android Fragments,Android Fragmentactivity,大家好,我正在尝试为一个项目演示制作一个非常简单的应用程序,可以在12个不同的片段之间切换。我想从一个片段开始,然后使用ontouch侦听器切换到下一个片段。以下是我的主要xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_p
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
我试图找到一种在不改变man xml的情况下切换主活动中片段的方法。我担心使用片段管理器可能会以某种方式更改主xml。我还担心设置其他内容的可见性会使所有片段都在后台运行,这会使我的模拟器速度变慢
任何帮助都将不胜感激,因为我只是一名简单的商务专业学生,对CS/android设计不太了解。向Xml添加片段将它们设置为静态片段,这意味着无法删除或替换 因此,您有两种选择: 1.将它们添加为动态片段,以便可以使用getFragmentManager/getSupportFragmentManager添加/替换片段 2.使用Viewpager并将片段添加到适配器,您可以在它们之间滑动
希望有帮助。如何将它们添加为动态片段?
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;
}
}