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

java-将Onclick()添加到ArrayList中的项目,第1张

概述我创建了一个由多个项目(图像和文本)组成的水平滚动,但是如何向这些项目中的每个事件添加点击事件.我已经按照本教程来了解自己的位置:https://www.youtube.com/watch?v=sTJm1Ys9jMIMainActivity.java: CarouselPicker carouselPicker1, carouselPicker2, caro

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

我已经按照本教程来了解自己的位置:
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);        carouselPicker1.setAdapter(imageAdapter);        //Carousel 2 with all text        List<CarouselPicker.PickerItem> textItems = new ArrayList<>();        textItems.add(new CarouselPicker.TextItem("One",20));        textItems.add(new CarouselPicker.TextItem("Two",20));        textItems.add(new CarouselPicker.TextItem("Three",20));        CarouselPicker.CarouselVIEwAdapter textAdapter = new CarouselPicker.CarouselVIEwAdapter(this,textItems,0);        carouselPicker2.setAdapter(textAdapter);        //Carousel 3 with both        List<CarouselPicker.PickerItem> mixItems = new ArrayList<>();        mixItems.add(new CarouselPicker.TextItem("One",20));        mixItems.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher_round));        mixItems.add(new CarouselPicker.TextItem("Three",20));        mixItems.add(new CarouselPicker.DrawableItem(R.mipmap.ic_launcher));        CarouselPicker.CarouselVIEwAdapter mixAdapter = new CarouselPicker.CarouselVIEwAdapter(this,mixItems,0);        carouselPicker3.setAdapter(mixAdapter);

Activity_Main.xml:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    tools:context="MainActivity">    <in.goodIEbag.carouselpicker.CarouselPicker        androID:ID="@+ID/carouselPicker1"        androID:layout_margintop="20dp"        androID:layout_marginBottom="20dp"        androID:background="#FFF"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        app:items_visible="three"        />    <in.goodIEbag.carouselpicker.CarouselPicker        androID:ID="@+ID/carouselPicker2"        androID:layout_margintop="20dp"        androID:layout_marginBottom="20dp"        androID:background="#FFF"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        app:items_visible="three"        />    <in.goodIEbag.carouselpicker.CarouselPicker        androID:ID="@+ID/carouselPicker3"        androID:layout_margintop="20dp"        androID:layout_marginBottom="20dp"        androID:background="#FFF"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        app:items_visible="three"        /></linearLayout>

Gradle脚本:

dependencIEs {    implementation filetree(dir: 'libs',include: ['*.jar'])    implementation 'com.androID.support:appcompat-v7:28.0.0'    implementation 'com.androID.support.constraint:constraint-layout:1.1.3'    testImplementation 'junit:junit:4.12'    androIDTestImplementation 'com.androID.support.test:runner:1.0.2'    androIDTestImplementation 'com.androID.support.test.espresso:espresso-core:3.0.2'    //add library    implementation "com.github.Vatican-Cameos:CarouselPicker:v1.0"}allprojects {    repositorIEs {        Google()        jcenter()        maven { url 'https://jitpack.io'}    }}

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

这是MainActivity中的新代码:

package com.example.carouselpicker;import androID.support.v4.vIEw.VIEwPager;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.Toast;import java.util.ArrayList;import java.util.List;import in.goodIEbag.carouselpicker.CarouselPicker;public class MainActivity extends AppCompatActivity {    CarouselPicker carouselPicker1,carouselPicker3;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        carouselPicker1 = findVIEwByID(R.ID.carouselPicker1);        carouselPicker2 = findVIEwByID(R.ID.carouselPicker2);        carouselPicker3 = 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,0);        carouselPicker3.setAdapter(mixAdapter);        carouselPicker1.addOnPagechangelistener(new VIEwPager.OnPagechangelistener() {            @OverrIDe            public voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {            }            @OverrIDe            public voID onPageSelected(int position) {                //position of the selected item                switch (position) {                    case 0:                        Toast toast = Toast.makeText(getApplicationContext(),"This is a message displayed in a Toast",Toast.LENGTH_SHORT);                        toast.show();                        break;                    case 1:                        Toast oast = Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT);                        oast.show();                        break;                    case 2:                        // do smth else                        break;                    //case 3,etc...                }            }            @OverrIDe            public voID onPageScrollStateChanged(int state) {            }        });    }}
最佳答案您应该按照此处的说明进行 *** 作:

https://github.com/GoodieBag/CarouselPicker

为每个轮播选择器设置侦听器,然后为每个位置的特定逻辑使用开关盒:

carouselPicker.addOnPagechangelistener(new VIEwPager.OnPagechangelistener() {            @OverrIDe            public voID onPageScrolled(int position,int positionOffsetPixels) {            }            @OverrIDe            public voID onPageSelected(int position) {                //position of the selected item                switch (position) {                        case 0:                            //do smth                            Toast.makeText(MainActivity.this,"first item selected",Toast.LENGTH_SHORT).show();                        break;                        case 1:                            // do smth else                            Toast.makeText(MainActivity.this,"second item selected",Toast.LENGTH_SHORT).show();                        break;                        //case 3,etc...                     }            }            @OverrIDe            public voID onPageScrollStateChanged(int state) {            }        });
总结

以上是内存溢出为你收集整理的java-将Onclick()添加到ArrayList中的项目 全部内容,希望文章能够帮你解决java-将Onclick()添加到ArrayList中的项目 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存