Get button to work in gallery with inflated layouts

匿名 (未验证) 提交于 2019-12-03 09:21:58

问题:

what i have until now is a customized gallery activity in which i inflate layouts instead of images to create a swipable ui quite similar to the android homescreen. In my application this represents a catalogue of descriptions and pictures of artworks the user can browse through by swiping.
Because gallery consumes all touchevents, i made my own subclass of the gallery class to allow scrollviews to function.

Here is the code of the activity where the gallery is initialized:

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

And here is my customized gallery subclass:

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!