1

我对 android 应用程序开发完全陌生,我目前正在为学校做一个项目。

根据我目前的理解,您可以使用导航抽屉来交换 UI 片段。根据此处给出的教程,我制作了一个到目前为止工作正常的导航抽屉:

https://developer.android.com/training/implementing-navigation/nav-drawer.html

我试图让它为每个选定的项目切换视图。我做了一个视图寻呼机片段活动,我希望程序在选择导航抽屉中的第 4 个项目(索引 3)时用我的视图寻呼机替换视图。我在片段类中添加了一个 switch 语句来确定要加载的布局。

这是导航抽屉的活动:

package com.mkge.mkg;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mTitle;
    private String[] mPageTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = getTitle();
        mPageTitles = getResources().getStringArray(R.array.nav_draw_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPageTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.drawer_open);
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) return true;
        return super.onOptionsItemSelected(item);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        Fragment fragment = new PageFragment();
        Bundle args = new Bundle();
        args.putInt(PageFragment.ARG_PAGE_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        setTitle(mPageTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    public static class PageFragment extends Fragment {
        public static final String ARG_PAGE_NUMBER = "page_number";

        public PageFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            int i = getArguments().getInt(ARG_PAGE_NUMBER);
            View rootView;
            switch(i) {
            case 0:
                rootView = inflater.inflate(R.layout.page_home, container, false);
                break;
            case 1:
                rootView = inflater.inflate(R.layout.page_services, container, false);
                break;
            case 2:
                rootView = inflater.inflate(R.layout.page_packages, container, false);
                break;
            case 3:
            View view = new MeetViewPager().getViewPager();
            rootView = view;
            break;
            case 4:
                rootView = inflater.inflate(R.layout.page_about, container, false);
                break;
            case 5:
                rootView = inflater.inflate(R.layout.page_contact, container, false);
                break;
            case 6:
                rootView = inflater.inflate(R.layout.page_extras, container, false);
                break;
            default:
                rootView = null;
            }

            String page = getResources().getStringArray(R.array.nav_draw_array)[i];
            getActivity().setTitle(page);
            return rootView;
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

我在这里创建了这个片段活动来创建视图寻呼机。我尝试获取我在这里创建的视图寻呼机对象并将其传递给我的导航抽屉活动的 switch 语句。

package com.mkge.mkg;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("ValidFragment")
    public class MeetViewPager extends FragmentActivity {
        public ViewPager mViewPager;
        public FragmentPagerAdapter mViewPagerAdapter;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.page_meet);
            List<Fragment> fragments = getFragments();
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPagerAdapter = new mPageAdapter(getSupportFragmentManager(), fragments);
            mViewPager.setAdapter(mViewPagerAdapter);
        }

        public View getViewPager() {
            return mViewPager;
        }

        private List<Fragment> getFragments() {
            List<Fragment> fList = new ArrayList<Fragment>();
            Fragment fragment = new mFragment();
            Bundle args = new Bundle();
            args.putInt("position", 0);
            fragment.setArguments(args);
            fList.add(fragment);

            fragment = new mFragment();
            args = new Bundle();
            args.putInt("position", 1);
            fragment.setArguments(args);
            fList.add(fragment);

            return fList;
        }

        private class mPageAdapter extends FragmentPagerAdapter {
            private List<Fragment> fragments;

            public mPageAdapter(FragmentManager f, List<Fragment> fragments) {
                super(f);
                this.fragments = fragments;
            }

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

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

        @SuppressLint("ValidFragment")
        public static class mFragment extends Fragment {

            public mFragment() {
            }

            public static final mFragment newInstance(int i) {
                mFragment f = new mFragment();
                Bundle args = new Bundle(1);
                args.putInt("position", i);
                f.setArguments(args);
                return f;
            }

            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v;
                int i = getArguments().getInt("postion");
                switch(i) {
                case 0:
                    v = inflater.inflate(R.layout.page_meet_0, container, false);
                    break;
                case 1:
                    v = inflater.inflate(R.layout.page_meet_1, container, false);
                    break;
                default:
                    v = null;
                }
                return v;
            }
        }
    }

任何帮助将不胜感激。

4

1 回答 1

0
mViewPager.setCurrentItem(0); 

这应该是您将 ViewPager 更改为第一个选项卡所需的全部内容。

这将从您的selectItem()方法内部调用。

于 2016-06-16T06:12:26.213 回答