2012-07-19 4 views
0

Pouvez-vous m'aider? Quand j'appuie sur le bouton, il reste enfoncé, mais ne fais rien.Android - Le bouton ne répond pasOnCliquez sur HorizontalScrollView avec dispatchTouchEvent

Désolé pour mon mauvais anglais! :)

Merci!

fasmenuprincipal.xml fichier:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

Je veux commencer une nouvelle activité de mise en page décompressé. J'ai fait un scrollview, puis j'ajoute des vues à la scrollview, gonflant les layouts. Voici de code Java:

public class ProvaPedidoScroll extends Activity { 

    Context mContext; 
    HorizontalScrollView mScrollView; 
    LinearLayout mLinearLayout; 
    LinearLayout.LayoutParams mLinearLayoutParams; 
    Display mDisplay; 
    // scroll behaviour 
    private int mScrollStartPosition; 
    private static final float SCROLL_MARGIN = 0.2f; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mContext = this; 
     // load layout from xml and get references to sub-views 
     setContentView(R.layout.scrollview); 
     mScrollView = (HorizontalScrollView) findViewById(R.id.scrollview); 
     mLinearLayout = (LinearLayout) findViewById(R.id.scrollviewlinearlayout); 
     // get a display reference (used to find screen size) 
     mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     // get the layout parameters to apply to the sub-views 
     mLinearLayoutParams = new LayoutParams(mDisplay.getWidth(), mDisplay.getHeight()); 
     // add some views to the ScrollView 
     addViewsToScrollView(); 
    } 

    /** 
    * Inflates and adds some views to the ScrollView 
    */ 
    private void addViewsToScrollView() { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 

     View menuprincipal = inflater.inflate(R.layout.fasmenuprincipal, null); 
     menuprincipal.setLayoutParams(mLinearLayoutParams); 
     mLinearLayout.addView(menuprincipal); 

     Button ClientesMenu = (Button) menuprincipal.findViewById(R.id.button1); 

     ClientesMenu.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       int iOpcio = 1; 
       Intent VeureClients = new Intent(ProvaPedidoScroll.this, buscar_client.class); 
       VeureClients.putExtra("Opcio", iOpcio); 
       startActivity(VeureClients); 

      } 
     }); 

     View view2 = inflater.inflate(R.layout.fasmenugestion, null); 
     view2.setLayoutParams(mLinearLayoutParams); 
     mLinearLayout.addView(view2); 

    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
     int viewWidth = mDisplay.getWidth(); // width of each view 
     int triggerWidth = (int) (SCROLL_MARGIN * viewWidth); // amount user has to scroll to move to next view 
     int pos = mScrollView.getScrollX(); 
     int diff = pos % viewWidth; // offset of current scroll from leftmost view's snap position 
     int posLeftView = pos - diff; // absolute snap position of the leftmost view on screen 
     switch (ev.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       // Record the starting scroll position. This is used to decide the scroll direction. 
       mScrollStartPosition = pos; 
       break; 
      case MotionEvent.ACTION_UP: 
       if (pos > mScrollStartPosition) { 
        // Scrolling right 
        if (diff > triggerWidth) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0); 
        else mScrollView.smoothScrollTo(posLeftView, 0); 
       } else { 
        // Scrolling left 
        if (diff > (viewWidth - triggerWidth)) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0); 
        else mScrollView.smoothScrollTo(posLeftView, 0); 
       } 
       // replacing our scrollTo command with it's own 
       return true; 
     } 
     return super.dispatchTouchEvent(ev); 
    } 


} 
+0

Peut être 'button1' n'est pas défini dans' R.layout.fasmenuprincipal' mise en page – MAC

+0

oui c'est défini – Ezrou

Répondre

0

essayer ce code

peut trouver d 'erreur

HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1); 

     LinearLayout topLinearLayout = new LinearLayout(this); 
     // topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT); 
     topLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 

     for (int i = 0; i < 15; i++){ 



      final ImageView imageView = new ImageView (this); 

      imageView.setTag(i); 

      imageView.setImageResource(R.drawable.ic_launcher); 

      topLinearLayout.addView(imageView); 

      imageView.setOnClickListener(new OnClickListener() 
      { 

       @Override 
       public void onClick(View v) 
       { 
        // TODO Auto-generated method stub 
        Log.e("Tag",""+imageView.getTag()); 
       } 
      }); 


     } 

     scrollView.addView(topLinearLayout); 
Questions connexes