0
点赞
收藏
分享

微信扫一扫

Android手机平板两不误,使用Fragment实现兼容手机和平板的程序

一天清晨 2022-01-31 阅读 34
  • ListView的适配器。
    */
    private ArrayAdapter adapter;

/**

  • 用于填充ListView的数据,这里就简单只用了两条数据。
    */
    private String[] menuItems = { “Sound”, “Display” };

/**

  • 是否是双页模式。如果一个Activity中包含了两个Fragment,就是双页模式。
    */
    private boolean isTwoPane;

/**

  • 当Activity和Fragment建立关联时,初始化适配器中的数据。
    */
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    adapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, menuItems);
    }

/**

  • 加载menu_fragment布局文件,为ListView绑定了适配器,并设置了监听事件。
    */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu_fragment, container, false);
    menuList = (ListView) view.findViewById(R.id.menu_list);
    menuList.setAdapter(adapter);
    menuList.setOnItemClickListener(this);
    return view;
    }

/**

  • 当Activity创建完毕后,尝试获取一下布局文件中是否有details_layout这个元素,如果有说明当前
  • 是双页模式,如果没有说明当前是单页模式。
    */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getActivity().findViewById(R.id.details_layout) != null) {
    isTwoPane = true;
    } else {
    isTwoPane = false;
    }
    }

/**

  • 处理ListView的点击事件,会根据当前是否是双页模式进行判断。如果是双页模式,则会动态添加Fragment。
  • 如果不是双页模式,则会打开新的Activity。
    */
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
    if (isTwoPane) {
    Fragment fragment = null;
    if (index == 0) {
    fragment = new SoundFragment();
    } else if (index == 1) {
    fragment = new DisplayFragment();
    }
    getFragmentManager().beginTransaction().replace(R.id.details_layout, fragment).commit();
    } else {
    Intent intent = null;
    if (index == 0) {
    intent = new Intent(getActivity(), SoundActivity.class);
    } else if (index == 1) {
    intent = new Intent(getActivity(), DisplayActivity.class);
    }
    startActivity(intent);
    }
    }

}

这个类的代码并不长,我简单的说明一下。在 onCreateView 方法中加载了 menu_fragment 这个布局,这个布局里面包含了一个 ListView,然后我们对这个 ListView 填充了两个简单的数据 “Sound” 和 “Display” 。又在 onActivityCreated 方法中做了一个判断,如果 Activity 的布局中包含了 details_layout 这个元素,那么当前就是双页模式,否则就是单页模式。onItemClick 方法则处理了 ListView 的点击事件,发现如果当前是双页模式,就动态往 details_layout 中添加 Fragment,如果当前是单页模式,就直接打开新的 Activity。

我们把 MenuFragment 中引用到的其它内容一个个添加进来。新建 menu_fragment.xml 文件,加入如下代码:

<?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:id="@+id/menu_list"

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

然后新建 SoundFragment,里面内容非常简单:

public class SoundFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.sound_fragment, container, false);

这里 SoundFragment 需要用到 sound_fragment.xml 布局文件,因此这里我们新建这个布局文件,并加入如下代码:

<?xml version="1.0" encoding="utf-8"?>

同样的道理,我们再新建 DisplayFragment 和 display_fragment.xml 布局文件:

public class DisplayFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_fragment, container, false);
return view;
}
}

<?xml version="1.0" encoding="utf-8"?>

然后新建 SoundActivity,代码如下:

public class SoundActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_activity);
}

}

这个 Activity 只是加载了一个布局文件,现在我们来实现 sound_activity.xml 这个布局文件:

<?xml version="1.0" encoding="utf-8"?>

这个布局文件引用了 SoundFragment,这样写的好处就是,以后我们只需要在 SoundFragment 中修改代码,SoundActivity 就会跟着自动改变了,因为它所有的代码都是从 SoundFragment 中引用过来的。

好,同样的方法,我们再完成 DisplayActivity:

public class DisplayActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
}

}

然后加入 display_activity.xml:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/display_fragment"
android:name=“com.example.fragmentdemo.DisplayFragment”
android:layout_width=“match_parent”

最后

由于文章篇幅原因,我只把面试题列了出来,详细的答案,我整理成了一份PDF文档,这份文档还包括了还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 ,帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习。

举报

相关推荐

0 条评论