سؤال

I'm using Fredrik Bornander's SandBoxView from this tutorial http://www.codeproject.com/Articles/319401/Simple-Gestures-on-Android
It works efficiently for an imageview but I want to use multiple imageviews.
Here is my code under GestureActivity but It still works for only one imageview.
Can anyone help me about this?

public class GesturesActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.d);
    SandboxView s = new SandboxView(this, bitmap);

    Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
    SandboxView s2 = new SandboxView(this, bitmap2);


    RelativeLayout ly=new RelativeLayout(this);
    ly.addView(s);
    ly.addView(s2);
    setContentView(ly);
}
هل كانت مفيدة؟

المحلول

The problem is that that control isn't being laid out with any LayoutParameters and it will always take all of the available space if that is the case, so you get two SandboxViews overlapping and that means only the top one receives the touch events.

To fix this (without too much hassle) change the layout to make sure the available space is split between the two controls;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.d);
  SandboxView s = new SandboxView(this, bitmap);

  Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
  SandboxView s2 = new SandboxView(this, bitmap2);

  LinearLayout l = new LinearLayout(this);
  l.setOrientation(LinearLayout.VERTICAL);

  // Note the weight parameter of 1, making sure they split the space between them
  LayoutParams lp1 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
  LayoutParams lp2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
  l.addView(s, lp1);
  l.addView(s2, lp2);
  setContentView(l);
}

This is easier to do if you add the three constructors to SandboxView from it's base class View, that will allow the control to show up in the designer and it's a lot easier to play around with the layout in there (which is admittedly what I should have done in the first place when I wrote the article).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top