英文:
Get position of image in carousel and set text
问题
private int[] sampleImages = {R.drawable.image1,
R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5,
R.drawable.image6};
ViewListener viewListener = new ViewListener() {
    @Override
    public View setViewForPosition(int position) {
        View customView = getLayoutInflater().inflate(R.layout.fragment_safety, null);
        title = customView.findViewById(R.id.safetyTextTitle);
        description = customView.findViewById(R.id.safetyTextDescription);
        safety_images = customView.findViewById(R.id.safetyImages);
        Glide.with(SafetyFragment.this).load(sampleImages[position]).into(safety_images);
        if (position == 0) {
            description.setText("hahahhahahahahhaha");
        }
        return customView;
    }
};
carouselView.setPageCount(sampleImages.length);
carouselView.setViewListener(viewListener);
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabContent"
    android:background="@drawable/rounded_corner2">
    <com.synnapps.carouselview.CarouselView
        android:id="@+id/carouselView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:fillColor="#FFFFFFFF"
        app:pageColor="#00000000"
        app:radius="6dp"
        app:slideInterval="5000"
        app:strokeColor="#FF777777"
        app:strokeWidth="1dp"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/safetyImages"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/safetyTextTitle"
        android:layout_below="@+id/safetyImages"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="15sp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/safetyTextDescription"
        android:layout_below="@+id/safetyTextTitle"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"/>
</RelativeLayout>
<details>
<summary>英文:</summary>
I am using an android library for displaying image in form of a carousel using [https://github.com/sayyam/carouselview][1]
How do I set a text depending on the image displayed in the carousel.
**I have a list of images in Array**
    private int[] sampleImages = {R.drawable.image1, 
    R.drawable.image2, R.drawable.image3, 
    R.drawable.image4, R.drawable.image5,   
    R.drawable.image6};
**In my java code I have set a custom view Listener according to the doc**
    ViewListener viewListener = new ViewListener() {
            @Override
            public View setViewForPosition(int position) {
                View customView = getLayoutInflater().inflate(R.layout.fragment_safety, null);
                
                title = customView.findViewById(R.id.safetyTextTitle);
                description = customView.findViewById(R.id.safetyTextDescription);
                safety_images = customView.findViewById(R.id.safetyImages);
                Glide.with(SafetyFragment.this).load(sampleImages[position]).into(safety_images);
                if(sampleImages[position]==1){
                    description.setText("hahahhahahahahhaha");
                }
                return customView;
            }
        };
        carouselView.setPageCount(sampleImages.length);
        carouselView.setViewListener(viewListener);
      
       return root;
    }
**As you can see, I tried to use this** 
    if(sampleImages[position]==0){
       description.setText("hahahhahahahahhaha");
     }
This is supposed to show the text "hahahhahahahahhaha" for the first image in the carousel, but its not displaying anything....How do I do this please??
**xml**
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabContent"
        android:background="@drawable/rounded_corner2">
        <com.synnapps.carouselview.CarouselView
            android:id="@+id/carouselView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:fillColor="#FFFFFFFF"
            app:pageColor="#00000000"
            app:radius="6dp"
            app:slideInterval="5000"
            app:strokeColor="#FF777777"
            app:strokeWidth="1dp"/>
      <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/safetyImages"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/safetyTextTitle"
            android:layout_below="@+id/safetyImages"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="15sp"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/safetyTextDescription"
            android:layout_below="@+id/safetyTextTitle"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            />
    </RelativeLayout>
  [1]: https://github.com/sayyam/carouselview
</details>
				专注分享java语言的经验与见解,让所有开发者获益!

评论