Fragment应用及与Activity通信

/Fragment与Activity通信/src/com/example/fragment/BookContent.java

package com.example.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;
		}
		
		
		
	}
		//使用list集合记录系统所包含的book对象
		public static List<Book> ITEMS=new ArrayList<BookContent.Book>();
		//使用map集合记录系统所包含的book对象
		public static Map<Integer, Book> ITEM_MAP=new HashMap<Integer,Book>();
		
		static{
			//使用静态初始化代码   将book对象添加到list集合,map集合中
			addItem(new Book(1, "JAVA", "学习java,走遍天下都不怕"));
			addItem(new Book(2, "C#", "学习C#,走遍天下都不怕"));
			addItem(new Book(3, "C++", "学习C++,走遍天下都不怕,此人好懒-_-!"));
		}
		
		private static void addItem(Book book){
			ITEMS.add(book);
			ITEM_MAP.put(book.id, book);
		}


}

/Fragment与Activity通信/src/com/example/fragment/BookDetailFragment.java

package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BookDetailFragment extends Fragment{

	public static final String ITEM_ID="item_id";
	BookContent.Book book;//保存该fragment显示的Book对象
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments().containsKey(ITEM_ID)){
			book=BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));			
		}
	}
	
	@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;
	}
}

 

/Fragment与Activity通信/src/com/example/fragment/BookListFragment.java

package com.example.fragment;

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{

	private Callbacks mCallbacks;
	
	public interface Callbacks{
		public void onItemSelected(Integer id);
	}
	
	@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));
	}
	
	//当该Fragment被添加,显示到activity时,回调该方法
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		if(!(activity instanceof Callbacks)){
			throw new IllegalStateException(
					"BookListFragment所在的activity必须实现Callbacks接口!");
		}
		mCallbacks=(Callbacks)activity;
	}
	
	//当该fragment从他所属的activity中被删除时回调该方法
	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		mCallbacks=null;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		mCallbacks.onItemSelected(BookContent.ITEMS.get(position).id);
	}
	
	public void setActivityOnItemClick(boolean activa){
		getListView().setChoiceMode(
				activa?ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE);
		
	}
}


/Fragment与Activity通信/src/com/example/fragment/MainActivity.java

 

package com.example.fragment;

import com.example.fragment.BookListFragment.Callbacks;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity implements Callbacks{

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

	@Override
	public void onItemSelected(Integer id) {
		//创建bundle,准备向fragment传入参数
		Bundle arguments=new Bundle();
		arguments.putInt(BookDetailFragment.ITEM_ID, id);
		BookDetailFragment fragment=new BookDetailFragment();
		//向fragment传入参数
		fragment.setArguments(arguments);
		getFragmentManager().beginTransaction()
		.replace(R.id.book_detail_container, fragment).commit();
	}

}

/Fragment与Activity通信/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 
    style="?android:attr/textAppearanceLarge"
    android:id="@+id/book_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"/>

<TextView 
    style="?android:attr/textAppearanceMedium"
    android:id="@+id/book_desc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"/>
</LinearLayout>

/Fragment与Activity通信/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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 
        android:name="com.example.fragment.BookListFragment"
        android:id="@+id/book_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <FrameLayout 
        android:id="@+id/book_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">
        
    </FrameLayout>

</LinearLayout>




 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值