如何将片段加载到SlidingPaneLayout菜单中?

时间:2018-06-05 23:35:12

标签: android slidingpanelayout

我是新手,所以请耐心等待我。我在我的应用程序中实现了SlidingPaneLayout。但是,在我接下来的示例中,它使用字符串数组填充菜单片段。我认为这只是一个例子。如何使用指向我希望用户访问的片段的链接填充菜单片段?

我有一个menu列表,其中包含我想要显示的片段的名称,但似乎没有实现。

以下是菜单片段中的代码:

public class MainListFragment extends ListFragment {

    private ArrayAdapter<String> mAdapter;

    public static final String[] items = { "Item 1: xxxxxxxxxxxxxxxxx",
            "Item 2: xxxxxxxxxxxxxxxxx", "Item 3: xxxxxxxxxxxxxxxxx",
            "Item 4: xxxxxxxxxxxxxxxxx", "Item 5: xxxxxxxxxxxxxxxxx",
            "Item 6: xxxxxxxxxxxxxxxxx", "Item 7: xxxxxxxxxxxxxxxxx" };

    ListFragmentItemClickListener iItemClickListener;

    /** An interface for defining the callback method */
    public interface ListFragmentItemClickListener {
        /**
         * This method will be invoked when an item in the ListFragment is
         * clicked
         */
        void onListFragmentItemClick(View view, int position);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);
        setListAdapter(mAdapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        setHasOptionsMenu(true);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /** A callback function, executed when this fragment is attached to an activity */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            /** This statement ensures that the hosting activity implements ListFragmentItemClickListener */
            iItemClickListener = (ListFragmentItemClickListener) activity;
        } catch(Exception e) {
            Toast.makeText(activity.getBaseContext(),"Exception", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main_list, menu);
    }

    @Override
    public void onListItemClick(ListView list, View view, int position, long id) {

        /**
         * Invokes the implementation of the method onListFragmentItemClick in
         * the hosting activity
         */
        iItemClickListener.onListFragmentItemClick(view, position);

    }

}

以下是menu列表中的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_ehr"
            android:title="@string/ehr_short" />
        <item
            android:id="@+id/nav_protocols"
            android:title="@string/protocols" />
    </group>
</menu>

1 个答案:

答案 0 :(得分:0)

您应该在XML文件中创建菜单,然后使用菜单事件处理程序处理事件。 This example可以帮助你。

主要思想是,当您使用Android菜单界面组件时,您可以访问onCreateOptionsMenuonOptionsItemSelected等事件处理程序,从而可以控制UI流程。