显示图示详情的书

来源:互联网 发布:网站搜索算法 编辑:程序博客网 时间:2024/05/01 07:35

1.MainActivity.java

   

package com.sh.appbooklistdetail;import android.app.Activity;import android.os.Bundle;/*(1)加载activity要显示的布局文件(two panes)        (2)实现Callbacks接口的onItemSelected(Integer id)方法:        Activity从FragmentA获取传入的ID,用来启动FragmentB。*/public class MainActivity 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_main);    }    //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()                .replace(R.id.book_detail_container, fragment)                .addToBackStack(null)                .commit();  /*注释:这一句等价于....   * FragmentManager Manager=getFragmentManager();   * FragmentTransaction Transaction=Manager.beginTransaction();   * Transaction.replace(R.id.book_detail_container, Manager);   * Transaction.commit();   * */    }}
2.增加 二个fragment
  a. 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        中取得标题的内容。*/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);    }}
  
   b.BookDetailFragment.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))        {            //获取启动该Fragment时传入的ITEM_ID参数并根据该ID获取BookContent的ITEM_MAP中的图书信息?           book =  BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));        }    }    //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)        {            ((TextView)view.findViewById(R.id.book_title)).setText(book.title);            ((TextView)view.findViewById(R.id.book_desc)).setText(book.desc);        }     return view;    }}
3.增加一个图书内容的类
  BookContent.java
  
/** * Created by Administrator on 2017/4/25. */public class BookContent {    //定义一个内部类,作为系统的业务对象    public static class Book    {          public Integer id;          public  String title;          public String desc;          public Book(Integer id,String title,String desc) //构造函数,初始化图书详情的"map键,标题,内容"          {              this.id = id;              this.title = title;              this.desc = desc;          }        @Override        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    {        addItem(new Book(1,"title_aaaaaa","desc1_aaaaaaaaa"));        addItem(new Book(2,"title_bbbbbb","desc1_bbbbbbbbb"));        addItem(new Book(3,"title_cccccc","desc1_ccccccccc"));        addItem(new Book(4,"title_dddddd","desc1_ddddddddd"));        addItem(new Book(5,"title_eeeeee","desc1_eeeeeeeee"));    }    private static  void  addItem(Book book)    {        ITEMS.add(book);        ITEM_MAP.put(book.id,book);            }}
4.xml文件 
  activity_main.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.sh.appfromactivitytofragment.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>
  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>


0 0
原创粉丝点击