【问题标题】:Dynamic UI with sliding menu and actionbarsherlock带有滑动菜单和 actionbarsherlock 的动态 UI
【发布时间】:2013-01-11 08:59:08
【问题描述】:

尝试使用facebook like sliding menu 和actionbarsherlock 实现动态用户界面 .首先我研究了介绍片段来处理动态按钮的android文档。但是没有运气和一周的时间,我仍然无法让它工作,我想是我对 android 概念的误解。滑动条和 actionbarsherlock 工作没有任何问题。

我有一个 HomeScreen.java,其中包含我所有的菜单和预设阶段 到目前为止,我已经创建了一个扩展 FragmentPagerAdapter 的 pagerAdapter1.java ,以及处理我的工作的三个示例片段类,即 task1.java,task2.java ,task3.java 够简单

这是我的代码的一部分 HomeScreen.java

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;
public class HomeScreen extends SlidingFragmentActivity {
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_screen);
            setBehindContentView(R.layout.menu_frame);
    }

PagerAdapter1.java

public class PagerAdapter1 extends FragmentPagerAdapter  {

    private List<Fragment> fragments;
    public PagerAdapter1(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    public int getCount() {
        return this.fragments.size();
    }

}

还有三个task1.java,2,3

    import android.support.v4.app.Fragment;
    public class Tab1Fragment extends Fragment{

onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
            return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
        }

我认为最好用图片来解释我的问题

主屏幕是一个预设的阶段,每当用户点击菜单时,这个页面就会变成他想要的页面

这是我的菜单

我的问题是如何将这 3 个片段包含到我的主屏幕中?我尝试了很多教程,但在我的情况下不起作用。大多数教程都是用代码创建片段,我只想将我的 3 个任务包含在其中

【问题讨论】:

    标签: android android-fragments actionbarsherlock dynamic-ui


    【解决方案1】:

    我将尝试解释此示例代码,您可以根据需要使用。

    这是您的BehindContent (SlidingMenu) 的ListFragment:

