初始化自定义视图元素

Initialize custom view elements

我最近 Android 开始使用个性化轮播创建对话。

我创建了一个 xml,其中包含将在 carouser 的每个视图中看到的两个元素(ImageView 和 TextView),但是当我创建我的 View 对象时,我不知道如何访问每个元素对其进行初始化。

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>

对话框class

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;
        }
    };
}

试试这个:

customView.findViewById(R.id.imageView_carousel_presentacion)

您可以访问这样的视图

ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);

添加此代码后,您的 class 将如下所示:

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;
    }
};
}