Skip to content

Instantly share code, notes, and snippets.

@dnkm
Last active August 29, 2015 14:18
Show Gist options
  • Save dnkm/c0f6e285f20029710e38 to your computer and use it in GitHub Desktop.
Save dnkm/c0f6e285f20029710e38 to your computer and use it in GitHub Desktop.
Android Tutorial - Chapter 4 Fragments! (using support library)
// fragment = nested sub-activity w/ its own layout and lifecycle. (modular section of an activity)
// fragment receives the same lifecycle callbacks as its parent activity does
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.article_view, container, false);
}
}
<!-- res/layout-large/news_articles.xml -->
<!-- news article layout will have 2 fragments when the screen is large -->
<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.....fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"/> <!-- obmitting layout attrs -->
<fragment android:name="com.example.....fragments.ArticleFragment"
android:id="@+id/article_fragment"/> <!-- obmitting layout attrs -->
</LinearLayout>
// < API 11 - extend FragmentActvity or ActionBarActivity (if using appcompat).
// >= API 11 - use Fragment class
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_article);
}
}
// dynamically adding fragments allows removal and replacement.
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_article);
if (findViewById(R.id.fragment_container != null) {
if (savedInstanceState != null) {
return; // avoid duplicate
}
// handle intent if needed
ArticleFragment frag = new ArticleFragment();
frag.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, frag).commit();
}
}
}
// res/layout/news_article.xml
// Rule - Activity layout must include a container "VIew" in which the fragment is inserted.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container" />
// create
ArticleFragment newFrag - new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFrag.setArguments(bundle);
FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
// replace!
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null); // add transaction itself to the back stack (allows for back btn navigation)
// commit
transaction.commit();
// frag to frag communication is done thru the associated activity. (NEVER directly)
puiblic class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// define interface to implement in the activity class
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttack(activity);
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must impplement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onArticleSelected(position);
}
}
/*
* Activity
*/
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener {
public void onArticleSelected(int pos) {
// display something..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment