I have a ViewPager
with a TextView
and an ImageView
. For the TextView
I have a String[]
which contains dynamically added Strings
and currently just a int[]
for the ImageView
.
So my question is, if I can make a if
or switch case
to set the a definite image to a text.
Like if the TextView
is "Text1" the image should be R.drawable.ic_launcher, and if the TextView
is "Text2" the image should be R.drawable.ic_launcher2 and so on.
Here is my code:
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);
}
}
Aucun commentaire:
Enregistrer un commentaire