Android学习笔记十四.深入理解fragment(二)之《图书详 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

Android学习笔记十四.深入理解fragment(二)之《图书详

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:19


深刻懂得fragment(二)
之《图书详情》拭魅战
    经由过程上一篇博文《深刻懂得fragment一》,我们进修了Android-Fragment的核心常识点。如今在此基本上,应用Fragment技巧开辟一款实用于大年夜屏幕手机/平板的查找图书详情的应用软件。该项目重要在于两方面,一是Activity、Fragment的源码实现;二是,构造界面资本文件的实现。
[img]http://img.blog.csdn.net/20150104174910282?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
1.res/../BookListFragment.java:
    自定义类,持续于ListFragment,无需实现OnCreateView()办法,用于Activity右边显示列表fragment。
(1)定义Callbacks接口:定义一个回调接口Callback,用于实现该Fragment与它地点的Activity交互;
(2)实现onCreate(Bundle savedInstanceState)办法:经由过程Adapter所供给的多个列表项,设置Fragment列表显示的列表项;
(3)实现onAttach(Activity activity)办法:将Fragment添加并显示到Acitvity中,并将传入的activity对象强迫类型转化为 Callbacks接口对象,以便调用接口公共办法onItemSelected(Integer id)响应用户单击的某列表项;
(4)实现ListFragment的onListItemClick(ListView l, View v, int position, long id)办法:
    当用户点击Acitivity中的某项列表时,onListItemClick办法被激发。在这个办法中调用接口的onItemSelected来竽暌闺activity共享事宜。onItemSelected()传入的参数id是列表的被选中的行ID,另一个fragment(B)( BookDetailFragment )用这个ID来大年夜法度榜样的ContentProvider中取得标题标内容。
package com.example.android_fragment_1;
import android.app.Activity; 
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class BookListFragment extends ListFragment { 
  //1.定义一个回调接口Callback,用于实现该Fragment与它地点的Activity交互(留意:该接口的实现须要在Activit中) 
 private Callbacks mCallbacks;	//Callbacks接口对象
 public interface Callbacks
 {
  public void onItemSelected(Integer id);	//参数为Map集合的键
 }
 
 //2.onCreate办法中为该ListFragment设置Adapter,让该ListFragment显示该Adapter所供给的多个列表项
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),//
    android.R.layout.simple_list_item_activated_1,
    android.R.id.text1,
    BookContent.ITEMS));	 //
 }
 
 //3.调用该办法将Fragment添加并显示到Acitvity中
 @Override
 public void onAttach(Activity activity) {
  // TODO Auto-generated method stub
  super.onAttach(activity);
  //a.如不雅Activity中没有实现Callbacks接口,抛出异常
  if(!(activity instanceof Callbacks))
  {
   throw new IllegalStateException("异常:BookListFragment地点的Activity必须实现Callback接口!");
  }
  //把该Activity当成Callbacks对象(就是这一句导致出现NullPointerException缺点)
        mCallbacks=(Callbacks)activity;
 }
 
 //4.当该Fragment大年夜他所属的Acitivity中被删除时,调用该办法
 @Override
 public void onDetach() {
  super.onDetach();
  mCallbacks=null;	//将mCallbacks赋值为null
 }
 
 //5.当用户单击某列表项时激发该回调办法
 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  mCallbacks.onItemSelected(BookContent.ITEMS.get(position).id);//激发mCallbacks接口的onItemSelected办法
 }
public void setActivateOnItemClick(boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE);
 }
}


2.res/../ BookDetailFragment.java:
      onItemSelected()传入的参数id是列表的被选中的行ID, BookDetailFragment 用这个ID来大年夜法度榜样的ContentProvider中取得标题标内容。  
(1)实现onCreate(Bundle savedInstanceState)办法:
    获取启动该Fragment时传入的ITEM_ID参数并根据该ID获取BookContent的ITEM_MAP中的图手札息
(2)实现onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)办法:
    a.为图书详情Fragment加载一个界面构造文件,为两个文本框;
    b.根据传入的参数ID来更新View容器,使文本框显示不合的内容;
package com.example.android_fragment_1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/*功能:
 * 应用Fragment将会显示加载一份简单的界面构造文件,并根据传入的参数来更新
 * 界面组件*/
public class BookDetailFragment extends Fragment {
 public static final String ITEM_ID="item_id";
 BookContent.Book book;	 //保存该Fragment显示的book对象
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //如不雅启动该Fragment时包含了ITEM_ID参数,个中 Map containsKey(String Key) 断定key有没有对应的value值; 有,则返回true 没有,则返回false
  //大年夜Bundle对象中获取传入的参数(键值对)
  if(getArguments().containsKey(ITEM_ID))
  {
       book=BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));
   //获取启动该Fragment时传入的ITEM_ID参数并根据该ID获取BookContent的ITEM_MAP中的图手札息?
  }
 }
 //2.重写该办法:该办法返回的View将作为Fragment显示的组件
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  //a.加载/res/layout/目次下的fragment_book_detail.xml构造文件,返回一个view目标使该fragment的构造显示在Activity中
  View view=inflater.inflate(R.layout.fragment_book_detail, //指明当前fragment的资本文件
         container, //父容器控件
         false);	 //注解是否连接该构造和其父容器控件(这里体系已经插入了构造到父容器中)
  /*将图手札息中的标题、属性显示在容器的两个文本框中*/
  if(book!=null)
  {
  //b.让book_title文本框显示book对象的title属性
   ((TextView) view.findViewById(R.id.book_title)).setText(book.title);
  //c.让book_desc文本框显示book对象的desc属性
   ((TextView) view.findViewById(R.id.book_desc)).setText(book.desc);
  }
  return view;
 }
 
}

