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); }*/ } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。