2015-01-15 27 views
0

所以,我有導航抽屜的應用程序。 我想把額外的片段「ScreenSourceCollection」並得到額外的片段「ScreenPlayList」。但如果我打電話給 i.putExtra(MainActivity.EXTRA_BOOK_ID,bookId); startActivity(i); 我得到了第一個索引爲0的第一個屏幕,而第二個屏幕的索引爲1.我意識到我犯了錯誤,但我沒有意識到我必須做的事情。從導航抽屜的應用程序中更改另一個片段

我的代碼如下

ScreenSourceCollection 

public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.tab_sources_albums, parent,false); 
    ListView lvSourceList = (ListView)rootView.findViewById(R.id.lvSourceList); 

    database = new MediaDatabase(getActivity()); 
    mDB = new MediaDB(getActivity()); 
    mBooks = mDB.getBooks(); 

    BookAdapter bookAdapter = new BookAdapter(mBooks); 
    lvSourceList.setAdapter(bookAdapter); 

    lvSourceList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view1, 
           int position, long id) { 

      Book book = (Book)parent.getAdapter().getItem(position); 
      long bookId = position; 
      Intent i = new Intent(getActivity(),MainActivity.class); 
      i.putExtra(MainActivity.EXTRA_BOOK_ID, bookId); 
      startActivity(i); 

      Toast.makeText(getActivity(), String.valueOf(book.getId()), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    return rootView; 
} 

ScreenPlayList

public static ScreenPlayList newInstance (long bookId) { 
    Bundle args = new Bundle(); 
    args.putLong(ARG_BOOK_ID, bookId); 
    ScreenPlayList pl = new ScreenPlayList(); 
    pl.setArguments(args); 
    return pl; 

} 

MainActivity

public class MainActivity extends ActionBarActivity { 

private String[] mScreenTitles; 
private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
public static final String EXTRA_BOOK_ID = "ru.thevoice.sounds.book_id"; 

private ActionBarDrawerToggle mDrawerToggle; 
private CharSequence mDrawerTitle; 
private CharSequence mTitle; 

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

    mTitle = mDrawerTitle = getTitle(); 
    mScreenTitles = getResources().getStringArray(R.array.screen_array); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 

    // Set the adapter for the list view 
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.drawer_list_item, mScreenTitles)); 
    // Set the list's click listener 
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 

    mDrawerToggle = new ActionBarDrawerToggle(
      this, /* host Activity */ 
      mDrawerLayout, /* DrawerLayout object */ 
      R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
      R.string.drawer_open, /* "open drawer" description */ 
      R.string.drawer_close /* "close drawer" description */ 
    ) { 

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      getSupportActionBar().setTitle(mTitle); 
      supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      getSupportActionBar().setTitle(mDrawerTitle); 
      supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
     } 
    }; 

    // Set the drawer toggle as the DrawerListener 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    // Initialize the first fragment when the application first loads. 
    if (savedInstanceState == null) { 
     selectItem(0); 
    } 

} 

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

/* Called whenever we call invalidateOptionsMenu() */ 
@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    // If the nav drawer is open, hide action items related to the content view 
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
    menu.findItem(R.id.action_search).setVisible(!drawerOpen); 
    return super.onPrepareOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Pass the event to ActionBarDrawerToggle, if it returns 
    // true, then it has handled the app icon touch event 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    // Handle action buttons 
    switch(item.getItemId()) { 
     case R.id.action_search: 
      // Show toast about click. 
      Toast.makeText(this, R.string.action_search, Toast.LENGTH_SHORT).show(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

/* The click listener for ListView in the navigation drawer */ 
private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     selectItem(position); 
    } 
} 

/** Swaps fragments in the main content view */ 
private void selectItem(int position) { 
    // Update the main content by replacing fragments 
    Fragment fragment = null; 
    switch (position) { 
     case 0: 
      fragment = new ScreenPlay(); 
      break; 
     case 1: 
      //fragment = new ScreenPlayList(); 
      long bookId = getIntent().getLongExtra(EXTRA_BOOK_ID, -1); 
      if (bookId != -1) { 
       fragment = ScreenPlayList.newInstance(bookId); 
      } else { 
       fragment = new ScreenPlayList(); 
      } 
      break; 
     case 2: 
      fragment = new ScreenSourcesCollection(); 
      break; 
     case 3: 
      fragment = new ScreenSettings(); 
      break; 
     case 4: 
      fragment = new ScreenPlayTabs(); 
      break; 
     default: 
      break; 
    } 

    // Insert the fragment by replacing any existing fragment 
    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_frame, fragment).commit(); 

     // Highlight the selected item, update the title, and close the drawer 
     mDrawerList.setItemChecked(position, true); 
     setTitle(mScreenTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } else { 
     // Error 
     Log.e(this.getClass().getName(), "Error. Fragment is not created"); 
    } 
} 

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

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

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Pass any configuration change to the drawer toggles 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

} 

回答

0

我怎麼做,通過保持一個參考片段我想一個包傳遞到:

Bundle args = new Bundle(); 
args.putInt(YourFragment.QUESTIONNUMBER, position + 1); 
args.putSerializable(YourFragment.QUESTIONCONTENT, question); 
currentFragment.setArguments(args); 

每一個價值製作繩標籤可以在片段裏傳似

public static final String QUESTIONCONTENT = "question" 

這樣,你總是知道到底是什麼,以便防止轉換錯誤傳遞。打電話給您的片段,設置參數和捆綁在你的片段,您可以撥打:

Bundle args = getArguments(); 
int position = (Integer)args.get(QUESTIONNUMBER); 
question = (Question)args.get(QUESTION); 

此代碼是我onCreateView,用它在其他地方還沒有真正嘗試過。祝你好運!

+0

非常感謝,但我告訴我,請: 1.如何獲得currentFragment? 2.我需要在第一個片段中使用什麼以及在第二個片段中使用什麼? – RyndinV

+0

那麼,你的片段總是包含在一個活動中,所以我假設你有代碼實例化它們,這意味着你可以保持引用的地方。從那裏,你將能夠創建一個接口,該接口的方法允許你調用傳遞包的特定方法,或者可能需要構造函數中的參數包?不知何故,你必須從包含它們的活動中獲取片段。 –