2012-11-16 16 views
1

我是一名android初學者,我在線性佈局內動態添加了圖像,它將從右向左滾動。我的問題是,圖像正在移動/滾動正確,但是當我點擊它時,不會調用點擊式偵聽器。帶圖像的按鈕在滾動時沒有響應onclick監聽器

實際上,當移動圖像時,圖像按鈕正在移動,但按下圖像的滾動位置不會改變。

真的我找不到方法,請幫幫我。

public class ImageLayoutsActivity extends Activity { 

    int ImageInt, fromXDelta, toXDelta; 
    int PosInt, myHarizantalLayoutInt, aDisplayInt, aDisplayDiv = 0; 


    AnimationSet myAnimation = new AnimationSet(true); 

    LinearLayout myHarizantalLayout; 
    HorizontalScrollView myHorizontalScroll; 

    View myView; 
    Intent myIntent; 

    private Button clickedButton = null; 
    public int currentimageindex = 0; 
    TranslateAnimation myTranslateAnimation = null; 

    private String[] imgArr = { "icn01", "icn02" }; 

    private String[] URLArray = { "http://www.google.co.in/", 
      "http://in.yahoo.com/?p=us" }; 

    LinearLayout.LayoutParams Parems; 

    Display aDisplay; 

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

     View aView = LayoutInflater.from(GroupActivity.myGroup).inflate(
       R.layout.horizantal, null); 
     setContentView(aView); 

     GroupActivity.myGroup.setTitle("---CII---"); 

     aDisplay = getWindowManager().getDefaultDisplay(); 

     aDisplayInt = aDisplay.getWidth(); 

     aDisplayDiv = aDisplayInt/5; 

     Log.i("value##########################", String.valueOf(aDisplayDiv)); 

     Log.i("aDisplayInt##########################", 
       String.valueOf(aDisplayInt)); 





     // define xml layout here 
     myHarizantalLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id); 

     myHorizontalScroll = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id); 

     // Button aBtnOne = (Button) findViewById(R.id.BtnOneId); 

     // Hide HorizantalLayout scroll bar 
     myHorizontalScroll.setHorizontalScrollBarEnabled(false); 

     myHorizontalScroll.isSmoothScrollingEnabled(); 


     myHarizantalLayout.measure(aDisplay.getWidth(), aDisplay.getHeight()); 

     Log.v("[email protected]@@@@@@@@", myHarizantalLayout.getMeasuredHeight() 
       + ", " + myHarizantalLayout.getMeasuredWidth()); 

     int aLayoutInt = myHarizantalLayout.getMeasuredWidth(); 

     Log.i("[email protected]@@@@@@@@@@", String.valueOf(aLayoutInt)); 


     // Add images in for loop 
     for (int i = 0; i < imgArr.length; i++) { 

      // int aVal=myHarizantalLayout.getChildCount(); 

      Log.i("[email protected]@@@@@@@@@@@@@@@@@@@@@@@@@@@", "=====Inside the loop======"); 

      // create button instance 
      final Button aImgBtn = new Button(this); 

      // myButton.setOnClickListener(null); 

      // add background image resource files 
      ImageInt = getResources().getIdentifier(imgArr[i], "drawable", 
        getPackageName()); 

      Log.i("[email protected]@@@@@@@@@@@@@@@@@@@@@@@@@@@", "ImageInt"); 

      // add integer image values to drawable control 
      Drawable aDrawable = this.getResources().getDrawable(ImageInt); 

      Log.i("aDrawable$$$$$$$$$$$$$$$$$$$$$$$$$", "aDrawable"); 

      // add drawable file to button instance 
      aImgBtn.setBackgroundDrawable(aDrawable); 

      aImgBtn.measure(aDisplay.getHeight(), aDisplay.getWidth()); 

      Log.i("aButton################@@@@@@@@@@@@@", "aButton"); 

      Parems = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 

      Parems.setMargins(10, 0, 5, 0); 

      Log.i("Parems%%%%%%%%%%%%%%%%%%%%%%%%%%", "Parems"); 

      // int aParemIn = Parems.width; 

      // Log.i("aparemIn$$$$$$$$$$$$", String.valueOf(aParemIn)); 

      myHarizantalLayout.measure(aDisplay.getHeight(), 
        aDisplay.getWidth()); 

      int myHarizantalLayoutInt = myHarizantalLayout.getMeasuredWidth(); 

      Log.i("myHarizantalLayoutInt$$$$$$$$$$$$", 
        String.valueOf(myHarizantalLayoutInt)); 

      myTranslateAnimation = new TranslateAnimation(aDisplayInt - 300, 
        -myHarizantalLayoutInt - aDisplayDiv, 0, 0); 
      myTranslateAnimation.setDuration(20000); 
      myTranslateAnimation.setRepeatCount(-1); 
      myTranslateAnimation.setRepeatMode(1); 
      myAnimation.addAnimation(myTranslateAnimation); 

      Log.i("myAnimation###########################", "myAnimation"); 


      aImgBtn.setLayoutParams(Parems); 
      myHarizantalLayout.addView(aImgBtn); 

      Log.i("myHarizantalLayout&&&&&&&&&&&&&&&&&&&&&&&&&", 
        "myHarizantalLayout"); 

      // add animation to HarizantalLayout 
      myHarizantalLayout.startAnimation(myTranslateAnimation); 

      // Call webview based on image button click event 
      aImgBtn.setOnClickListener(new OnClickListener() { 

       public void onClick(View view) { 

        Log.i("[email protected]@@@@@@@@@@@@", "---btn clicked -----"); 

        int aVal = myHarizantalLayout.indexOfChild(view); 

        Log.i("indexOfChild*************************", 
          "indexOfChild"); 

        if (view == aImgBtn) { 

         Log.i("aButton%%%%%%%%%%%%%%%%%%%%%%%%", "aButton"); 
         aImgBtn.setId(aVal); 

         String aUrlStr = URLArray[aVal]; 

         Log.i("[email protected]@@@@@@@@@@@@", aUrlStr); 

        } 



        // Log.i("[email protected]@@@@@@@@@@@@", aUrlStr); 

        // Pass values to webview activity 


     } 

    } 

} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:gravity="bottom" android:background="#00f"> 

    <LinearLayout android:layout_below="@+id/RelativeLayout" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:orientation="horizontal" android:gravity="bottom"> 

     <ImageView android:id="@+id/barImageId" 
      android:layout_width="150dp" android:layout_height="58dp" 
      android:background="@drawable/cii" android:layout_alignParentRight="true" /> 

     <HorizontalScrollView android:layout_width="fill_parent" 
      android:layout_height="58dp" android:id="@+id/horiztonal_scrollview_id" 
      android:fadingEdge="none" android:background="#000" > 

      <LinearLayout android:id="@+id/horiztonal_outer_layout_id" 
       android:fadingEdge="none" android:layout_height="fill_parent" android:gravity="center_vertical" 
       android:layout_width="fill_parent" android:background="#f00"> 

      </LinearLayout> 
     </HorizontalScrollView> 

    </LinearLayout> 
</RelativeLayout> 

回答