Fragment的使用简介【Android】

来源:互联网 发布:内网演示软件 编辑:程序博客网 时间:2024/05/19 19:32

Fragment在实际项目开发中使用的越来越多,现在简单介绍一下

布局文件:

<LinearLayout 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:orientation="vertical"    tools:context=".MainActivity" >    <FrameLayout        android:id="@+id/layout_content"        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1.0" />    <RadioGroup        android:id="@+id/main_radio"        android:layout_width="fill_parent"        android:layout_height="50dp"        android:layout_gravity="bottom"        android:paddingTop="8dp"        android:background="@drawable/bottom_tab_bg"        android:gravity="center_vertical"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/rb_1"            style="@style/main_tab_bottom"            android:layout_width="wrap_content"            android:layout_height="50dp"            android:drawableTop="@drawable/icon_function" />        <RadioButton            android:id="@+id/rb_2"            style="@style/main_tab_bottom"            android:layout_width="wrap_content"            android:layout_height="50dp"            android:drawableTop="@drawable/icon_newscenter" />        <RadioButton            android:id="@+id/rb_3"            style="@style/main_tab_bottom"            android:layout_width="wrap_content"            android:layout_height="50dp"            android:drawableTop="@drawable/icon_govaffairs" />        <RadioButton            android:id="@+id/rb_4"            style="@style/main_tab_bottom"            android:layout_width="wrap_content"            android:layout_height="50dp"            android:drawableTop="@drawable/icon_setting" />        <RadioButton            android:id="@+id/rb_5"            style="@style/main_tab_bottom"            android:layout_width="wrap_content"            android:layout_height="50dp"            android:drawableTop="@drawable/icon_smartservice" />    </RadioGroup></LinearLayout>

Java文件:

package com.succ7.fragmenttest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentStatePagerAdapter;import android.view.Window;import android.widget.FrameLayout;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends FragmentActivity {private FrameLayout layout_content;/** * 默认显示的fragment */private int index = 0;private int current_index = R.id.rb_1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);layout_content = (FrameLayout) findViewById(R.id.layout_content);RadioGroup main_radio = (RadioGroup) findViewById(R.id.main_radio);main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.rb_1:index = 0;break;case R.id.rb_2:index = 1;break;case R.id.rb_3:index = 2;break;case R.id.rb_4:index = 3;break;case R.id.rb_5:index = 4;break;}//这个是初始化fragmentFragment fragment = (Fragment) fragments.instantiateItem(layout_content, index);//默认选择第0个fragmentfragments.setPrimaryItem(layout_content, 0, fragment);//fragment提交更新事务fragments.finishUpdate(layout_content);}});main_radio.check(current_index);}// getSupportFragmentManager() 这个方法是在FragmentActivity 里面的FragmentStatePagerAdapter fragments = new FragmentStatePagerAdapter(getSupportFragmentManager()) {//几个fragment就返回几个@Overridepublic int getCount() {return 5;}@Overridepublic Fragment getItem(int arg0) {Fragment fragment = null;switch (arg0) {case 0:fragment = new Fragment1();break;case 1:fragment = new Fragment2();break;case 2:fragment = new Fragment3();break;case 3:fragment = new Fragment4();break;case 4:fragment = new Fragment5();break;}return fragment;}};}

Fragment1,Fragment2-5代码:

package com.succ7.fragmenttest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;public class Fragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {ImageView imageview = new ImageView(getActivity());imageview.setBackgroundResource(R.drawable.g1);return imageview;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic void setMenuVisibility(boolean menuVisible) {super.setMenuVisibility(menuVisible);if(getView() != null){getView().setVisibility(menuVisible?View.VISIBLE:View.INVISIBLE);}/*if(menuVisible){getView().setVisibility(View.VISIBLE);}else{getView().setVisibility(View.INVISIBLE);}*/}}



0 0
原创粉丝点击