具有滑动菜单和ActionBarSherlock的动态UI

8
尝试使用 Facebook 式侧滑菜单ActionBarSherlock 实现动态 UI。首先,我查阅了 Android 文档,介绍了 Fragment 处理动态按钮。但是,经过一周的尝试,我仍然无法让它正常工作,我猜测是我对 Android 概念的误解。侧滑菜单和 ActionBarSherlock 工作得很好。
我有一个 HomeScreen.java 文件,其中包含所有的菜单和展示页面。到目前为止,我已经创建了一个扩展 FragmentPagerAdapter 的 pagerAdapter1.java,并且有三个示例 Fragment 类来处理我的工作,即 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();
    }

}

还有三个任务1.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);
        }

我认为最好用图片来解释我的问题。
一个主屏幕是演示舞台,每当用户点击菜单时,该页面将改变到他想要的页面。
这是我的主页: homescreen 这是我的菜单: menu_frame 我的问题是如何将这3个片段包含在我的主屏幕中?我尝试了很多教程,但在我的情况下它并没有起作用。大多数教程都是使用代码创建片段,而我只想将我的3个任务包含进去。
3个回答

14
我会尝试解释这个示例代码并让您能够根据您的需要使用它。 这是您 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;

to:

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...它们都有不同的布局...非常感谢=) - Leon Armstrong
我已经使用ArrayList类完成了一个简单的示例,你可以更改并使用task1.java代替,但是不要忘记你正在处理片段。你的task1.java不能扩展Activity。 - Marckaraujo
抱歉,我今天才能测试这段代码,测试结果会及时告知您...谢谢。 - Leon Armstrong
这里的 "ColorFragment()" 是什么,你能解释一下吗?@Marckaraujo - Pratik Butani
@PratikButani 这只是一个示例类,您可以将其替换为所需的类。 - Leon Armstrong
显示剩余3条评论

2
我的问题是如何将这三个片段包含在我的主屏幕中?
这取决于您希望它们如何运作。如果您希望它们一次只出现一个,而且不允许在它们之间滑动怎么办? 如果是这样,在Activity中添加/插入一个容器布局(例如简单的FrameLayout),然后在其上添加Fragments。我没有使用过SlidingMenu库,但它应该有一个回调函数,用于当您单击菜单中的某个项目时触发。在该回调中,您将把适当的片段附加到我之前提到的容器布局(FrameLayout)中。
如果您只想显示一个Fragment,但想允许用户在其中滑动怎么办? 如果是这样,在活动布局中使用一个ViewPager,并在由SlidingMenu库的菜单选择触发的回调中,使用setCurrentItem()方法设置ViewPager的当前页面。
如果您想要其他功能,请提供更多详细信息。
大多数教程都是使用代码创建片段,我只想将我的3个任务包含在其中
这一点我不太理解。如果您想直接在xml布局中“包含”您的任务片段,您可以这样做,但是它们的使用受到限制(而且所有片段都会在一个屏幕上),我会避免这样做。如果您想要其他内容,请提供更多详细信息。

感谢您的阅读,您对我的问题理解得很接近。对于回调部分,我没有任何问题。我这里有一个菜单导航中的按钮列表和一个充当我的舞台的content_frame。现在,我想动态响应用户单击我的菜单并将我的content_frame更改为所需内容(不是滑动,我只想在用户单击时在它们之间切换),当我有几个执行不同工作和内容的Java类,例如task1.java,task2.java,task3.java,应该如何正确地实现这一点? - Leon Armstrong
@LeonArmstrong 我不确定我理解了。那些按钮在滑动菜单里还是其他地方?将我的content_frame更改为所需的内容 - 如果我理解你的意思,一个简单的FragmentTransaction应该可以解决这个问题。 - user

1

我认为使用Fragment不会像那样起作用,我也在寻找解决方案,最终是手动添加了Fragments。

我正在处理类似的问题,但对我来说还有一个打开指定URL的WebViews的情况。因此,“上面”的屏幕将始终在任何点击时更新。

为了控制这个行为,我创建了一个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处理,它使用switch检查被点击的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和WebViews它运行得非常好。这些片段是基于MenuItemResource中的value启动的。 我不确定您如何像在注释中那样引用片段(Task1.java等),因为您不像Activities那样使用Intents来启动它们。此外,我不确定为什么您希望对片段进行动态处理(虽然我可以想象这种情况对于WebViews是动态的),因为它们需要被编译,所以我的菜单项是手动添加的。


如果连接出现问题怎么办? - Leon Armstrong
这是我们在 WebViewFragment 中处理的事情。 - StingRay5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接