个中界面资本构造文件为/res/layout/fragment_book_detail.xml
<?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/textAppearanceLarge"
     android:id="@+id/book_desc"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:padding="16dp"/>
</LinearLayout>
3.res/../SelectBookActivity.java:
应用法度榜样主Acitivity
(1)加载activity要显示的构造文件(two panes)
(2)实现Callbacks接口的onItemSelected(Integer id)办法:
      Activity大年夜FragmentA获取传入的ID,用来启动FragmentB。
package com.example.android_fragment_1;
import android.app.Activity; 
import android.os.Bundle;
public class SelectBookActivity extends Activity implements BookListFragment.Callbacks 
{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
 //1.加载Activity本身的构造资本/res/layout/main.xml并显示(two panes)
  setContentView(R.layout.activity_book_twopane);
 }
 
 //2.实现Callbacks接口必须实现的办法,用于实现该Fragment与它地点的Activity交互(即选择点击哪个列表项,向Fragment传入参数id)//更新Activity右边内容
 
 @Override
 public void onItemSelected(Integer id) {
  //a.创建Bundle,预备向Fragment传入参数
  Bundle arguments=new Bundle();
  arguments.putInt(BookDetailFragment.ITEM_ID, id);	//装入值id到"item_id"键
  //b.创建BookDetailFragment对象,并项Fragment传入参数
  BookDetailFragment fragment=new BookDetailFragment();
  fragment.setArguments(arguments);
  //c.应用fragment调换book_detail_container容器当前显示的Fragment
  getFragmentManager().beginTransaction()
    .WordStr(R.id.book_detail_container, fragment)
    .commit();
 
  /*注释:这一句等价于....
   * FragmentManager Manager=getFragmentManager();
   * FragmentTransaction Transaction=Manager.beginTransaction();
   * Transaction.WordStr(R.id.book_detail_container, Manager);
   * Transaction.commit();
   * */
 }
}

个中界面资本构造文件为/res/layout/activity_book_twopane.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个水等分列的LinearLayout,并指定应用中等分隔条 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle">
    <!-- 应用资本文件方法:添加一个fragment到Activity中 -->
    <fragment
        android:name="com.example.android_fragment_1.BookListFragment"
        android:id="@+id/book_list"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        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的显示界面:
 左边将会显示一个ListFragment,右边只是一个FrameLayout容器
 个中FrameLayout容器将会动态更新个中显示的Fragment -->


4.res/../BookContent.java:
    用于模仿体系的数据模型
 (1)List集合为左边fragment供给图书(标题)列表项数据
    public static List<Book> ITEMS=new ArrayList<Book>();
对于List集合来说,book为(1,"猖狂Android教材","小我评价:这本书很好,就是有点厚!"2...3...)
 (2)Map集合为右边fragment供给图书详情(标题、属性)数据
    public static Map<Integer,Book> ITEM_MAP =new HashMap<Integer,Book>();
对应Map集合来说,键book.id<--->值(1.......2.......3......)
package com.example.android_fragment_1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BookContent {
 //1.定义一个内部类,作为体系的营业对象
 public static class Book
 {
  public Integer id;	 //Map键
  public String title;
  public String desc;
  public Book(Integer id,String title,String desc)//构造函数,初始化图书详情的"map键,标题,内容"
  {
   this.id=id;
   this.title=title;
   this.desc=desc;
  }
  public String toString()
  {
   return title;
  }
 }
 //2.应用List集合记录体系所包含的Book对象ITEMS,应用它调用其add(对象)办法项List集合中添加列表项
 public static List<Book> ITEMS=new ArrayList<Book>();
 //3.应用Map集合记录体系所包含的Book对象ITEM_MAP,应用它调用put(Key,Value)办法向Map集合中添加列表项
 public static Map<Integer,Book> ITEM_MAP =new HashMap<Integer,Book>();
 static{
   //应用静态初始化代码,将Book对象添加到List集合、Map集合中
   addItem(new Book(1,"猖狂Android教材","小我评价:这本书很好,就是有点厚!"));
   addItem(new Book(2,"数学之美","小我评价:来自天然语音的使者"));
   addItem(new Book(3,"大年夜话数据构造","小我评价:不知道如何,据说很不错"));
 }
 private static void addItem(Book book) {
  // TODO Auto-generated method stub
  ITEMS.add(book);	 //添加List集合中列表项
  ITEM_MAP.put(book.id,book);//添加Map集合中列表项
 }
}

/*注释:
 * static{},称为static代码块,也叫静态代码块。是在类中自力于类成员的static语句块,可以有多个且地位可以随便放。
 * 它不属于任何的办法体内,JVM加载类时会履行这些静态的代码块,如不雅有多个则按先后次序履行且每个代码块智会被履行依次。
 * 比如,我们可以应用静态代码块可以对一些statci变量进行赋值*/
[img]http://img.blog.csdn.net/20150104175040109?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

相关案例查看更多