0

您好,我有一个画廊,里面有 2 张图片。每次我更改图像时,我都希望在我的布局中使用 textview 来更改其名称。在我的代码中,它只是第一次发生并且保持这种状态。

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' 任何人都知道如何解决这个问题?

4

1 回答 1

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

}
于 2012-04-24T14:20:37.620 回答