画廊的图像变化的TextView更新 [英] Gallery on image change textview update
本文介绍了画廊的图像变化的TextView更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我有它2个图像画廊。我想每一次我改变形象一个TextView在我的布局更改其名称。在我的code它发生仅在第一次和仍然是这样。
Hello I have a gallery with 2 images in it. I want each time i change the image a textview in my layout to change its name. In my code it happens only the first time and remains that way.
public class BrowseActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse_activity_layout);
Gallery gallery = (Gallery) findViewById(R.id.browseActivityGallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSpacing(0);
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.building_a,
R.drawable.building_b,
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.browseActivityGalleryStylable);
attr.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView browseActivityTitleTextView = (TextView)findViewById(R.id.browseActivityTitleTextView);
if(position == 0){
browseActivityTitleTextView.setText(" Α'");
}
if(position == 1){
browseActivityTitleTextView.setText(" Β'");
}
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
}
}
当我刷卡它变为B',因为它应该,但是当我回到轻扫它停留B'画廊
任何人都知道如何解决这个问题?
When i swipe the gallery it changes to B' as it should but when i swipe back it stays B' Anyone knows how to fix this?
推荐答案
而不是改变从BaseAdapter文本,你应该添加一个侦听到画廊,并改变这种方式。你也应该找到您onCreate方法引用到TextView的。
Rather than changing the text from the BaseAdapter you should add a listener to the gallery and change it that way. You should also find the reference to the textview in your onCreate method.
private TextView browseActivityTitleTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse_activity_layout);
browseActivityTitleTextView = (TextView)findViewById(R.id.browseActivityTitleTextView);
Gallery gallery = (Gallery) findViewById(R.id.browseActivityGallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSpacing(0);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
switch(position){
case 0:
browseActivityTitleTextView.setText(" Α'");
break;
case 1:
browseActivityTitleTextView.setText(" B'");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
这篇关于画廊的图像变化的TextView更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文