将动态部分添加到导航抽屉

时间:2014-11-26 23:07:27

标签: android navigation-drawer

我一直在寻找文章和主题,但每个代码都与Android在创建带有导航抽屉的新活动时提供的原始包装不同。这是我目前的代码:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.example.gregoire.rfa.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

根据我的理解,片段是包含所有部分的ListView。默认情况下,会创建3个部分:

Android NavigationDrawer default sections

我正在尝试创建RSS Feed聚合器。我希望能够动态添加其他部分,每个部分代表一个RSS提要。我得到了标题,但无法弄清楚如何将它们添加为部分。我需要使用适配器吗?

我也不明白这三个默认部分的来源。他们在哪里创建?这是Android生成的代码以及我的贡献:

public class HomeActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks
{
    private WebService  m_webService;
    private SharedPreferences m_prefs;
    private String  m_email, m_password;
    private ArrayList<Pair<String, Integer>> m_map;

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.m_map = new ArrayList<Pair<String, Integer>>();
        this.m_webService = new WebService("http://tomcat8-wokesmeed.rhcloud.com");
        this.m_prefs = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = this.m_prefs.edit();
        editor.putBoolean("isLogged", true);

        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            m_email = extras.getString("email");
            editor.putString("email", m_email);
            m_password = extras.getString("password");
            editor.putString("password", m_password);
        }
        else
        {
            m_email = this.m_prefs.getString("email", "");
            m_password = this.m_prefs.getString("password", "");
        }
        editor.commit();

        setContentView(R.layout.activity_home);
        mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));


        new Thread()
        {
            public void run()
            {
                try
                {
                    if (m_webService.connectUser(m_email, m_password))
                    {
                        String feeds = m_webService.getFeeds();
                        JSONObject  jsonObject = new JSONObject(feeds);
                        JSONArray jArray = jsonObject.getJSONArray("feeds");
                        for (int i = 0; i < jArray.length(); i++)
                        {
                            JSONObject row = jArray.getJSONObject(i);
                            m_map.add(new Pair<String, Integer>(row.getString("title"), row.getInt("id")));
                        }
                    }
                } catch (Exception e)   {   e.printStackTrace();    }
            }
        }.start();
    }

    @Override
    public void onNavigationDrawerItemSelected(int position)
    {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1)).commit();
    }

    public void onSectionAttached(int number)
    {
        switch (number)
        {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
        }
    }

    public void restoreActionBar()
    {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        if (!mNavigationDrawerFragment.isDrawerOpen())
        {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.home, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_logout)
        {
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        m_webService.disconnectUser();
                    } catch (Exception e)   {   e.printStackTrace();    }
                }
            }.start();

            SharedPreferences.Editor editor = this.m_prefs.edit();
            editor.putBoolean("isLogged", false);
            editor.commit();
            Intent i = new Intent(this, LoginActivity.class);
            startActivity(i);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment
    {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber)
        {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity)
        {
            super.onAttach(activity);
            ((HomeActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }
}

我不知道public void onNavigationDrawerItemSelected(int position)如何知道应用开头有多少部分。

1 个答案:

答案 0 :(得分:0)

当然,你可以把任何你想要的东西放在DrawerLayout中。您可以添加ListView并动态填充它。