#android #android-custom-view
#Android #android-пользовательский вид
Вопрос:
Недавно я запустил Android и создаю диалог с персонализированной каруселью.
Я создал XML, содержащий два элемента, которые будут отображаться в каждом представлении карусели (ImageView и TextView), но когда я создаю свой объект просмотра, я не знаю, как получить доступ к каждому элементу для его инициализации.
Пользовательский вид XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@ id/imageView_carousel_presentacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/com_facebook_button_icon_blue" />
<TextView
android:id="@ id/textView_carouser_presentacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Класс диалога
public class CarouserInicialDialogo {
private Context contexto;
private Dialog dialogo;
private Button btn;
private CarouselView carousel;
private int[] imagenes = {R.drawable.default_profile, R.drawable.felicitades, R.drawable.logo_app};
public CarouserInicialDialogo(final Context contexto) {
dialogo = new Dialog(contexto);
dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogo.setCancelable(true);
dialogo.setContentView(R.layout.dialogo_carousel_inicial);
this.contexto = contexto;
carousel = (CarouselView) dialogo.findViewById(R.id.carousel_view_presenacion);
carousel.setPageCount(imagenes.length);
carousel.setViewListener(viewListener);
dialogo.show();
}
ViewListener viewListener = new ViewListener() {
@Override
public View setViewForPosition(int position) {
LayoutInflater inflater = (LayoutInflater) contexto.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View customView = inflater.inflate(R.layout.custom_view_carouser_presentacion, null);
//set view attributes here
//
//
//
return customView;
}
};
}
Ответ №1:
Вы можете получить доступ к таким представлениям, как это
ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);
Как только вы добавите этот код, ваш класс будет выглядеть следующим образом:
public class CarouserInicialDialogo {
private Context contexto;
private Dialog dialogo;
private Button btn;
private CarouselView carousel;
private int[] imagenes = {R.drawable.default_profile, R.drawable.felicitades, R.drawable.logo_app};
public CarouserInicialDialogo(final Context contexto) {
dialogo = new Dialog(contexto);
dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogo.setCancelable(true);
dialogo.setContentView(R.layout.dialogo_carousel_inicial);
this.contexto = contexto;
carousel = (CarouselView) dialogo.findViewById(R.id.carousel_view_presenacion);
carousel.setPageCount(imagenes.length);
carousel.setViewListener(viewListener);
dialogo.show();
}
ViewListener viewListener = new ViewListener() {
@Override
public View setViewForPosition(int position) {
LayoutInflater inflater = (LayoutInflater) contexto.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View customView = inflater.inflate(R.layout.custom_view_carouser_presentacion, null);
ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);
//Views are initialized with layout.
return customView;
}
};
}
Ответ №2:
Попробуйте это:
customView.findViewById(R.id.imageView_carousel_presentacion)