2012-07-19 53 views
0

你能帮我吗?当我按下按钮时,它保持按下,但不要做任何事情。Android - 按钮不响应onClick Horizo​​ntalScrollView与dispatchTouchEvent

对不起,我的英文不好! :)

谢谢!

fasmenuprincipal.xml文件:

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

我要开始充气布局的新活动。我做了一个滚动视图,然后我添加视图到滚动视图,膨胀布局。 这里是德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

是'button1'不'R.layout.fasmenuprincipal'布局 – MAC 2012-07-19 10:34:24

+0

定义是其定义 – Ezrou 2012-07-19 10:39:35

回答

0

试试这个代码

可发现错误

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); 
相关问题