以下是初始化图库的活动代码:
public class CatalogueActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.catalogue_gallery);
//initialize Gallery Widget
MyGallery g = (MyGallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(selectedItemView != null){
switch (position){
case 3:
//GoToMap Button in DetailView1
Button toMap_btn = (Button) selectedItemView.findViewById(R.id.GoToMap01);
toMap_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toCatalogue = new Intent().setClassName("com.uart", "com.uart.uartMain");
toCatalogue.putExtra("tabContent", 71);
startActivity(toCatalogue);
}
});
case 4:
//GoToMap Button in DetailView2
Button toMap_btn2 = (Button) selectedItemView.findViewById(R.id.GoToMap02);
toMap_btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toCatalogue = new Intent().setClassName("com.uart", "com.uart.uartMain");
toCatalogue.putExtra("tabContent", 72);
startActivity(toCatalogue);
}
});
}
}
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mLayoutIds = {
R.layout.cover,
R.layout.catalogue_layout,
R.layout.introduction,
R.layout.artwork_detail,
R.layout.artwork_detail2,
R.layout.artwork_detail3,
R.layout.artwork_detail4
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mLayoutIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(mLayoutIds[position], null);
convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toCatalogue = new Intent().setClassName("com.uart", "com.uart.uartMain");
toCatalogue.putExtra("tabContent", 72);
startActivity(toCatalogue);
}
});
return convertView;
}
}
}
这是我的自定义图库子类:
public class MyGallery extends Gallery {
FriendlyScrollView currScrollView;
boolean isSwiping = false;
public MyGallery(Context ctx, AttributeSet attrSet) {
super(ctx, attrSet);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
currScrollView = getCurrScrollView();
return super.onInterceptTouchEvent(ev);
}
private boolean myGestureDetection(float distanceX){
if(distanceX >= 20 || distanceX <= -20){
Log.i("myGallery", "distanceX = "+distanceX);
isSwiping = true;
}
return isSwiping;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (myGestureDetection(distanceX) == false){
if(currScrollView != null){
currScrollView.scrollBy(0, (int) distanceY);
Log.i("myGallery", "Is scrolling vertical");
}
} else{
//Hier: Horizontal Scroll der Gallery Items
Log.i("myGallery", "Is scrolling horizontal");
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i("myGallery", "is Swiping");
isSwiping = false;
// Calculate swipe-animation duration depending on gesture velocity
float velMax = 2500f;
float velMin = 1000f;
float velX = Math.abs(velocityX);
if (velX > velMax) {
velX = velMax;
} else if (velX < velMin) {
velX = velMin;
}
velX -= 600;
int k = 500000;
int speed = (int) Math.floor(1f / velX * k);
setAnimationDuration(speed);
return true;
}
private FriendlyScrollView getCurrScrollView() {
int pos = getFirstVisiblePosition();
if(pos != getAdapter().getCount()-1)
return (FriendlyScrollView)this.getSelectedView();
else
return null;
}
}
现在的问题是,我是否还要覆盖画廊的ontouch事件? 我该怎么办?如果这是解决方案,我如何为膨胀布局中的每个按钮定义不同的功能(例如,通过意图开始新活动或跳转到目录中的不同“页面”)?