是否可以使用 if 或 switch 在 Viewpager 中设置图像?

Is it possible to set Images in a Viewpager with a if or switch?

我有一个 ViewPager,一个 TextView 和一个 ImageView。对于 TextView,我有一个 String[],其中包含动态添加的 Strings,目前只有 int[] 用于 ImageView

所以我的问题是,如果我可以制作 ifswitch case 来将明确的图像设置为文本。

如果 TextView 是 "Text1" 图像应该是 R.drawable.ic_launcher,如果 TextView 是 "Text2" 图像应该是 R.drawable.ic_launcher2等等。


这是我的代码:

public class ViewPage extends Fragment {

    ViewPager viewPager;
    ViewPagerAdapter adapter;
    Uebung.SessionItemAdapter adapter2;
    String[] Titel;
    int[] Image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from viewpager_main.xml
    }

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

        ArrayList<String> strtext=getArguments().getStringArrayList("key");
        String frnames[]=strtext.toArray(new String[strtext.size()]);

        Titel = frnames;

        Image = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher};

        viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
        adapter = new ViewPagerAdapter(getActivity(), Titel, Image);
        viewPager.setAdapter(adapter);


        return layout;
    }

ViewPagerAdapter

public class ViewPagerAdapter extends PagerAdapter {
    Context context;
    String[] Titel;
    int[] images;
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, String[] Titel, int[] images) {
        this.context = context;

        this. Titel = Titel;
        this.images = images;
    }


    @Override
    public int getCount() {
        return Titel.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView txtTitel;

        ImageView image;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.swipeview, container,
                false);
        txtTitel = (TextView) itemView.findViewById(R.id.swipeviewtitle);

        txtTitel.setText(Titel[position]);

        image = (ImageView) itemView.findViewById(R.id.swipeViewimage);
        image.setImageResource(images[position]);

        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

如果您想 select 基于字符串实际内容的图像资源,您需要使用一系列 if-else 块来进行字符串比较(与 .equals(),而不是 ==),或者您应该创建一个 Map。如果您打算拥有 很多 个,那么我会选择 Map,因为它比 if-else 的长链表现更好,但是如果您只有两个或三个那么你可能可以摆脱 if-else.

if-else:

String title = Title[position];
int imageRes = 0;
if ("text1".equals(title)) {
    imageRes = R.drawable.ic_launcher;
} else if ("text2".equals(title)) {
    imageRes = R.drawable.ic_launcher2;
} // etc.
image.setImageResource(imagesRes);

Map:

// outside of any method
private static final Map<String, Integer> TITLE_IMAGES = 
    new HashMap<String, Integer>();
static {
    TITLE_IMAGES.put("text1", R.drawable.ic_launcher);
    TITLE_IMAGES.put("text2", R.drawable.ic_launcher2);
}

// in instantiateItem()
String title = Title[position];
Integer imageRes = TITLE_IMAGES.get(title);
if (imageRes == null) { // not found in map
    image.setImageResource(0);
} else {
    image.setImageResource(imagesRes);
}