简体   繁体   中英

button with image is not responding to onclick listener when scrolling

I am a android beginner, I have added images dynamically inside the linear layout, which will scroll from right to left. The problem is that the images are moving / scrolling properly but when I click on it the on-click listener is not called.

Actually when moving the images the image button is moving but the onclick position is not changed according to the image scrolling.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM