欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

android3.0之Fragment(碎片)基礎(chǔ)

系統(tǒng) 1908 0
HoneyComb3.0組件運(yùn)用可以看這里:
http://blog.csdn.net/mayingcai1987/article/category/786494

了解Fragment生命周期看這里:
http://www.open-open.com/lib/view/open1421734804296.html

http://www.android123.com.cn/androidkaifa/772.html
Fragment是Android honeycomb 3.0新增的概念,F(xiàn)ragment名為碎片不過卻和Activity十分相似,下面Android123介紹下Android Fragment的作用和用法。Fragment用來描述一些行為或一部分用戶界面在一個(gè)Activity中,你可以合并多個(gè)fragment在一個(gè)單獨(dú)的activity中建立多個(gè)UI面板,同時(shí)重用fragment在多個(gè)activity中.你可以認(rèn)為fragment作為一個(gè)activity中的一節(jié)模塊 ,fragment有自己的生命周期,接收自己的輸入事件,你可以添加或移除從運(yùn)行中的activity.

? 一個(gè)fragment必須總是嵌入在一個(gè)activity中,同時(shí)fragment的生命周期受activity而影響,舉個(gè)例子吧,當(dāng)activity暫停,那么所有在這個(gè)activity的fragments將被destroy釋放。然而當(dāng)一個(gè)activity在運(yùn)行比如resume時(shí),你可以單獨(dú)的操控每個(gè)fragment,比如添加或刪除。

1,先定義2個(gè)Fragment,布局文件R.layout.first&R.layout.second根據(jù)自己需求隨便寫一個(gè),我這里就不貼代碼了。
    
import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;

public class FirstFragment extends Fragment{

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                             Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.first, container, false);
		registerForContextMenu(root.findViewById(R.id.editText1));
		return root; 
    } 
	
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, 0, Menu.NONE, "菜單1");
        menu.add(Menu.NONE, 1, Menu.NONE, "菜單2");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return super.onContextItemSelected(item);
    }
	
}

  

    
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SecondFragment extends Fragment{

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
		return inflater.inflate(R.layout.second, container, false); 
	} 
}

  


2,在Activity中使用
    
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Honeycomb extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
//        FirstFragment firstFragment=new FirstFragment();
//        //在Activity中通過這個(gè)與Fragment通訊
//        getFragmentManager().beginTransaction().add(android.R.id.content, firstFragment).commit();
        
        FragmentManager fm = getFragmentManager();
        addShowHideListener(R.id.btn_1, fm.findFragmentById(R.id.firstFragment));
        addShowHideListener(R.id.btn_2, fm.findFragmentById(R.id.secondFragment));
        
    }
    
    void addShowHideListener(int buttonId, final Fragment fragment) {
        final Button button = (Button)findViewById(buttonId);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                //為Fragment設(shè)置淡入淡出效果
                ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
                        
                if (fragment.isHidden()) {
                    ft.show(fragment);
                    button.setText("隱藏");
                } else {
                    ft.hide(fragment);
                    button.setText("顯示");
                }
                ft.commit();
            }
        });
    }
    
}

  

3,布局R.layout.main中引用碎片
    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    >
   
     <fragment android:name="com.ql.app.FirstFragment" 
            android:id="@+id/firstFragment" 
            android:layout_weight="1" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            /> 
    <fragment android:name="com.ql.app.SecondFragment" 
            android:id="@+id/secondFragment" 
            android:layout_weight="2" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            />
            
     <Button android:id="@+id/btn_1"
      	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content"
    	android:text="隱藏"
     /> 
     <Button android:id="@+id/btn_2"
      	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content"
    	android:text="隱藏"
     /> 
</LinearLayout>

  

4,上圖

android3.0之Fragment(碎片)基礎(chǔ)

http://blog.csdn.net/nkmnkm/article/details/7256605

android3.0之Fragment(碎片)基礎(chǔ)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美精品18videosex性俄罗斯 | 日韩在线视频导航 | 日本欧美人xxxxx在线观看 | 成人免费视频观看 | 丝袜捆绑调教视频免费区 | 亚洲伊人成色综合网 | 国产福利视频一区 | 老汉色影院 | 亚洲一区久欠无码A片 | 黄色av网站在线免费观看 | 影音先锋欧美资源 | 乳欲人妻办公室奶水在线电影国产 | 亚洲欧美中文日韩在线 | 一级做a爰片久久毛片人呢 达达兔午夜起神影院在线观看麻烦 | 黄片毛片在线观看 | 午夜免费电影网 | 欧美成人做性视频在线播放 | 魔法骑士在线观看免费完整版 | 久草视频福利在线观看 | 欧美日韩性高爱潮视频 | 欧洲成人| 国产专区在线播放 | 小明永久2015www永久免费观看 | 高清一区二区亚洲欧美日韩 | 午夜a级片 | 欧美日韩精品一区二区在线播放 | 欧美日韩免费在线观看 | 色综合久久天天综合网 | 青青草视频网 | 成人精品在线观看 | 国产一区二区三区久久久久久久久 | 成人午夜大片免费看爽爽爽 | 一区二区福利视频 | 日韩在线高清视频 | 亚洲youjizz| 成人免费在线 | 极品美女一区二区三区视频 | 欧美人两个人激情的免费视频 | 免费激情网址 | 高清视频在线播放 | 99在线播放视频 |