Android Fragment 示例

    xiaoxiao2021-03-26  26

    一、效果


    二、编码

    (1)model类,存放书籍的相关信息

    public class BookContent { public static class Book{ public Integer id; public String title; public String desc; public Book(Integer id,String title,String desc){ this.id = id; this.title = title; this.desc=desc; } @Override public String toString() { return title; } } public static List<Book> ITEMS = new ArrayList<Book>(); public static Map<Integer,Book> ITEMS_MAP = new HashMap<>(); static { // 使用静态初始化代码,将Book对象添加到List集合、Map集合中 addItem(new Book(1, "疯狂Java讲义" , "一本全面、深入的Java学习图书,已被多家高校选做教材。")); addItem(new Book(2, "疯狂Android讲义" , "Android学习者的首选图书,常年占据京东、当当、 " + "亚马逊3大网站Android销量排行榜的榜首")); addItem(new Book(3, "轻量级Java EE企业应用实战" , "全面介绍Java EE开发的Struts 2、Spring 3、Hibernate 4框架")); } private static void addItem(Book book) { ITEMS.add(book); ITEMS_MAP.put(book.id, book); } }

    (2)创建Fragment需要重写如下onCreate()和onCreateView(),onCreate()是创建Fragment对象时初始化的方法,而onCreateView()是Fragment绘制界面回调的方法。

    /** * 呈现详细资料 * Created by Sean on 2017/2/3. */ public class BookDetailFragment extends Fragment { public static final String ITEM_ID="item_id"; BookContent.Book book; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments().containsKey(ITEM_ID)){ book = BookContent.ITEMS_MAP.get(getArguments().getInt(ITEM_ID)); } } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_book_detail, container,false); if (book!=null){ ((TextView)rootView.findViewById(R.id.book_title)).setText(book.title); ((TextView) rootView.findViewById(R.id.book_desc)) .setText(book.desc); } return rootView; } }

    (3)目录页面使用ListFragment,无需重写onCreateView()方法,直接调用ListFragment的setAdapter()即可。

    public class BookListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(), android.R.layout.simple_list_item_activated_1, android.R.id.text1,BookContent.ITEMS)); } @Override public void onAttach(Context context) { super.onAttach(context); if (!(context instanceof Callbacks)){ throw new IllegalStateException( "BookListFragment所在的Activity必须实现Callbacks接口!" ); } mCallback = (Callbacks) context; } @Override public void onDetach() { super.onDetach(); mCallback=null; } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Log.d("BookListFragment","onItemSelected"); mCallback.onItemSeledted(BookContent.ITEMS.get(position).id); } public void setActivateOnItemClick(boolean activateOnItemClick){ getListView().setChoiceMode( activateOnItemClick?ListView.CHOICE_MODE_SINGLE: ListView.CHOICE_MODE_NONE); } }

    (4)Fragment同Activity通过回掉建立通信,并使用Fragment事务方式更新Fragment。

    public Callbacks mCallback; /** * 负责与Activity进行通信 */ public interface Callbacks{ public void onItemSeledted(Integer id); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Log.d("BookListFragment","onItemSelected"); mCallback.onItemSeledted(BookContent.ITEMS.get(position).id); } public class MainActivity extends AppCompatActivity implements BookListFragment.Callbacks { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_book_twopane); } @Override public void onItemSeledted(Integer id) { Log.d("mainActivity","onItemSelected"); Bundle bundle = new Bundle(); bundle.putInt(BookDetailFragment.ITEM_ID,id); BookDetailFragment fragment = new BookDetailFragment(); fragment.setArguments(bundle); getFragmentManager().beginTransaction() .replace(R.id.book_detail_container,fragment) .commit(); } }

    (5)Fragment布局

    <?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.project.seanma.fragmetnttest.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> <?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>

    源码地址:https://github.com/codingma/FragmentTest

    转载请注明原文地址: https://ju.6miu.com/read-659080.html

    最新回复(0)