Fragment

2015-07-14  本文已影响249人  Dear宅学长

Fragment用于创建动态的、多窗口的交互体验

创建一个Fragment

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Budle savedInstanceState) {
        return inflater.inflate(R.layout.article_view,container,false);
    }
}

用XML将fragment添加到Activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />
</LinearLayout>

注:使用XML布局的方式将Fragment添加到Activity时,Fragment是不能被动态移除的

动态添加

FragmentManager类提供了方法,在Activity运行时能够对Fragment进行添加、移除、替换

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.news_articles);
        if(findViewById(R.id.fragment_container) != null){
            if (saveInstanceState != null) {
                return;
            }
        }
        HeadlinesFragment firstFragment = new HeadelinesFragment();
        firstFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,firstFragment).commit();
    }
}

Fragment替换

当移除或替换一个Fragment并把它放回到返回站中时,被移除的Fragment的生命周期是stopped,当用户返回重新恢复这个Fragment,它的生命周期的restarts。如果没有放入返回栈,移除或替换时,它就被destoryed了

ArticleFragement newFragment = new AricleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION,postion);
newFragment.setArguments(args);
FramentTransaction transaction = getSupportFragmentManager().beginTransacation();
transaction.replace(R.id.fragment_container,newFragment);
transaction.commit();

Fragment之间的交互

为了更好的实现逻辑,Fragmet之间的交互需要通过它们关联的Activity,Fragment之间不能直接交互

定义一个接口

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    public interface onHeadlineSelectedLinstener {
        public void onArticleSelected(int position);    
    }

    @Override
    public void onAttach(Activity activiy) {
        super.onAttach(activity);
        try {
            mCallback = (OnHeadlineSelectedListener)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
        }
    }
    // .... 列表被点击,传递给父Activity
    @Overrid
    public void onListItemClick(ListView l,View v,int position,long id) {
        mCallback.onArticleSelected(position);
    }
}

实现接口

public class MainActivity extends FragmentActivity implements HeadlinesFragments.onHeadlineSelectedLinstener {
    public void onAricleSelected(int postion) {
        // 处理部分
    }
}

传递消息给Fragment

public claa MainActivity extends FragmentActivity implements HeadlinesFragments.onHeadlineSelectedLinstener {
    //...承接上部分
    public void onArticleSelected(int position) {
        ArticleFragment articleFrag = (ArticleFragment)getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            articleFrag.updateArticleView(position);
        } else {
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            transaction.commit();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读