Fragment使用
Activity的按钮响应里实现Fragment切换:
package com.sg.zh; import android.app.Activity; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import com.sg.zh.fragment.FragmentOne; import com.sg.zh.fragment.FragmentTwo; public class MainActivity extends Activity { private ImageButton imgSurvey; private ImageButton imgService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgSurvey = (ImageButton)super.findViewById(R.id.img_survey); imgService = (ImageButton)super.findViewById(R.id.img_service); imgSurvey.setOnClickListener(new OnClickListenerImpl()); imgService.setOnClickListener(new OnClickListenerImpl()); } public class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); switch (v.getId()) { case R.id.img_survey: FragmentOne survey = new FragmentOne(); ft.replace(R.id.container, survey); ft.commit(); break; case R.id.img_service: FragmentTwo service = new FragmentTwo(); ft.replace(R.id.container, service); ft.commit(); break; default: break; } } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context=".MainActivity" > <!-- 顶部 --> <RelativeLayout android:id="@+id/top_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/top_big_bg" > <!-- 返回按钮 --> <ImageView android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@drawable/btn_back" /> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/exhibitors" android:textColor="@color/white" /> <TextView android:id="@+id/txt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dip" android:text="N2馆" android:textColor="@color/white" /> </RelativeLayout> <RelativeLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_menu" android:layout_below="@id/top_layout" android:paddingLeft="5dip" android:paddingRight="5dip" /> <!-- 底部菜单 --> <include android:id="@+id/bottom_menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" layout="@layout/bottom_menu" /> </RelativeLayout>
package com.sg.zh.fragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.sg.zh.R; public class FragmentOne extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_exhibitors, null); return view; } }