1

我是一名 android 初学者,我在线性布局中动态添加了图像,它将从右向左滚动。问题是图像正在正确移动/滚动,但是当我单击它时,不会调用单击侦听器。

实际上,当移动图像时,图像按钮正在移动,但点击位置不会根据图像滚动而改变。

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>
4

1 回答 1

0

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

于 2012-11-16T09:57:28.020 回答