兼顾屏幕分辨率的应用---Activity与Fragment的交互

在实际开发中,为了适应当前的各种手机,平板等,我们就会在开发中兼顾屏幕分辨率,开发中可以在res/目录下为大屏幕,600dpi的屏幕建立相应的资源文件夹:values-large,values-sw600dp,在该文件夹下建立一个名为refs.xml的引用资源文件.该引用资源文件专门用于定义各种引用项.下面实例的引用资源文件中只有一项,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 定义activity_book_list实际引用@layout/activity_book_twopane资源 -->
    <item type="layout" name="activity_book_list">
        @layout/activity_book_twopane</item>
</resources>
上面的引用资源文件指activity_book_list引用res/layout/目录下的activity_book_twopane.xml界面布局文件.

然后在Activity加载R.layout.activity_book_list时将会根据运行平台的屏幕大小自动选择界面布局文件:在大屏幕的平板电脑上,R.layout.activity_book_list将会变成res/layout/目录下的activity_book_twopan界面布局文件;在小屏幕的手机上,R.layout_activity_book_list依然引用res/layout目录下的R.layout_activity_book_list界面布局文件,

下面是activity_book_list和activity_book_twopan界面布局文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 直接使用BookListFragment作为界面组件 -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.example.niu.seniorfra.BookListFragment"
        android:id="@+id/book_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"/>
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个水平排列的LinearLayout,并指定使用中等分隔条 -->
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginLeft="16dp"
   android:layout_marginRight="16dp"
   android:divider="?android:attr/dividerHorizontal"
   android:showDividers="middle">
   <!-- 添加一个Fragment -->
   <fragment
      android:name="com.example.niu.seniorfra.BookListFragment"
      android:id="@+id/book_list"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" />
   <!-- 添加一个FrameLayout容器 -->
   <FrameLayout
      android:id="@+id/book_detail_container"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="3" />
</LinearLayout>
从代码知在activity_book_list.xml文件中看出,该布局文件仅仅显示BookListFragment组件,表明该界面布局文件只是显示图书列表.该BookListFragment组件代码如下:

public class BookListFragment extends ListFragment
{
    private Callbacks mCallbacks;
    // 定义一个回调接口,该Fragment所在Activity需要实现该接口
    // Fragment将通过该接口与它所在的Activity交互
    public interface Callbacks
    {
        public void onItemSelected(Integer id);
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 为该ListFragment设置Adapter
        setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1, BookContent.ITEMS));  //    }
    // 当该Fragment被添加、显示到Activity时,回调该方法
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        // 如果Activity没有实现Callbacks接口,抛出异常
        if (!(activity instanceof Callbacks))
        {
            throw new IllegalStateException(
                    "BookListFragment所在的Activity必须实现Callbacks接口!");
        }
        // 把该Activity当成Callbacks对象
        mCallbacks = (Callbacks)activity;
    }
    // 当该Fragment从它所属的Activity中被删除时回调该方法
    @Override
    public void onDetach()
    {
        super.onDetach();
        // mCallbacks赋为null        mCallbacks = null;
    }
    // 当用户单击某列表项时激发该回调方法
    @Override
    public void onListItemClick(ListView listView
            , View view, int position, long id)
    {
        super.onListItemClick(listView, view, position, id);
        // 激发mCallbacksonItemSelected方法
        mCallbacks.onItemSelected(BookContent
                .ITEMS.get(position).id);
    }
    public void setActivateOnItemClick(boolean activateOnItemClick)
    {
        getListView().setChoiceMode(
                activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
                        : ListView.CHOICE_MODE_NONE);
    }
}
接下来加载该布局文件的Activity将会针对不同的屏幕分辨率分别进行处理,代码如下:
public class BookListActivity extends Activity implements
      BookListFragment.Callbacks
{
   // 定义一个旗标,用于标识该应用是否支持大屏幕
   private boolean mTwoPane;
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      // 指定加载R.layout.activity_book_list对应的界面布局文件
      // 但实际上该应用会根据屏幕分辨率加载不同的界面布局文件
      setContentView(R.layout.activity_book_list);
      // 如果加载的界面布局文件中包含IDbook_detail_container的组件
      if (findViewById(R.id.book_detail_container) != null)
      {
         mTwoPane = true;
         ((BookListFragment) getFragmentManager()
               .findFragmentById(R.id.book_list))
               .setActivateOnItemClick(true);
      }
   }
   @Override
   public void onItemSelected(Integer id)
   {
      if (mTwoPane)
      {
         // 创建Bundle,准备向Fragment传入参数
         Bundle arguments = new Bundle();
         arguments.putInt(BookDetailFragment.ITEM_ID, id);
         // 创建BookDetailFragment对象
         BookDetailFragment fragment = new BookDetailFragment();
         // Fragment传入参数
         fragment.setArguments(arguments);
         // 使用fragment替换book_detail_container容器当前显示的Fragment
         getFragmentManager().beginTransaction()
               .replace(R.id.book_detail_container, fragment).commit();
      }
      else
      {
         // 创建启动BookDetailActivityIntent
         Intent detailIntent = new Intent(this, BookDetailActivity.class);
         // 设置传给BookDetailActivity的参数
         detailIntent.putExtra(BookDetailFragment.ITEM_ID, id);
         // 启动Activity
         startActivity(detailIntent);
      }
   }
}
由于该实例为大屏幕设备定义了引用资源文件,因此该应用将会根据大屏幕加载对应的界面布局文件,因此上面程序中白色背景红色字体的代码会判断界面布局是否包含ID为book_detail_container的组件,如果包含该组件,则表明是适应大屏幕的""双屏"界面;否则,该程序界面上只包含一个简单的BookListFragment列表组件.此时当用户单击列表项时,程序不再是简单地更换Fragment,而是启动BookDetailActivity来显示指定图书的详细信息.

