Share
Embed
Transcript
12 slides
-
How can I use Fragments? Include android-support-v4.jar (RightClick
on your project, Android Tools-> Add Support Library) Extend Fragment (has same methods of Activity plus someone else) Create a FragmentActivity (with a particular xml) to host your Fragment. Register your FragmentActivity in Manifest Android Programming -
Host FragmentActivity layout <?xml version="1.0" encoding="utf-8"?>! <fragment xmlns:android=" http://schemas.android.com/apk/res/android"! android:id="@+id/list_fragment”!
"android:name=” com.example.fragmentsexample.MyListFragment"! android:layout_width=”wrap_content"! android:layout_height=”wrap_content"! android:layout_weight="0.5" >! </fragment>! Android Programming -
Fragment’s only methods @Override public void onAttach(Activity activity) { //when
activity host this fragment } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) //called when this fragment was first created View v = inflater.inflate(R.layout.test, container); return v; } //Some other methods //All Activity methods Android Programming -
Make it double! Create a directory named “layout- land”
in res folder. Place a layout inside this folder with 2 fragments So that simple! Android Programming -
double_layout.xml <?xml version="1.0" encoding="utf-8"?>! <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:id="@+id/list_fragment"! android:name="com.example.fragmentsexample.MyListFragment"! android:layout_width="match_parent"! android:layout_height="match_parent"! android:layout_weight="0.5" >! </fragment>! ! <fragment! android:id="@+id/view_fragment"! android:name="com.example.fragmentsexample.ViewFragment"! android:layout_width="match_parent"! android:layout_height="match_parent"! android:layout_weight="0.5" >! </fragment>! ! </LinearLayout> Android Programming -
How can I communicate with the other fragment? Create
a listener (interface) to respond when a user tap on Item host activity of the left fragment should implements that interface Left fragment should contain a list (maybe a ListFragment? ;-) ) that use that interface Create a method on the right fragment to update the content =) Android Programming -
Wait… how can I know if the right fragment is
visible? RightFragment viewer =(RightFragment) getSupportFragmentManager(). findFragmentById( R.id.view_fragment); if(viewer!=null && viewer.isVisible()) { doTheJob(); } Android Programming -
Credits: Let’s do it for tablet image Are
your kidding me image Android Programming