获取走马灯中图片的位置并设置文本

huangapple 未分类评论43阅读模式
英文:

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(&quot;hahahhahahahahhaha&quot;);
                }

                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(&quot;hahahhahahahahhaha&quot;);
     }

This is supposed to show the text &quot;hahahhahahahahhaha&quot; for the first image in the carousel, but its not displaying anything....How do I do this please??

**xml**

    &lt;RelativeLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/tabContent&quot;
        android:background=&quot;@drawable/rounded_corner2&quot;&gt;

        &lt;com.synnapps.carouselview.CarouselView
            android:id=&quot;@+id/carouselView&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;200dp&quot;
            app:fillColor=&quot;#FFFFFFFF&quot;
            app:pageColor=&quot;#00000000&quot;
            app:radius=&quot;6dp&quot;
            app:slideInterval=&quot;5000&quot;
            app:strokeColor=&quot;#FF777777&quot;
            app:strokeWidth=&quot;1dp&quot;/&gt;

      &lt;ImageView
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;200dp&quot;
            android:id=&quot;@+id/safetyImages&quot;
            /&gt;

        &lt;TextView
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:id=&quot;@+id/safetyTextTitle&quot;
            android:layout_below=&quot;@+id/safetyImages&quot;
            android:layout_marginTop=&quot;20dp&quot;
            android:gravity=&quot;center&quot;
            android:textStyle=&quot;bold&quot;
            android:textSize=&quot;15sp&quot;
            /&gt;

        &lt;TextView
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:id=&quot;@+id/safetyTextDescription&quot;
            android:layout_below=&quot;@+id/safetyTextTitle&quot;
            android:layout_marginLeft=&quot;20dp&quot;
            android:layout_marginRight=&quot;20dp&quot;
            android:layout_marginBottom=&quot;20dp&quot;
            android:gravity=&quot;center&quot;
            /&gt;

    &lt;/RelativeLayout&gt;




  [1]: https://github.com/sayyam/carouselview

</details>


huangapple
  • 本文由 发表于 2020年4月9日 07:15:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61111519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定