Fragment笔记

来源:互联网 发布:android 开机启动优化 编辑:程序博客网 时间:2024/05/19 12:41

1.遇到问题
官网上fragment的例子,在一个Activity上attach两个fragment,然后出现了ClassCastException异常(com.example.fragment1 cannot be cast view.view)。
通过百度找到错误,原来我导入的是v4包的fragment,所以Activity要继承FragmentActivtiy.

2.动态使用fragment
FragmentManager fm = getFragmentManager();//v4包:getSupportFragmentManager
FragmentTransaction ft = fm.beginTransaction();
ft.replace(int containerViewId,Fragment fragment);
ft.commit();

//int containerViewId:activty中的容器ID,把fragment放在该容器中。//fragment:要显示的fragmentFragmentTransaction 还有add(),remove(),hide(),show();detach(),attach()等操作。ft.addToBackStack(null);  添加到回退栈.

3.Fragment交互:所有fragment之间的交互需要通过他们关联的activity,两个fragment之间不应该直接交互。
(1).定义一个接口(在Fragment中定义接口)

public class HeadlinesFragment extends ListFragment {    OnHeadlineSelectedListener mCallback;    // Container Activity must implement this interface    public interface OnHeadlineSelectedListener {        public void onArticleSelected(int position);    }    @Override    public void onAttach(Activity activity) {        super.onAttach(activity);        // This makes sure that the container activity has implemented        // the callback interface. If not, it throws an exception        try {            mCallback = (OnHeadlineSelectedListener) activity;        } catch (ClassCastException e) {            throw new ClassCastException(activity.toString()                    + " must implement OnHeadlineSelectedListener");        }    }     @Override        public void onListItemClick(ListView l, View v, int position, long id) {            // Send the event to the host activity            mCallback.onArticleSelected(position);        }        ...}

(2)在Activity中实现接口,传递消息给fragment

public static class MainActivity extends Activity        implements HeadlinesFragment.OnHeadlineSelectedListener{    ...    public void onArticleSelected(int position) {        // The user selected the headline of an article from the HeadlinesFragment        // Do something here to display that article        //获取fragment实例,调用其public方法来传递消息        ArticleFragment articleFrag = (ArticleFragment)                getSupportFragmentManager().findFragmentById(R.id.article_fragment);        if (articleFrag != null) {          // If article frag is available, we're in two-pane layout...              articleFrag.updateArticleView(position);        } else {        // Otherwise, we're in the one-pane layout and must swap frags...            ArticleFragment newFragment = new ArticleFragment();            Bundle args = new Bundle();            args.putInt(ArticleFragment.ARG_POSITION, position);            newFragment.setArguments(args);            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();            // Replace whatever is in the fragment_container view with this fragment,            // and add the transaction to the back stack so the user can navigate back            transaction.replace(R.id.fragment_container, newFragment);            transaction.addToBackStack(null);            transaction.commit();        }    }}
0 0
原创粉丝点击