2015-10-16 60 views
0

我有一項活動。在我啓動一個片段時,每次啓動片段時都應該獲得不同的圖像。如何在每次啓動單個碎片時更改映像?

public void onNavigationDrawerItemSelected(int position) { 
     // update the main content by replacing fragments 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     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: 
       //`enter code here` mTitle = getString(R.string.title_section3); 
       break; 
     } 
    } 

回答

0

我看你使用Android Studio,但你必須要更具體的你要如何確定的形象,如果你要確定它基於發送到分段試整數這個

public static class CustomFragment extends Fragment{ 
..... 
@Override 
public void onCreateView() 
{ 
    position = getArguments().getInt("ARG_SECTION_NUMBER") 
    View view = getView(getActivity()); 
    return view; 
} 

public View getView(context) 
{ 
    View view = context.getLayoutInflater().inflate(R.id.view_layout,null); 
    ImageView image= (ImageView)view.findViewById(R.id.image_view); 
    switch(position) 
    { 
     case(1): 
     image.setImageResource(your_image_res_for_1); 
      break; 
     ..... 
     default: 
      break 
    } 
} 

}

或者如果你想確定基於別的東西使用if語句甚至開關取決於它是你用來確定.....希望我幫助

相關問題