accessing the objects of the java class created in xml file(as tag) android

I have a cover flow class extending the Gallery class and I have many variables in the class like Rotation_angle, imageWidth and imageHeight, etc. I am setting the values for all of these variables in XML, and one important thing is that there are two gallery views in my UI. The code is something like this.

‘ public class CoverFlow extends Gallery { private int mMaxRotationAngle ; private float imageHeight; private float imageWidth;

XML:

<view class="pl.polidea.coverflow.CoverFlow" xmlns:coverflow="http://schemas.android.com/apk/res/pl.polidea.coverflow"
            coverflow:imageWidth="100dip" coverflow:mMaxRotationAngle = "270"
             coverflow:imageHeight="100dip" android:id="@+id/coverflow" 
            >

 <pl.polidea.coverflow.CoverFlow xmlns:coverflow="http://schemas.android.com/apk/res/pl.polidea.coverflow"
        coverflow:imageWidth="100dip" coverflow:imageHeight="100dip" coverflow:withReflection="true"
        coverflow:imageReflectionRatio="0.2" coverflow:reflectionGap="2dip" 
        coverflow:mMaxRotationAngle = "145"   android:id="@+id/coverflowReflect"

By doing this I can give different value to the variables declared in the CoverFlow class.

Now what I want is to have the objects in the Java class of the two galleries defined in XML, because I want to override the scrolling actions in such a way that if I scroll one gallery view the other should also scroll automatically. How can I do this?

Answer

This is only idea:

public class FlingGallery extends Gallery {
    private OnFlingListener onFlingListener;

    public FlingGallery(Context context) {
        super(context);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean isConsumed = super.onFling(e1, e2, velocityX, velocityY);

        if(isConsumed) {
            if(onFlingListener != null) {
                onFlingListener.onFling(e1, e2, velocityX, velocityY);
            }
        }

        return isConsumed;
    }

    public OnFlingListener getOnFlingListener() {
        return onFlingListener;
    }

    public void setOnFlingListener(OnFlingListener onFlingListener) {
        this.onFlingListener = onFlingListener;
    }

    public static interface OnFlingListener {
        void onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
    }

Usage:

    final FlingGallery g1 = ...//from somewhere
    final FlingGallery g2 = ...//from somewhere

    g1.setOnFlingListener(new OnFlingListener() {
            @Override
            public void onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
               g2.onFling(e1, e2, velocityX, velocityY);
            }
    });

Scroll on g1 should scroll g2
Modify and test it.

Leave a Reply

Your email address will not be published. Required fields are marked *