我有5张图片的堆栈视图。现在我想在堆栈视图顶部设置与图像相同的背景,并且当下一个图像的滑动堆栈时应该更改背景。我试图从setOnItemSelectedListener
设置,但它在堆栈视图中不起作用。
这是我的代码..
public class MainActivity extends ActionBarActivity {
public static String Tag= "MainActivity";
StackView stackView;
ImageView rlayout;
Drawable d;
ImageView imageView;
Integer[] images = {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stackView = (StackView)this.findViewById(R.id.stackview);
stackView.setAdapter(new ImageAdapter(this));
rlayout = (ImageView)this.findViewById(R.id.dfs);
stackView.setRotation(180);
stackView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.e(Tag, "current position: "+position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context contxt;
public ImageAdapter(Context c) {
contxt = c;
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return position;
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public View getView(int position, View view, ViewGroup
parent) {
if (view == null) {
LayoutInflater vi = (LayoutInflater)
getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.item, null, false);
}
imageView = (ImageView) view.findViewById(
R.id.imageView);
imageView.setImageResource(images[position]);
imageView.setRotation(180);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("image click", "IMAGE CLICK");
}
});
return view;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
Log.e("position", ""+position);
return 0;
}
}
}