zoukankan      html  css  js  c++  java
  • 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);
    		}*/
    	}
    
    }
    



  • 相关阅读:
    Java中的BoneCP数据库连接池用法
    css 属性选择器笔记
    px,em,rem的区别
    谈谈newDate()的简单使用 JS
    user-select属性用法
    闭包解决的问题
    jQuery 中position()与offset()的区别
    attr() VS prop()
    .toArray()与jQuery.makeArray()的区别
    浏览器的缓存控制
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5340782.html
Copyright © 2011-2022 走看看