滚动时,带图像的按钮不响应onclick侦听器

时间:2012-11-16 09:27:57

标签: android button imagebutton horizontalscrollview

我是一个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("EmailActivity@@@@@@@@@", myHarizantalLayout.getMeasuredHeight()
                + ", " + myHarizantalLayout.getMeasuredWidth());

        int aLayoutInt = myHarizantalLayout.getMeasuredWidth();

        Log.i("aLayoutInt@@@@@@@@@@@", String.valueOf(aLayoutInt));


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

            // int aVal=myHarizantalLayout.getChildCount();

            Log.i("ImageInt@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "=====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("ImageInt@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "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("view$$$$$$$$$$$$@@@@@@@@@@@@@", "---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("aUrlStr$$$$$$$$$$$$@@@@@@@@@@@@@", aUrlStr);

                    }



                    // Log.i("aUrlStr$$$$$$$$$$$$@@@@@@@@@@@@@", 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>

1 个答案:

答案 0 :(得分:0)

尝试使用Pager而不是Horizo​​ntalScrollView, 此链接可能有用https://github.com/nostra13/Android-Universal-Image-Loader