注:BookDetailActivity只是一个简单的封装,它将直接复用前面已有的BookDetailFragment.,它们布局文件和java代码如下:

fragment_book_detail:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <!-- 定义一个TextView来显示图书标题 -->
   <TextView
      style="?android:attr/textAppearanceLarge"
      android:id="@+id/book_title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="16dp"/>
   <!-- 定义一个TextView来显示图书描述 -->
   <TextView
      style="?android:attr/textAppearanceMedium"
      android:id="@+id/book_desc"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="16dp"/>
</LinearLayout>
activity_book_detail:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/book_detail_container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>
java代码如下:

public class BookDetailFragment extends Fragment
{
   public static final String ITEM_ID = "item_id";
   // 保存该Fragment显示的Book对象
   BookContent.Book book;
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      // 如果启动该Fragment时包含了ITEM_ID参数
      if (getArguments().containsKey(ITEM_ID))
      {
         book = BookContent.ITEM_MAP.get(getArguments()
            .getInt(ITEM_ID));
      }
   }
   // 重写该方法,该方法返回的View将作为Fragment显示的组件
   @Override
   public View onCreateView(LayoutInflater inflater
      , ViewGroup container, Bundle savedInstanceState)
   {
      // 加载/res/layout/目录下的fragment_book_detail.xml布局文件
      View rootView = inflater.inflate(R.layout.fragment_book_detail,
            container, false);
      if (book != null)
      {
         // book_title文本框显示book对象的title属性
         ((TextView) rootView.findViewById(R.id.book_title))
            .setText(book.title);
         // book_desc文本框显示book对象的desc属性
         ((TextView) rootView.findViewById(R.id.book_desc))
            .setText(book.desc);
      }
      return rootView;
   }
}
public class BookDetailActivity extends Activity
{
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      // 指定加载/res/layout目录下的activity_book_detail.xml布局文件
      // 该界面布局文件内只定义了一个名为book_detail_containerFrameLayout
      setContentView(R.layout.activity_book_detail);
      // ActionBar上应用图标转换成可点击的按钮
      getActionBar().setDisplayHomeAsUpEnabled(true);
      if (savedInstanceState == null)
      {
         // 创建BookDetailFragment对象
         BookDetailFragment fragment = new BookDetailFragment();
         // 创建Bundle对象,
         Bundle arguments = new Bundle();
         arguments.putInt(BookDetailFragment.ITEM_ID, getIntent()
               .getIntExtra(BookDetailFragment.ITEM_ID, 0));
         // Fragment传入参数
         fragment.setArguments(arguments);
         // 将指定fragment添加到book_detail_container容器中
         getFragmentManager().beginTransaction()
               .add(R.id.book_detail_container, fragment).commit();
      }
   }
   @Override
   public boolean onOptionsItemSelected(MenuItem item)
   {
      if (item.getItemId() == android.R.id.home)
      {
         // 创建启动BookListActivityIntent
         Intent intent = new Intent(this, BookListActivity.class);
         // 添加额外的Flag,将Activity栈中处于FirstActivity之上的Activity弹出
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         // 启动intent对应的Activity
         startActivity(intent);
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}
上面白色背景代码创建BookDetailFragment,并让该Activity显示该Fragment即可.

除此之外,该Activity还启用了ActionBar上的应用程序图标,允许用户点击该图标返回程序的主Activity.

ok:在大屏幕设备上运行代码示例,效果图如下,单击列表项后效果图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值