Fragment详解(三)--->横竖屏幕切换完整示例
发布日期:2021-06-30 11:13:46
浏览次数:2
分类:技术文章
本文共 8030 字,大约阅读时间需要 26 分钟。
MainActivity如下:
package cc.testsimplefragment2;import android.app.Activity;import android.os.Bundle;/** * Demo描述: * Fragment使用示例 * * 备注说明: * 1 在main.xml中存在一个Fragment即 * TitleListFragment.用它来显示Titles * 2 点击TitleListFragment中的某个条目的时候 * 判断的横竖屏 * 2.1 若是横屏,则用另外一个Fragment显示详细信息 * 2.2 若是竖屏,则启动另一个Activity显示详细信息 * 2.2.1 在该Activity中动态添加一个Fragment显示详细信息 * * 参考文档: * 1 http://blog.csdn.net/t12x3456/article/details/8120309 * 2 http://blog.csdn.net/nkmnkm/article/category/958669/5 * 3 http://blog.csdn.net/xiaanming/article/details/9254749 * Thank you very much * */public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); System.out.println("---> MainActivity onSaveInstanceState()"); }}TitleListFragment如下:
package cc.testsimplefragment2;import android.app.FragmentTransaction;import android.app.ListFragment;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;public class TitleListFragment extends ListFragment { //当前是否为横屏 private boolean currentIsLand; //当前选中位置 private int currentCheckedPosition = -1; private final String currentCheckedKey="currentChecked"; //每次重绘Fragment时都会调用该onActivityCreated()方法 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); System.out.println("---> onActivityCreated"); //设置适配器 ArrayAdapterDetailFragment如下:arrayAdapter= new ArrayAdapter (getActivity(),android.R.layout.simple_list_item_checked, Data.TITLES); setListAdapter(arrayAdapter); //判断当前是否为横屏 //注意方式: //先利用getActivity()找到该Fragment隶属于的Activity,再findViewById() //为什么要这么判断是否是横屏呢? //因为横屏的时候会去读取layout-land下的main.xml布局文件 //在该布局文件下有一个RelativeLayout,其id为containerRelativeLayout //所以,若能找到它那么就可以表明当前是横屏 View containerView = getActivity().findViewById(R.id.containerRelativeLayout); currentIsLand = containerView != null && containerView.getVisibility() == View.VISIBLE; if (savedInstanceState != null) { currentCheckedPosition = savedInstanceState.getInt(currentCheckedKey, 0); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); getListView().setItemChecked(currentCheckedPosition, true); System.out.println("---> onActivityCreated() currentCheckedPosition="+currentCheckedPosition); }else{ System.out.println("---> onActivityCreated() savedInstanceState == null "); } if (currentIsLand) { //设置ListView为单选模式 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); showDetailContent(currentCheckedPosition); } } @Override public void onStart() { super.onStart(); System.out.println("---> onStart"); } //处理手机一直在竖屏时: //点击标题,跳转后,再按下back键的情况 @Override public void onResume() { super.onResume(); System.out.println("---> onResume"); if (!currentIsLand) { getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); //currentCheckedPosition默认为-1 //所以在第一次时不选择任何一个 if (currentCheckedPosition>=0) { getListView().setItemChecked(currentCheckedPosition, true); } } } //保存数据 //Fragment被系统或者某些内存清理,或者横竖屏切换而被销毁时 //将会触发onSaveInstanceState(Bundle savedInstanceState): //Called to ask the fragment to save its current dynamic state, //so it can later be reconstructed in a new instance of its process is restarted. //然后调用onActivityCreated()生命周期方法,重绘Fragment //存在的疑问: //在手机一直竖屏的情况下,点击TitleListFragment的条目 //为什么会执行该onSaveInstanceState()方法和MainActivity的 //onSaveInstanceState()方法 @Override public void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(bundle); bundle.putInt(currentCheckedKey, currentCheckedPosition); System.out.println("---> onSaveInstanceState()"); System.out.println("---> onSaveInstanceState() currentCheckedPosition="+currentCheckedPosition); } //点击ListFragment上的某个条目时调用 @Override public void onListItemClick(ListView l, View v, int position, long id) { showDetailContent(position); } //显示详细内容 private void showDetailContent(int index) { //保存当前被选中的位置 currentCheckedPosition=index; //横屏时 if (currentIsLand) { //1 先设置TitleListFragment // 改变ListFragment的index位置被选中 getListView().setItemChecked(index, true); //2 再设置DetailFragment // 利用FragmentManager判断在某个容器(container)中是否有一个DetailFragment // 如果没有或在有的情况下其CheckedIndex不等于当前被选中的index // 则进行replace()操作 DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.containerRelativeLayout); if (detailFragment == null || detailFragment.getCheckedIndex() != index) { //生成新的Fragment. detailFragment = DetailFragment.newInstance(index); //开始Fragment的事务Transaction //因为在这个事务中执行了replace()操作,所以会将该 //detailFragment attach到activity,按照生命周期 //的顺序必然会调用DetailFragment的onCreateView(). //更加白话地说:其实相当于我们先做了一些内在的东西,再做了外在的东西. //什么意思呢? //内在的部分:我们先new了一个DetailFragment,并且为这个DetailFragment //设置了一些参数. //这些都体现在了DetailFragment.newInstance(index)方法中 //外在的部分:利用replace()将该DetailFragment attach到activity //这些都体现在了DetailFragment的onCreateView()方法中.所以在 //该方法中我们可以去取为此DetailFragment设置的参数. //即方法getCheckedIndex() FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); //替换容器(container)原来的Fragment fragmentTransaction.replace(R.id.containerRelativeLayout, detailFragment); //设置转换效果 fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //提交事务 fragmentTransaction.commit(); } //竖屏时 } else { Intent intent = new Intent(getActivity(),DetailActivity.class); intent.putExtra("index", index); startActivity(intent); } }}
package cc.testsimplefragment2;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ScrollView;import android.widget.TextView;public class DetailFragment extends Fragment { //往ScrollView中添加一个TextView然后将其返回 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { if (container == null) { return null; } ScrollView scrollView = new ScrollView(getActivity()); TextView textView = new TextView(getActivity()); textView.setPadding(10, 10, 10, 10); scrollView.addView(textView); textView.setText(Data.DETAILS[getCheckedIndex()]); return scrollView; } //生成DetailFragment实例,并且保存index public static DetailFragment newInstance(int index) { DetailFragment detailFragment = new DetailFragment(); Bundle bundle = new Bundle(); bundle.putInt("index", index); detailFragment.setArguments(bundle); return detailFragment; } //取出index public int getCheckedIndex() { int index =getArguments().getInt("index", 0); return index; }}DetailActivity如下:
package cc.testsimplefragment2;import android.app.Activity;import android.content.res.Configuration;import android.os.Bundle;/** * 在此Activity中我们将一个Fragment嵌入到 * 该Activity中进行显示 */public class DetailActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //横屏的处理 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; } //竖屏的处理 //此处的道理和TitleListFragment中对于横屏的 //处理很相似,只是方式稍有差别. //1 生成设置好参数的DetailFragment //2 将此DetailFragment attach到当前的activity if (savedInstanceState == null) { DetailFragment detailFragment = new DetailFragment(); detailFragment.setArguments(getIntent().getExtras()); getFragmentManager() .beginTransaction() .add(android.R.id.content, detailFragment) .commit(); } }}Data如下:
package cc.testsimplefragment2;public final class Data { //标题 public static final String[] TITLES = { "标题1","标题2", "标题3","标题4"}; //内容 public static final String[] DETAILS = { "内容1","内容2", "内容3","内容4"};}layout/main.xml如下:
layout-land/main.xml如下:
AndroidManifest.xml如下:
转载地址:https://it9527.blog.csdn.net/article/details/12460849 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月07日 08时01分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
检测到会话cookie中缺少HttpOnly属性
2019-04-30
Centos下Yum安装PHP5.5,5.6
2019-04-30
zabbix安装搭建php环境yum语句
2019-04-30
Linux下配置APACHE支持PHP环境
2019-04-30
Ubuntu忘记超级用户root密码,重新设置密码
2019-04-30
Ubuntu下使用Vi是方向键变乱码 退格键不能使用的解决方法
2019-04-30
redis的安装、启动、停止
2019-04-30
Linux下防火墙开启相关端口及查看已开启端口
2019-04-30
一次zabbix连接不上mysql数据库的经历
2019-04-30
Zabbix error - Invalid first parameter.
2019-04-30
通过maven打包依赖jar,打包主类,在pom.xml中设置
2019-04-30
redis中pipeline的使用
2021-07-03
linux命令seq产生序列
2021-07-03
apache配置https
2021-07-03
ORACLE查出表所有的触发器及触发器详细信息
2021-07-03
redis主从复制配置
2021-07-03
Redis重启数据丢失问题
2021-07-03