如何在动作栏中从Spinner启动片段?

时间:2014-08-26 22:03:50

标签: android android-intent android-fragments android-viewpager android-spinner

我有一个应用程序与滑动抽屉,15碎片(滑动)选项卡与视图寻呼机编号1-15,旋转器在行动栏,查看寻呼机显示15碎片 我需要的是当我选择选项4(例如)在Spinner DropDown中我希望屏幕显示片段4以及4的视图寻呼机 我怎样才能实现?

我的MainActivity.java

public class MainActivity extends FragmentActivity implements  ActionBar.OnNavigationListener{

private static final String TAG = MainActivity.class.getSimpleName();

private DrawerLayout mDrawerLayout;
private ActionBar actionBar;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private ArrayList<SpinnerNavItem> navSpinner;
private TitleNavigationAdapter adapter;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawerItmes;

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

    mTitle = mDrawerTitle = getTitle();
    actionBar = getActionBar();
    // Hide the action bar title
    actionBar.setDisplayShowTitleEnabled(false);

    // Enabling Spinner dropdown navigation
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    navSpinner = new ArrayList<SpinnerNavItem>();
    navSpinner.add(new SpinnerNavItem("MOVIE1", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE2", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE3", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE4", R.drawable.ic_incmp));  
    navSpinner.add(new SpinnerNavItem("MOVIE5", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE6", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE7", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE8", R.drawable.ic_incmp));  
    navSpinner.add(new SpinnerNavItem("MOVIE9", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE10", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE11", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE12", R.drawable.ic_incmp));  
    navSpinner.add(new SpinnerNavItem("MOVIE13", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE14", R.drawable.ic_incmp));
    navSpinner.add(new SpinnerNavItem("MOVIE15", R.drawable.ic_incmp));

    adapter = new TitleNavigationAdapter(getApplicationContext(), navSpinner);

    // assigning the spinner navigation     
    actionBar.setListNavigationCallbacks(adapter, this);

    mDrawerItmes = getResources().getStringArray(R.array.drawer_titles);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer oepns
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,  GravityCompat.START);

    // Add items to the ListView
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mDrawerItmes));
    // Set the OnItemClickListener so something happens when a 
    // user clicks on an item.
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // Enable ActionBar app icon to behave as action to toggle nav drawer
    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(); // creates call to onPrepareOptionsMenu
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

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

}

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

    return super.onOptionsItemSelected(item);
}

/*
 * When using the ActionBarDrawerToggle, you must call it during onPostCreate()
 * and onConfigurationChanged()
 */

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

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

private class DrawerItemClickListener implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.v(TAG, "LEVEL SELECTED");
        navigateTo(position);
    }

    }



    private void navigateTo(int position) {
    Log.v(TAG, "List View Item: " + position);

    switch(position) {
    case 0:
        /*getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.content_frame, 
                    ItemOne.newInstance(),
                    ItemOne.TAG).commit();*/
        getSupportFragmentManager()
        .beginTransaction()
             .replace(R.id.content_frame,TabbedActivity.newInstance(),TabbedActivity.TAG).commit();
        break;
    case 1:
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame,
                    TabbedActivity2.newInstance(),
                    TabbedActivity2.TAG).commit();
        break;
    }
}

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

@Override
public boolean onNavigationItemSelected(int position, long arg1) {
     SpinnerNavItem selectedItem = navSpinner.get(position );
     Fragment fragment = null;
     FragmentTransaction fragmentTransaction = getSupportFragmentManager()
             .beginTransaction();
     switch (position) {
     case 0:
         fragment = new Movie1();
         break;

     case 1:
         fragment =new Movie2();
         break;

     case 2:
         fragment = new Movie3();
         break;

     default:
         break;
     }
     fragmentTransaction.replace(R.id.content_frame ,fragment);
     fragmentTransaction.commit();
     return true;




     }
     }

和我的

activity_main.xml中

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


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


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>


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

TabbedActivity.java

public class TabbedActivity extends Fragment {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

public static final String TAG = TabbedActivity.class.getSimpleName();

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;


public static TabbedActivity newInstance() {
    return new TabbedActivity();
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_item_one, container, false);
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getChildFragmentManager());

    mViewPager = (ViewPager) v.findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    return v;
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
        return new Movie1();
        case 1:
        return new Movie2(2);
        case 2:
        return new Movie3();
        case 3:
            return new Movie4();
        case 4:
            return new Movie5();
        case 5:
            return new Movie6();
        case 6:
            return new Movie7();
        case 7:
            return new Movie8();
        case 8:
            return new Movie9();
        case 9:
            return new Movie10();
        case 10:
            return new Movie11();
        case 11:
            return new Movie12();
        case 12:
            return new Movie13();
        case 13:
            return new Movie14();
        case 14:
            return new Movie15();
        case 15:
            return new Movie16();

        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 15;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        Log.v(TAG, "SECTION: " + position);
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        case 3:
            return getString(R.string.title_section4).toUpperCase(l);
        case 4:
            return getString(R.string.title_section5).toUpperCase(l);
        case 5:
            return getString(R.string.title_section6).toUpperCase(l);
        case 6:
            return getString(R.string.title_section7).toUpperCase(l);
        case 7:
            return getString(R.string.title_section8).toUpperCase(l);
        case 8:
            return getString(R.string.title_section9).toUpperCase(l);
        case 9:
            return getString(R.string.title_section10).toUpperCase(l);
        case 10:
            return getString(R.string.title_section11).toUpperCase(l);
        case 11:
            return getString(R.string.title_section12).toUpperCase(l);
        case 12:
            return getString(R.string.title_section13).toUpperCase(l);
        case 13:
            return getString(R.string.title_section14).toUpperCase(l);
        case 14:
            return getString(R.string.title_section15).toUpperCase(l);
        case 15:
            return getString(R.string.title_section15).toUpperCase(l);


        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

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

        return rootView;
    }
}

}

1 个答案:

答案 0 :(得分:0)

当用户选择微调项目时,您必须执行以下步骤:

首先通过调用

找到TabbedActivity
   TabbedActivity  f  =  (TabbedActivity) getSupportFragmentManager().findFragmentById(TabbedActivity.Tag);

然后您必须在该片段中获得viewpager,您可以使用getter - setter或声明public,我假设getter方法:

   f.getViewPager().setCurrentItem(the position you want to go, false);

所以你的代码必须是:

小心你有两个TabbedActivity,我不知道哪一个在显示,所以你必须在下面的代码中选择正确的一个:

@Override
public boolean onNavigationItemSelected(int position, long arg1) {

       TabbedActivity  f  =  (TabbedActivity) getSupportFragmentManager().findFragmentById(TabbedActivity.Tag);
      f.getViewPager().setCurrentItem(position, false);
      return true;

 }