猿问

将 Onclick() 添加到 ArrayList 中的项目

我已经创建了一个由多个项目(图像和文本)组成的水平滚动,但是如何为每个项目添加一个点击事件。


我已经按照本教程获得了现在的位置: https ://www.youtube.com/watch?v=sTJm1Ys9jMI


MainActivity.java:


 CarouselPicker carouselPicker1, carouselPicker2, carouselPicker3;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        carouselPicker1 = (CarouselPicker) findViewById(R.id.carouselPicker1);

        carouselPicker2 = (CarouselPicker) findViewById(R.id.carouselPicker2);

        carouselPicker3 = (CarouselPicker) findViewById(R.id.carouselPicker3);



//Carousel 1 with all images


        List<CarouselPicker.PickerItem> itemsImages = new ArrayList<>();

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher_round));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        itemsImages.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));

        CarouselPicker.CarouselViewAdapter imageAdapter = new CarouselPicker.CarouselViewAdapter(this, itemsImages, 0);

</LinearLayout>


如何为 MainActivy.java 中显示的每个图像添加按钮功能?


繁星淼淼
浏览 75回答 1
1回答

杨魅力

您应该按照此处的说明进行操作:https://github.com/GoodieBag/CarouselPicker为每个 Carousel 选择器设置监听器,然后为每个位置使用 switch-case 的特定逻辑:carouselPicker.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onPageSelected(int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //position of the selected item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do smth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this, "first item selected", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do smth else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this, "second item selected", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //case 3, etc...&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onPageScrollStateChanged(int state) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答