简体   繁体   中英

Android Studio cannot resolve method SetOnItemClickListener (Java)

I am trying to make carousel with clickable images, made as a list. I want to make so using SetOnItemCLickListener, but when I try to rebuild the project an error pops up and tells me:

error: cannot find symbol method setOnItemClickListener(<anonymous OnItemClickListener>)

I searched the internet for a solution but I am still stuck.

I have tried so far:

  1. Clean the project (obvious);
  2. Searched for name illegal elements, there are none;

When I tried only with SetOnCLickListener it did not work; it is in red-colored letters and there is no method like this.

public class FinalSadMovies extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final_sad_movies);
List<CarouselPicker.PickerItem> imageItems = new ArrayList<>();
        imageItems.add(new CarouselPicker.DrawableItem(R.drawable.joker));
        imageItems.add(new CarouselPicker.DrawableItem(R.drawable.starwars_resized));
        imageItems.add(new CarouselPicker.DrawableItem(R.drawable.test));

        CarouselPicker.CarouselViewAdapter imageAdapter = new CarouselPicker.CarouselViewAdapter(this, imageItems, 0);
        carouselPicker.setAdapter(imageAdapter);
        carouselPicker.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("MainActivity", "ListView item clicked.");
            }
        });

If you're using carouselPicker from GoodieBag then setOnItemClickListener is not supported. The only thing you can rely on is the addOnPageChangeListener but this requires you to change your app UI and flow (because you need to use a button to confirm the selection).

Another option is to create a custom CarouselViewAdapter class where you can handle the click event on the instantiateItem method.

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