    public class ColorMenuFragment extends ListFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.list, null);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            String[] colors = getResources().getStringArray(R.array.color_names);
            ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(), 
                    android.R.layout.simple_list_item_1, android.R.id.text1, colors);
            setListAdapter(colorAdapter);
    //This array is only to fill SlidingMenu with a Simple String Color.
    //I used MergeAdapter from Commonsware to create a very nice SlidingMenu.
        }
    
        @Override
        public void onListItemClick(ListView lv, View v, int position, long id) {
    //This switch case is a listener to select wish item user have been selected,  so it Call
    //ColorFragment, you can change to Task1Fragment, Task2Fragment, Task3Fragment.
            Fragment newContent = null;
            switch (position) {
            case 0:
                newContent = new ColorFragment(R.color.red);
                break;
            case 1:
                newContent = new ColorFragment(R.color.green);
                break;
            case 2:
                newContent = new ColorFragment(R.color.blue);
                break;
            case 3:
                newContent = new ColorFragment(android.R.color.white);
                break;
            case 4:
                newContent = new ColorFragment(android.R.color.black);
                break;
            }
            if (newContent != null)
                switchFragment(newContent);
        }
    
        // the meat of switching the above fragment
        private void switchFragment(Fragment fragment) {
            if (getActivity() == null)
                return;
    
            if (getActivity() instanceof FragmentChangeActivity) {
                FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
                fca.switchContent(fragment);
            } else if (getActivity() instanceof ResponsiveUIActivity) {
                ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
                ra.switchContent(fragment);
            }
        }
    
    
    }
    

    这是您的 BaseActivity 类:

    它没有滑动,据我所知,你不需要这个。

    public class FragmentChangeActivity extends BaseActivity {
    
        private Fragment mContent;
    
        public FragmentChangeActivity() {
            super(R.string.changing_fragments);
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // set the Above View
            if (savedInstanceState != null)
                mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
            if (mContent == null)
                mContent = new ColorFragment(R.color.red);  
    
            // set the Above View
                //This will be the first AboveView
            setContentView(R.layout.content_frame);
            getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, mContent)
            .commit();
    
            // set the Behind View
                //This is the SlidingMenu
            setBehindContentView(R.layout.menu_frame);
            getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.menu_frame, new ColorMenuFragment())
            .commit();
    
            // customize the SlidingMenu
                //This is opcional
            getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }
    
        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            getSupportFragmentManager().putFragment(outState, "mContent", mContent);
        }
    
        public void switchContent(Fragment fragment) {
                // the meat of switching fragment
            mContent = fragment;
            getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();
            getSlidingMenu().showContent();
        }
    
    }
    

    好的,所以如果您想将 ColorFragment 更改为其他任何内容,请执行以下操作:

    首先,选择您要使用的项目:

    case 0:
                    newContent = new ColorFragment(R.color.red);
                    break;
    

    收件人:

    case 0:
                newContent = new ArrayListFragment();
                break;
    

    我只是做了一个arraylist,它只是一个简单的例子,你可以做很多事情,然后你可以阅读Fragment来学习如何做不同的事情。

        public class ArrayListFragment extends ListFragment {
    
        @Override                               
                public void onActivityCreated(Bundle savedInstanceState) {
                    super.onActivityCreated(savedInstanceState);
                    setListAdapter(new ArrayAdapter<String>(getActivity(),
                            android.R.layout.simple_list_item_1, Listnames.TITLES));
    //Listnames is a class with String[] TITLES;
    
    }
    
            @Override
            public void onListItemClick(ListView l, View v, int position, long id) {
                Log.i("FragmentList2", "Item clicked: " + id);
    
                String item = (String) getListAdapter().getItem(position);
            Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
    
            }
    
        }
    

    好吧,如果你误解了什么,请告诉我。

    【讨论】:

    • 哦,我认为我需要的是 android 库示例中的良好解释...感谢您阅读示例并向我解释,只是关于 onListItemClick 的背后内容...。我如何替换颜色我写了一组类似的java类......比如task1.java,task2.java,taskjava.java......它们都有不同的布局......非常感谢=)
    • 我用 arraylist 类做了一个简单的例子,你可以改变和使用 task1.java 代替,只是不要忘记你正在使用片段。您的 task1.java 不能扩展 Activity。
    • @LeonArmstrong,请考虑接受这个问题作为正确的问题。
    • 对不起,我今天只能测试这段代码,结果会告诉你...谢谢
    • 顺便说一句,不能扩展活动是什么意思?
    【解决方案2】:

    我的问题是如何将这 3 个片段包含到我的主屏幕中?

    这真的取决于你希望他们如何表现。

    您是否希望它们一次只显示一个而不允许在它们之间滑动?如果是,则在您的Activity 中添加/插入容器布局(例如简单的FrameLayout),您将在其中添加Fragments。我没有使用SlidingMenu 库,但是当您单击菜单中的一项时,它应该会调用一个回调。在该回调中,您会将正确的片段附加到我之前提到的容器布局(FrameLayout)。

    你想只显示一个片段,但你想让用户在它们之间滑动吗?如果是,则在活动布局中使用ViewPager,并在SlidingMenu 库的菜单选择触发的回调中使用setCurrentItem() 方法设置ViewPager 的当前页面。

    如果您想要不同的东西,那么这里提供更多详细信息。

    大多数教程都是用代码创建片段,我只想包括 我的 3 任务进入它

    这个,我不太明白。如果你想直接在你的 xml 布局中“包含”你的任务片段,你可以,但你会被限制在你可以用它们做什么(更不用说所有片段都将在一个屏幕上),我会避免它。如果您想要其他内容,请提供更多详细信息。

    【讨论】:

    • 感谢您的阅读,我的问题很接近了,对于回调部分我没有任何问题,我在菜单导航中有一个按钮列表,还有一个 content_frame 作为我的阶段,现在我想动态响应用户点击我的菜单并将我的 content_frame 更改为所需的内容(没有滑动,我只想在用户点击时在它们之间切换),当我很少时,正确的方法是什么执行不同工作和内容的 java 类,如 task1.java 、 task2.java 、 task3.java 代表不同的内容
    • @LeonArmstrong 我不确定我是否理解。这些按钮在滑动菜单中还是在其他地方? 将我的 content_frame 更改为想要的内容 - 如果我了解您想要什么,一个简单的 FragmentTransaction 应该可以解决这个问题。
    【解决方案3】:

    我认为它不会像 Fragments 那样工作,我也在寻找解决方案,最终手动添加了 Fragments。

    我正在做类似的事情,但对我来说,还有将WebViews 打开到指定 URL 的情况。所以“上方”屏幕总是会在任何点击时更新。

    为了控制它的行为,我创建了一个MenuItemResource 对象,它基本上包含属性,如图标的 ID、菜单项的名称和 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;
        }
    }
    

    该行为由OnItemClickListener 处理,该OnItemClickListener 使用开关检查正在单击的MenuItemResource 中的值。对于WebView,这很简单:

                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
    

    startFragment 方法只是使用FragmentManager 和FragmentTransaction 替换当前的Fragment。这同样适用于其他启动常规片段的MenuItemResources。

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

    我还没有提到MenuItemResource 中的片段(还),但它对于 URL 和 WebView 非常有效。片段基于MenuItemResource中的value启动 我不确定您将如何像在 cmets(Task1.java 等)中那样引用片段,因为您不像 Activities 那样以 Intents 开头。另外我不确定为什么要为 Fragments 动态执行此操作(我可以想象这种情况对于 WebViews 是动态的),因为它们无论如何都需要编译,所以这就是我的菜单项是手动添加的原因。

    【讨论】:

    • 如果连接中断怎么办?
    • 这是我们在 WebViewFragment 本身中处理的。
    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2012-12-27
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多