获取按钮以在图库中使用充气布局

时间:2011-05-16 16:56:05

标签: android layout button gallery inflate

直到现在我所拥有的是一个自定义的图库活动,其中我使用布局而不是图像来创建一个与Android主屏幕非常相似的可转换UI。在我的应用程序中,这代表了用户可以通过滑动浏览的艺术作品的描述和图片目录 因为gallery使用了所有touchevents,所以我创建了我自己的gallery类的子类,以允许scrollview起作用 我现在正在尝试为膨胀的布局中的某些按钮添加功能。我想再次讨论画廊消耗touchevents的问题,因为在自定义baseAdapters getView方法的膨胀视图上设置.setOnClickListener时没有任何反应。我还尝试在通过onItemSelectedListener获取它们后,在膨胀布局中的Buttons上直接设置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事件? 我该怎么办?如果这是解决方案,我如何为膨胀布局中的每个按钮定义不同的功能(例如,通过意图开始新活动或跳转到目录中的不同“页面”)?

0 个答案:

没有答案