Android 如何在每次启动单个片段时更改图像?

Android 如何在每次启动单个片段时更改图像?,android,android-fragments,Android,Android Fragments,我有一项活动。在这种情况下,当我启动单个片段时,我应该为每次启动片段获得不同的图像 public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getSupportFragmentManager(); fragmentMa

我有一项活动。在这种情况下,当我启动单个片段时,我应该为每次启动片段获得不同的图像

  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;
        }
    }

我知道您使用的是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语句,甚至根据您正在使用的内容进行切换,以确定……希望我能提供帮助