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

时间:2015-10-16 10:31:38

标签: android android-fragments

我有一项活动。当我发布一个片段时,我应该为每个片段的发布获得不同的图像。

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

1 个答案:

答案 0 :(得分: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语句甚至切换,取决于你用它来确定.....希望我帮助