Java 在carouse android中为每张照片设置标题和说明
我正在为我的应用程序使用CarouseView库 图像正在显示,旋转木马正在工作,但如何在每张图像滑动时在每张图像下方设置标题和说明 我有一个字符串,包含每个图像的所有标题和文本 e、 gJava 在carouse android中为每张照片设置标题和说明,java,android,Java,Android,我正在为我的应用程序使用CarouseView库 图像正在显示,旋转木马正在工作,但如何在每张图像滑动时在每张图像下方设置标题和说明 我有一个字符串,包含每个图像的所有标题和文本 e、 g 我找到了一个办法 1.为要在每个转盘中显示的每个文本声明一个字符串数组 private String[] titles = {"One", "Two", "Three"} 2。设置自定义视图侦听器并在布局中获取视图 ViewListener vie
我找到了一个办法 1.为要在每个转盘中显示的每个文本声明一个字符串数组
private String[] titles = {"One", "Two", "Three"}
2。设置自定义视图侦听器并在布局中获取视图
ViewListener viewListener = new ViewListener() {
int i;
@Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.fragment_safety, null);
//set view attributes here
safety_title = customView.findViewById(R.id.safetyTextTitle);
safety_images = customView.findViewById(R.id.safetyImages);
Glide.with(SafetyFragment.this).load(sampleImages[position]).into(safety_images);
//Very Important
safety_title.setText(titles[position]);
return customView;
}
};
我找到了一个办法 1.为要在每个转盘中显示的每个文本声明一个字符串数组
private String[] titles = {"One", "Two", "Three"}
2。设置自定义视图侦听器并在布局中获取视图
ViewListener viewListener = new ViewListener() {
int i;
@Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.fragment_safety, null);
//set view attributes here
safety_title = customView.findViewById(R.id.safetyTextTitle);
safety_images = customView.findViewById(R.id.safetyImages);
Glide.with(SafetyFragment.this).load(sampleImages[position]).into(safety_images);
//Very Important
safety_title.setText(titles[position]);
return customView;
}
};
根据文档,您可以创建自定义视图并将视图与数据绑定
ViewListener viewListener = new ViewListener() {
@Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.view_custom, null);
//set view attributes here
return customView;
}
};
根据文档,您可以创建自定义视图并将视图与数据绑定
ViewListener viewListener = new ViewListener() {
@Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.view_custom, null);
//set view attributes here
return customView;
}
};
我这样说:
ViewListener viewListener = new ViewListener() {
@Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.custom_carousel, null);
//set view attributes here
TextView car_title = (TextView) customView.findViewById(R.id.textTitle);
ImageView car_image = (ImageView) customView.findViewById(R.id.imageView);
car_title.setText(sampleTitle[position]);
car_image.setImageResource(sampleImages[position]);
return customView;
}
};
而custom_carousel.xml中的自定义视图将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textDesc"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
tools:src="@tools:sample/avatars[1]" />
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="test" />
</RelativeLayout>
我用这种方式:
ViewListener viewListener = new ViewListener() {
@Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.custom_carousel, null);
//set view attributes here
TextView car_title = (TextView) customView.findViewById(R.id.textTitle);
ImageView car_image = (ImageView) customView.findViewById(R.id.imageView);
car_title.setText(sampleTitle[position]);
car_image.setImageResource(sampleImages[position]);
return customView;
}
};
而custom_carousel.xml中的自定义视图将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textDesc"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
tools:src="@tools:sample/avatars[1]" />
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="test" />
</RelativeLayout>