Android Fragment的回退栈示例详细介绍
2016-12-25 11:50
471 查看
Android Fragment的回退栈
点开之后按一次回退键只返回一次
MainActivity 类
public class MainActivity extends AppCompatActivity { ListView lv; List<Article> mList = new ArrayList<>(); ContentFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); //找到xml中静态创建的fragment // fragment = (ContentFragment) getSupportFragmentManager().findFragmentByTag("fragment"); // //获取到引用 // fragment.showToask("这是activity中调用fragment中的方法"); initData(); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Article a = mList.get(position); ContentFragment fragment = new ContentFragment(); //Activity向新建的fragment传参 Bundle bundle = new Bundle(); bundle.putSerializable("article",a); fragment.setArguments(bundle); //去显示 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.fl_content,fragment); //添加回退栈 ft.addToBackStack(null); ft.commit(); } }); } public void showToask(String msg){ Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); } private void initData() { mList.add(new Article("习近平会见菲总统:把分歧管控好 把合作谈起来", "习近平指出,中菲是隔海相望的近邻,两国人民是血缘相亲的兄弟。中菲同为发展中国家,团结、互助、合作、发展是我们的共同目标。虽然我们之间经历风 雨,但睦邻友好的情感基础和合作意愿没有变。中方高度重视中菲关系,愿同菲方一道努力,不断增进政治互信、深化互利合作、妥善处理分歧,做感情上相近相 通、合作中互帮互助、发展中携手前行的睦邻友好伙伴。")); mList.add(new Article("外交部官员", "。中菲同为发展中国家,团结、互助、合作、发展是我们的共同目标。虽然我们之间经历风 雨,但睦邻友好的情感基础和合作意愿没有变。中方高度重视中菲关系,愿同菲方一道努力,不断增进政治互信、深化互利合作、妥善处理分歧,做感情上相近相 通、合作中互帮互助、发展中携手前行的睦邻友好伙伴。")); mList.add(new Article("洪秀柱邀国民党4巨头谈党产议题 马英九将出席:把分歧管控好 把合作谈起来", "邻,两国人民是血缘相亲的兄弟。中菲同为发展中国家,团结、互助、合作、发展是我们的共同目标。虽然我们之间经历风 雨,但睦邻友好的情感基础和合作意愿没有变。中方高度重视中菲关系,愿同菲方一道努力,不断增进政治互信、深化互利合作、妥善处理分歧,做感情上相近相 通、合作中互帮互助、发展中携手前行的睦邻友好伙伴。")); } class Article implements Serializable{ String title; String content; public Article( String title,String content) { this.title = title; this.content = content; } } private BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { return mList.size(); } @Override public Article getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = new TextView(MainActivity.this); TextView tv = (TextView) convertView; tv.setText(mList.get(position).title); return convertView; } }; }
ContentFragment 类
public class ContentFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return View.inflate(getActivity(), android.R.layout.simple_list_item_2, null); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); TextView text1 = (TextView) view.findViewById(android.R.id.text1); TextView text2 = (TextView) view.findViewById(android.R.id.text2); Bundle bundle = getArguments(); if (bundle == null) { text1.setText("测试"); text2.setText("测试内容"); } else { MainActivity.Article article = (MainActivity.Article) bundle.get("article"); text1.setText(article.title); text2.setText(article.content); } //开始调用 MainActivity activity = (MainActivity) getActivity(); activity.showToask("这是fragment中调用activity中的方法"); } public void showToask(String msg){ Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show(); } }
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.backstack10_20.MainActivity"> <!--<fragment--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="match_parent"--> <!--android:layout_weight="1"--> <!--android:tag="fragment"--> <!--android:name="com.example.backstack10_20.ContentFragment"--> <!--/>--> <ListView android:layout_width="0dp" android:layout_weight="1" android:id="@+id/lv" android:layout_height="wrap_content"/> <FrameLayout android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:id="@+id/fl_content"/> </LinearLayout>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
- Android基础之使用Fragment控制切换多个页面
- Android Fragment 基本了解(图文介绍)
- Android 管理Activity中的fragments
- android开发教程之实现滑动关闭fragment示例
- Android的Fragment的生命周期各状态和回调函数使用
- Android程序开发之Fragment实现底部导航栏实例代码
- FrameLayout和Fragment处理Android应用UI布局实例
- Android App中ViewPager与Fragment结合的一些问题解决
- Android 开发之BottomBar+ViewPager+Fragment实现炫酷的底部导航效果
- Android App中使用ViewPager+Fragment实现滑动切换效果
- 详解Android应用中DialogFragment的基本用法
相关文章推荐
- Android Handler详细介绍+示例
- android fragment详细介绍
- android fragment详细介绍
- android中用Fragment的简单介绍和创建选项卡详细列子
- 【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
- android fragment详细介绍
- Android Handler详细介绍+示例
- Android:fragment书上例子详细介绍
- 【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
- phonegap 开发入门 PhoneGap官方网站上有详细的入门示例教程,这里,我针对使用PhoneGap进行Android移动应用的开发对其官网的Get Started进行一些介绍、补充。 Ste
- android fragment详细介绍
- 【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
- 连连算法介绍-示例基于android平台
- Android——Fragment介绍
- Android游戏开发之数据库SQLite 详细介绍(十七) .
- Android FragmentManage FragmentTransaction介绍
- Android FragmentManage FragmentTransaction介绍
- Android游戏开发之数据库SQLite 详细介绍(十七)
- Android WebView 详细介绍
- Fragment、Activity比较——Android碎片介绍