creating unique onClick listener for each item in a carousel - Android / Java

1k views Asked by At

I'm attempting to create an onClick listener for each item in my imageCarousel - however I'm unable to create a unique listener for each object in the carousel.

Currently - the listener functions - but it applies to all items in the carousel - and my attempts at finding/using the unique ID for each item has failed.

Current implementation:

    for (int i = 0; i < titleResourcesTypedArray.length(); ++i) {
        // Create new ImageView
        imageItem = new ImageView(this);

        // Set the shadow background
        imageItem.setBackgroundResource(R.drawable.shadow);

        // Set the image view resource
        imageItem.setImageResource(titleResourcesTypedArray.getResourceId(
                i, -1));

        // Set the size of the image view to the previously computed value
        imageItem.setLayoutParams(new LinearLayout.LayoutParams(imageWidth,
                imageWidth));

        //imageItem.setOnClickListener(this);

        // Add image to the carousel
        mCarouselContainer1.addView(imageItem);

        imageItem.setOnClickListener(new View.OnClickListener() {
            // create an onClick Listener
            @Override
            public void onClick(View v) {
                // create an onClick Event (Start the Map Download)
                Intent intent = new Intent(Home.this, Download.class);
                startActivity(intent);

            }
        });

    }

Desired implementation:

I believe I should be using something along the lines of:

        imageItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                titleResourcesTypedArray.getResources();
                if (v.getId() = 7) {
                    Intent intent = new Intent(Home.this, Download.class);
                    startActivity(intent);


                    ...

or perhaps:

        imageItem.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                titleResourcesTypedArray.getResources();
             if (v.getResources() = 10) {
                        // Do Stuff
                        Intent intent = new Intent(Home.this, Download.class);
                        startActivity(intent);                          
                    }


                }
            }

How might I properly identify the ID of each item in a carousel?

2

There are 2 answers

0
Jayesh Elamgodil On

In addition to comparing with ids, there is an additional way to use a 'tag'. You can set a tag on the imageView using the setTag method and then in the onClick method you can compare this tag.

0
zoulou On

To add a View.OnClickListener on items in a Carousel with MotionLayout is not different to any other scenarios. Assume a motionlayout.xml as

<androidx.constraintlayout.motion.widget.MotionLayout ...
    android:id="@+id/motionLayoutCarousel"
    ...
    <ImageView android:id="@+id/item1" .../>
    <ImageView android:id="@+id/item2" .../>
    ...
    <ImageView android:id="@+id/item5" .../>
    <androidx.constraintlayout.helper.widget.Carousel 
        ... />
</androidx.constraintlayout.motion.widget.MotionLayout>        

then in the Fragment (or Activity) where the motionLayoutCarousel is inflated, simply add code like below. Note: example below uses View Binding, but this is actually irrelevant.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    
    MotionlayoutBinding binding = MotionlayoutBinding.inflate(inflater, container, false);

    final @IdRes int[] clickableViews = {
            R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5
    };
    for (@IdRes int v : clickableViews) {
        View clickView = binding.motionLayoutCarousel.findViewById(v);
        
        clickView.setOnClickListener(this);
    }
}

@Override
public void onClick(View v) {
    final @IdRes int id = v.getId();
    Integer idx = (Integer) v.getTag();
    Log.i(LOGTAG, "onClick 0x" + Integer.toHexString(id) + " idx " + idx);
}

In the Adapter of the Carousel you need to assign the index of a certain item via setTag. Then the onClick(View) will allow you to know, which index into the data was actually clicked.

@Override
public void populate(View view, int index) {
    view.setTag(index)
    ...
}