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

前端之家收集整理的这篇文章主要介绍了java-将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);
        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) {

            }
        });
原文链接:https://www.f2er.com/android/531332.html

猜你在找的Android相关文章