標籤:
1.Fragment建立
extents Fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
rootView.findViewById(R.id.ShowAnotherFragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.fragment, new AnotherFragment())
.commit();
}
});
2.Intent的使用
使用意圖 把上一所活動的資料 傳遞 到下一個活動,
intent.putExtra("msg", data);
String data = intent.getStringExtra("msg");
使用意圖 將下一個活動的資料 返回 到上一個活動
intent.putExtra("msg","Hello FirstActivity");
setResult(RESULT_OK, intent);
startActivityForResult(intent1, 1);
protected void onActivityResult() {
switch (requestCode){
case 1: if(resultCode == RESULT_OK){
} break ; }
public static interface NavigationDrawerCallbacks {
/**
3 對於Fragment 在活動中的傳遞的例子(LearnFragment)理解
1. MainActivity類載入一個activity_main.xml,如果介面沒有資料,載入一個MainActivityFragment類;
2. MainActivity類載入一個fragment_main.xml,介面有兩個按鈕,一個按鈕顯示另一個Fragment,另一個按鈕開啟側邊欄活動(SliderActivity類);
3. SliderActivity類的onCreate()載入一個activity_slider.xml介面,裡面有FrameLayout布局和自訂fragment布局,FrameLayout可以重疊作為背景,
fragment載入NavigationDrawerFragment類.
SliderActivity類的onCreateView()載入fragment_slider.xml介面,覆蓋activity_slider.xml介面裡面有FrameLayout布局.
4. NavigationDrawerFragment類的onCreateView()方法中,建立View view.inflate填充一個R.layout.slider_content介面,
slider_content.xml介面有一個按鈕,實現按鈕點擊跳轉功能,
然後在NavigationDrawerFragment類中建立一個NavigationDrawerCallbacks介面,寫一個onGoToJkxuClicked()方法.
最後在SliderActivity類中實現NavigationDrawerCallbacks介面,重新onGoToJkxuClicked()方法,方法中具體實現片段的切換.
4.關於LayoutInflater類inflate(int resource, ViewGroup root, boolean attachToRoot)方法三個參數的含義
resource:需要載入布局檔案的id,意思是需要將這個布局檔案中載入到Activity中來操作。
root:需要附加到resource資源檔的根控制項,什麼意思呢,就是inflate()會返回一個View對象,
如果第三個參數attachToRoot為true,就將這個root作為根對象返回,否則僅僅將這個root對象的LayoutParams屬性附加到resource對象的根布局對象上,
也就是布局檔案resource的最外層的View上,比如是一個LinearLayout或者其它的Layout對象。
attachToRoot:是否將root附加到布局檔案的根視圖上
5.如何在Activity 中使用 Fragment
建立一個fragment.xml---fragment(Fragment)---activity.xml布局嵌套fragment---activity(Activity)調用getFragmentManager().findFragmentById(R.id.fragment)方法
6.一個新聞內容頁面
1.建立一個新聞類
2.建立一個新聞列表布局,建立一個新聞列表布局的適配器;
3.建立一個新聞內容布局,建立一個片段載入新聞內容布局;
4.建立一個顯示新聞內容布局(可以引用之前的片段新聞內容布局),建立一個活動類來顯示新聞內容;
5.建立一個顯示新聞列表布局,建立一個片段來載入這個布局{
在onAttach()方法資料的初始化,完成2的適配器操作;
然後在onCreateView()方法 ,載入顯示新聞列表布局 ,顯示新聞列表; 然後新聞列表setOnItemClickListener(),在方法中傳遞資料,並啟動3活動類;
}
7. Fragment 的總結
1活動中的使用片段
Fragment fragment = (Fragment)getFragmentManger().findFragmentById(R.id.fragment);
2片段中得到活動
MainActivity activity = (MainActivity)getActivity()
3,片段通過活動擷取另外一個片段
8. BroadcastReceiver
1.
全域發送標準廣播sendBroadcast(intent);
全域發送有序廣播sendOrderedBoadcast(intent,null)
全域接受標準廣播 AndroidManifest.xml註冊<receiver> <intent-filter><action android:name="com,example.broadcasttest.MY_BROADCAST">
全域接受有序廣播 加上優先順序<intent-filter android:priority>
2.
應用內的廣播
發送廣播: sendBroadcast(intent)
註冊廣播監聽器:registerReceiver(BroadcastReceiver執行個體,IntentFilter執行個體)
9. Activity Intent的FLAG
1.Intent.FLAG_ACTIVITY_NEW_TASK 取出一個棧的活動或者重新建立一個活動
2.FLAG_ACTIVITY_SINGLE_TOP 相當於現用模式中的singletop
3.FLAG_ACTIVITY_CLEAR_TOP 相當於現用模式中的SingleTask
10. Activity有四種載入模式:standard(預設), singleTop, singleTask和 singleInstance
singleTop 在棧頂的活動不會重新建立
singleTask 不在棧頂,建立唯一的活動執行個體
singleInstance 單一執行個體,其他程式調用活動時,重新建立一個單獨的返回棧
11.
一。獲得 LayoutInflater 執行個體的三種方式(比如 LayoutInflater.from(context).inflate(R.layout.title, this))
1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3. LayoutInflater inflater = LayoutInflater.from(context);
二。inflate方法
public View inflate(int Resourece,ViewGroup root)
作用:填充一個新的視圖階層從指定的XML資源檔中
reSource:View的layout的ID
root: 產生的階層的根視圖
12.簡單的那個View的載入 或 使用Fragment 載入View
比如你用view, 很多邏輯代碼可能都需要寫在Activity裡,如果view很多.
耦合度會很高。用Fragment則可以各自管理,可以看作一種解耦吧
12.
資料存放區:檔案儲存體 SharePreference和資料庫儲存
1.儲存檔案儲存體
FileOutputStream out = openFileOutput("檔案名稱",Context.MODE_PRIVATE);
BufferedWriter writer = new BufferdeWriter(new OutputStreamWriter(out));
writer.wreter("111111111111111111--data");
2.讀取檔案儲存體
FileInputStream in = openFileInput("檔案名稱");
BufferedRead read = new BufferedReader(new InputStreamReader(in));
StringBuilder content = new StringBuilder();
content.append(reader.readLine())
3.SharePreference儲存
SharePreference.Editor editor = getSharedPreferences("檔案名稱",MODE_PRIVATE.edit());
editor.putString("KEY","值")
editor.commit();
4.SharePreference讀取
SharePreferences pref = getSharedPreference("檔案名稱",MODE_PRIVATE);
String string = pref.getString("key","預設值")
5.SQLiteOpenHelper db
建立資料庫 db.execSQL("create table person(id integer primary key, name text,age integer)")
6.SharePreference 使用事物 , 資料要麼全部完成要麼不完成.
SQLiteDatebase db = (new SQLiteOpenHelper).getWritableDatabase();
da.beginTranscation();//開啟事物
ContentValues values = new ContentValues();
values.put("name","xxx");
db.insert("資料庫名",null,values)
db.setTransactionSuccessful//事務開啟成功
db.endTransaction();//事務結束
android 筆記一