首页 > 代码库 > Fragment的使用简介【Android】
Fragment的使用简介【Android】
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; @Override protected 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() { @Override public 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; } //这个是初始化fragment Fragment fragment = (Fragment) fragments.instantiateItem(layout_content, index); //默认选择第0个fragment fragments.setPrimaryItem(layout_content, 0, fragment); //fragment提交更新事务 fragments.finishUpdate(layout_content); } }); main_radio.check(current_index); } // getSupportFragmentManager() 这个方法是在FragmentActivity 里面的 FragmentStatePagerAdapter fragments = new FragmentStatePagerAdapter( getSupportFragmentManager()) { //几个fragment就返回几个 @Override public int getCount() { return 5; } @Override public 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 { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ImageView imageview = new ImageView(getActivity()); imageview.setBackgroundResource(R.drawable.g1); return imageview; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public 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); }*/ } }
Fragment的使用简介【Android】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。