Android Fragment
2016-01-20 23:56
399 查看
package com.tz.fragmentreplace; import android.os.Bundle; import android.support.v4.app.FragmentActivity; // 2.2 FragmentActivity public class MainActivity extends FragmentActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/title"
android:name="com.tz.fragmentreplace.MyListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2" />
<FrameLayout
android:id="@+id/content1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
package com.tz.fragmentreplace; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager.BackStackEntry; import android.support.v4.app.FragmentManager.OnBackStackChangedListener; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; public class MyListFragment extends ListFragment implements OnItemClickListener { String[] days = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; private FragmentManager fm; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, days)); this.getListView().setOnItemClickListener(this); fm = getFragmentManager(); fm.addOnBackStackChangedListener(new OnBackStackChangedListener() { public void onBackStackChanged() { // TODO Auto-generated method stub int backStackEntryCount = fm.getBackStackEntryCount(); for (int i = 0; i < backStackEntryCount; i++) { BackStackEntry backStackEntryAt = fm.getBackStackEntryAt(i); String name = backStackEntryAt.getName(); Log.i("INFO", name); } } }); super.onActivityCreated(savedInstanceState); } public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // some item been clieked showContent(position); } /** * replace fragment * * @param position */ private void showContent(int position) { // 新的fragment MyContentFragment contentFragment = MyContentFragment .getInstance(position); // 旧的fragment MyContentFragment mf = (MyContentFragment) fm .findFragmentById(R.id.content1); if (mf == null || mf.getMindex() != position) { // 需要将新的fragment替换成旧的 // 开启事务 FragmentTransaction ft = fm.beginTransaction();// ft.replace(R.id.content1, contentFragment); ft.addToBackStack("fragment" + position); // 提交 ft.commit(); } } }
package com.tz.fragmentreplace; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyContentFragment extends Fragment { private int mIndex; public int getMindex() { return mIndex; } public void setMindex(int mIndex) { this.mIndex = mIndex; } private String[] contents = new String[] { "today is Monday", "today is Tuesday", "today is Wednesday", "today is Thursday", "today is Friday", "today is Saturday", "today is Sunday" }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub // 根据星期日期不同,显示不同的内容 Bundle bundle = this.getArguments(); int index = bundle.getInt("index"); this.mIndex = index; String name = bundle.getString("name"); String content = contents[index]; TextView tv = new TextView(getActivity()); tv.setText(content); tv.setGravity(Gravity.CENTER); return tv; } public static MyContentFragment getInstance(int index) { MyContentFragment contentFragment = new MyContentFragment();// 创建fragment Bundle bundle = new Bundle();// bundle.putInt("index", index); bundle.putString("name", "坑总"); contentFragment.setArguments(bundle);// 设置参数进去 return contentFragment; } }
相关文章推荐
- Android图形显示系统——一张图片的显示流程
- android逆向基础:apk 反编译 重打包 重签名
- Android_程序退出_关闭所有activity代码
- Gradle DSL method not found: 'android()
- android 自定义控件属性(TypedArray以及attrs解释)
- Android 打开WIFI并快速获取WIFI的信息
- 【Android】获取控件的宽和高
- Android Studio快捷键
- Android M runtime permission介绍
- Android 上滑弹性动画+下拉放大图像+淘宝菜单
- android studio 1.4 开发错误记录 -不断更新
- Activity生命周期和启动模式 - Android开发艺术探索读书笔记(第一章)
- 自定义Android Switch控件
- 【Android】创建窗口的过程
- Android 在fragment显示调用的方法
- Android-Tips
- Android摄像头基础——第二阶段 自定义简单的相机
- ubntu获取android源码的方法
- android 动画-Tween Animation(一)动画体系简介,涉及到的核心类、核心接口讲解(下)
- android 动画整理概述