Android Layout Fragment/Activity Confusion

余生颓废 提交于 2019-12-13 05:21:30

问题


I am making my first android application with the ActionBarSherlock.

The application will always have an action bar consisting of 3 tabs (first tab selected by default). The app could be extended for use with a tablet.

I have been searching the web, and following the android development guides, however I am finding a few things confusing. The first tab screen will be a list view with a list of items, onitemselected should send the user to a screen which features more details about that item.

When should I use a fragment? Should each tab be a fragment? Or, should each tab call a new activity, which consists of fragments? And, if using fragments, should I place them in different classes, or embed them within an activity??

Appreciate any help, thanks.


回答1:


you should probably read these two links first.

http://android-developers.blogspot.com/2011/09/preparing-for-handsets.html

http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html

If you plan to make an app that work on both phone and tablet. It is a good idea to use a fragment, and then use a shell activity to wrap that fragment.




回答2:


My experience with Fragments is mostly on ViewPager, so I am not entirely sure if it applies here.

In Android, you should use Fragments as much as possible. As a general rule of thumb, imagine you are translating the UI from phones to tablets, elements that can stay together in the same configuration should be a Fragment.

There is a Fragment subclass called ListFragment, so you might want to look into that for your first tab. (ListFragment is for Fragment like ListActivity is for Activity)

There is also a tutorial I found to deal with fragments. Did not really look into it but I hope it helps.

http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

As for ActionBar / ActionBarSherlock, I have absolutely no experience withit so someone might want to add to that.



来源:https://stackoverflow.com/questions/9106063/android-layout-fragment-activity-confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!