Dynamic UI with sliding menu and actionbarsherlock

后端 未结 3 1555
再見小時候
再見小時候 2021-02-01 11:14

Trying to achieve a dynamic UI with facebook like sliding menu and actionbarsherlock .First i have look into android documentation which introduce fragment to handle dynamic but

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 11:31

    I don't think it will work like that with Fragments, I was looking for a solution as well and ended up adding the fragments by hand.

    I'm working on something similar like this, but for me there was also the case of opening WebViews to designated URL's. So the "above" screen would always update on any click.

    To control the behaviour of this I created a MenuItemResource object, which basically holds the properties, like the ID of the icon, the name of the menu item and the URL.

    public class MenuItemResource {
        private int aValue;
        private int aUrl;
        private int aIconIdle;
        private int aIconActive;
    
        public MenuItemResource(int value, int url, int iconIdle, int iconActive) {
            aValue = value;
            aUrl = url;
            aIconIdle = iconIdle;
            aIconActive = iconActive;
        }
    }
    

    The behaviour is handled by an OnItemClickListener which checks with a switch which values are in the MenuItemResource that is being clicked. For the WebView it's quite straightforward:

                newFragment = new WebViewFragment();
                final Bundle arguments = new Bundle();
                arguments.putString(Constants.KEY_URL, getString(item.getUrl()));
                newFragment.setArguments(arguments);
                startFragment(newFragment, false); 
                // boolean is used to add the fragment to the backstack
    

    The startFragment method just uses the FragmentManager and FragmentTransaction to replace the current Fragment. This works the same for other MenuItemResources that do start regular fragments.

                newFragment = new Task1Fragment();
                startFragment(newFragment, false);
    

    I don't refer to the fragments in the MenuItemResource (yet), but it works pretty well for URLs and WebViews. The fragments are started based on the value in the MenuItemResource I'm not sure how you would refer to the fragments like you did in the comments (Task1.java, etc), since you don't start them with Intents like Activities. Also I'm not sure why you would want to do this dynamically for Fragments (I can imagine this case being dynamic for WebViews though) as they need to be compiled anyway, so that's why my menu items are added by hand.

提交回复
热议问题