您好我有一个图库,里面有2张图片。我希望每次我在布局中更改图像时都会更改其名称。在我的代码中,它只是第一次发生并保持这种状态。
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' 任何人都知道如何解决这个问题?
答案 0 :(得分:1)
您不应该更改BaseAdapter中的文本,而应该向图库添加一个监听器并以此方式进行更改。您还应该在onCreate方法中找到对textview的引用。
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) {}
});
}