标签:
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();//事务结束
标签:
原文地址:http://www.cnblogs.com/520-1314/p/4786281.html