Share
Embed
Transcript
71 slides
-
• Smaller View Controllers • Reusable UI & Logic Components
• Address device fragmentation -
• Smaller View Controllers • Reusable UI & Logic Components
• Address device fragmentation • Decomposition of application code -
Adding Fragments Statically <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/puppy_list" android:name="com.kenodoggy.masterdetailflow.PuppyListFragment" android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="16dp" android:layout_marginRight="16dp" tools:context=".PuppyListActivity" tools:layout="@android:layout/list_content"/> -
public static MyFragment newInstance(int position) { MyFragment fragment = new
MyFragment(); Bundle args = new Bundle(); args.putInt(ARG_POSITION, position); fragment.setArguments(args); return fragment; } New Instances -
public static MyFragment newInstance(int position) { MyFragment fragment = new
MyFragment(); Bundle args = new Bundle(); args.putInt(KEY_POSITION, position); fragment.setArguments(args); return fragment; } New Instances -
setRetainInstance(true) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //
Retain this fragment across configuration changes. setRetainInstance(true); } -
Navigation Layout for Phone <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/puppy_list" android:name="com.kenodoggy.masterdetailflow.PuppyListFragment" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" tools:context=".PuppyListActivity" tools:layout="@android:layout/list_content"/> -
Detail Layout for Phone <android.support.design.widget.CoordinatorLayout> <android.support.design.widget.AppBarLayout> <android.support.design.widget.CollapsingToolbarLayout> <android.support.v7.widget.Toolbar /> </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/puppy_detail_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior=“@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout> -
List/Detail Layout for Tablet <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal"> <fragment
android:id="@+id/puppy_list" android:name="com.kenodoggy.masterdetailflow.PuppyListFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" tools:layout="@android:layout/list_content"/> <FrameLayout android:id="@+id/puppy_detail_container" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3"/> </LinearLayout> -
Dual or Single Pane? if (findViewById(R.id.puppy_detail_container) != null) { //
The detail container view will be present only in the // large-screen layouts (res/values-large and // res/values-sw600dp). If this view is present, then the // activity should be in two-pane mode. mTwoPane = true; ... code specific to two pane layout } -
The Parent Activity Layout <android.support.design.widget.CoordinatorLayout> <android.support.design.widget.AppBarLayout> <android.support.v7.widget.Toolbar /> <android.support.design.widget.TabLayout
android:id="@+id/tabs" android:layout_width=“wrap_content" android:layout_height="wrap_content" app:tabMode="scrollable"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> -
The Parent Activity Layout <android.support.design.widget.CoordinatorLayout> <android.support.design.widget.AppBarLayout> <android.support.v7.widget.Toolbar /> <android.support.design.widget.TabLayout
android:id="@+id/tabs" android:layout_width=“wrap_content" android:layout_height="wrap_content" app:tabMode="scrollable"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> -
Setting up our ViewPager ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
/* add the Fragments to the ViewPagerAdapter */ for(String title : mPuppyTitles) { adapter.addFrag(PuppyFragment.newInstance(index++), title); } mViewPager.setAdapter(adapter); -
Creating our PuppyFragment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
if (getArguments() != null) { mPageNumber = getArguments().getInt(ARG_POSITION); } } -
Loading the UI @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_puppy, container, false); int imageId = getResources().getIdentifier(mPuppyImages[mPageNumber], "drawable", getActivity().getPackageName()); ImageView image = (ImageView) view.findViewById(R.id.puppy_picture); image.setImageResource(imageId); return view; } -
Loading the UI @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_puppy, container, false); int imageId = getResources().getIdentifier(mPuppyImages[mPageNumber], "drawable", getActivity().getPackageName()); ImageView image = (ImageView) view.findViewById(R.id.puppy_picture); image.setImageResource(imageId); return view; } -
Fragment fragment = DetailFragment.newInstance(puppy, position); FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction(); // do replace and commit operation transaction.replace(R.id.detail_container, fragment).commit(); -
Fragment fragment = DetailFragment.newInstance(puppy, position); FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction(); // do replace, add to backstack and commit operation transaction.replace(R.id.detail_container, fragment) .addToBackStack(“details”) // optional name for this back stack state, or null .commit(); -
public class SampleDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { @Override public
Dialog onCreateDialog(Bundle savedInstanceState) { View view = getActivity() .getLayoutInflater() .inflate(R.layout.fragment_sample_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); return(builder .setView(view) .setTitle(title) .setPositiveButton(R.string.close, null) .create()); } } -
public class SampleDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { @Override public
Dialog onCreateDialog(Bundle savedInstanceState) { View view = getActivity() .getLayoutInflater() .inflate(R.layout.fragment_sample_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); return(builder .setView(view) .setTitle(title) .setPositiveButton(R.string.close, null) .create()); } } -
public class SampleDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { @Override public
Dialog onCreateDialog(Bundle savedInstanceState) { View view = getActivity() .getLayoutInflater() .inflate(R.layout.fragment_sample_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); return(builder .setView(view) .setCustomTitle(title) .setPositiveButton(R.string.close, null) .create()); } } -
@Override public void onAttach(Activity activity) { super.onAttach(activity); try { mDialogDismissedCallback
= (OnDialogDismissed)activity; } catch (ClassCastException cce) { Log.e("Error", getClass().getSimpleName() + ", calling Activity must implement OnDialogDismissed"); } } -
public class MainActivity extends AppCompatActivity implements SampleDialogFragment.OnDialogDismissed { // ...
other methods and implementation not shown @Override public void onDialogDismissed(String whichSalutation) { if (whichSalutation.equals(SampleDialogFragment.GOODBYE)) { Toast.makeText(this, "Thank you!", Toast.LENGTH_SHORT).show(); } } } -
@Override public void onClick(DialogInterface dialog, int which) { // display
a toast when the dialog is dismissed for GOODBYE only if (mSalutation.equals(GOODBYE)) { mDialogDismissedCallback.onDialogDismissed(GOODBYE); } }