开发者

Android: Switch views in tabs without leaving current tab. Like USA Today App

I'm in the process of building a sports team app and have created my own tab bar as opposed to using Google's tab code. I like the way USA Today's App works. USA Today App When you choose amongst the different categories of news, only the view below changes, it doesn't sw开发者_C百科itch to a completely new screen/activity. How are they doing this?


From the screenshot, it looks like the different categories are contained within a Gallery view. Through setOnItemSelectedListener, you can register for a callback when the user chooses a different item. You can then programmatically change the main View or populate it with new data.

For example, say you have a ViewFlipper and a Gallery defined in your layout where the order of the items in your Gallery correspond to the ordering of the Views in the ViewFlipper. In your Activity, you use findViewById to get references to those Views(the flipper and gallery variables). You could then do:

    myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            flipper.setDisplayedChild(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });


You can use ActivityGroup to switch between different view within a current tab. here is a demo for that. http://androidgenuine.com/

Complete Demo of ActivityGroup


I don't know how they have implemented that feature in their app. They are most likely just altering the View tree, i.e. replacing one View with another programmatically. The biggest problem with this is that you usually need a lot of code to support each of the Views that could be shown within a particular Activity -- code for loading data, handling user taps, etc. You could put a lot of that code inside a custom View class instead of having it in the Activity. If putting "business logic" inside a View seems perverse, then I would recommend using Fragments. Then changing tabs can be handled using FragmentTransactions. These were added in Android 3.0, but there is a compatibility package that let's you use these on Android 1.6+.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