到目前为止,我拥有的是一个自定义的画廊活动,我在其中膨胀布局而不是图像来创建一个与 android 主屏幕非常相似的可滑动用户界面。在我的应用程序中,这表示用户可以通过滑动浏览的艺术品的描述和图片目录。
因为画廊会消耗所有的触摸事件,所以我创建了自己的画廊类的子类以允许滚动视图发挥作用。
我现在正在尝试为膨胀布局中的某些按钮添加功能。我认为这里又是画廊消耗触摸事件的问题,因为在自定义 baseAdapters getView 方法中的膨胀视图上设置 .setOnClickListener 时没有任何反应。在通过 onItemSelectedListener 获取它们之后,我还尝试直接在膨胀布局中的按钮上设置 onClickListener,但结果是空指针异常。
这是初始化画廊的活动的代码:
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 事件?我该怎么做?如果这是解决方案,我如何为膨胀布局中的每个按钮定义不同的功能(例如,通过意图启动新活动或跳转到目录中的不同“页面”)?