【问题标题】:How to change TextView with the change of the ImageView in a Carousel in Android如何在 Android 的轮播中随 ImageView 的变化而改变 TextView
【发布时间】:2021-02-10 12:14:09
【问题描述】:

我正在开展一个项目,在该项目中我使用 library 创建了一个轮播。

我还想在 TextView 中显示图像描述,该描述应该随着图像的变化而变化。 随着图像的变化,我无法更改 TextView。我应该怎么做才能做到这一点?

我的 Java 代码:

public class Carousel extends AppCompatActivity {
CarouselView carouselView;

int[] sampleImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about_devs);

    carouselView = findViewById(R.id.carouselView);
    carouselView.setPageCount(sampleImages.length);
    carouselView.setImageListener(imageListener);
}
ImageListener imageListener = (position, imageView) -> imageView.setImageResource(sampleImages[position]);

我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:background="@color/retro_red"
tools:context=".AboutDevs">


<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/pragya"
    android:text="@string/about_devs"
    android:textColor="@color/white"
    android:textSize="28sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.074" />

<com.synnapps.carouselview.CarouselView
    android:id="@+id/carouselView"
    android:layout_width="400dp"
    android:layout_height="450dp"
    android:layout_marginTop="28dp"
    android:layout_marginEnd="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView5"
    app:layout_constraintVertical_bias="0.235" />
</androidx.constraintlayout.widget.ConstraintLayout>

我知道这可能是一个愚蠢的问题,但我无法弄清楚。我将不胜感激。

【问题讨论】:

    标签: java android text carousel


    【解决方案1】:

    从您的 xml 中,我可以看到您正在使用您提到的 Carousel View Library,但 Carousel View Lib 不支持 Image Listener,因此您最好的选择是更改您的库,或者在您的活动中使用 Fragments ,了解更多here

    【讨论】:

      【解决方案2】:

      我不确定我是否正确理解了您的问题,但是

          ImageListener imageListener = new ImageListener() {
              @Override
              public void setImageForPosition(int position, ImageView imageView) {
                  imageView.setImageResource(sampleImages[position]);
              }
          };
      

      你不能在这个方法中添加设置你的 textview 值吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        相关资源
        最近更新 更多