Раздуть вид внутри надутого вида - PullRequest
0 голосов
/ 07 марта 2019

Я использую третье лицо, которое расширяет базовый адаптер.В базовом адаптере я накачал свой пользовательский макет и хочу, чтобы внутри этого пользовательского макета был накачан другой макет.

Сторонний вид - это карты с возможностью смены карт, и я пытаюсь отображать нативную рекламу на Facebook между картами

Вот мой код

custom_ads_cards.xml

<androidx.cardview.widget.CardView 
  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:id="@+id/card_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/white"
  android:gravity="center_horizontal"
  card_view:cardCornerRadius="5dp"
 card_view:cardElevation="4dp"
 card_view:cardUseCompatPadding="true">

   <com.facebook.ads.NativeAdLayout
    android:id="@+id/card_ad_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

   <TextView
    android:id="@+id/txt_advertisement"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@color/black_25"
    android:fontFamily="@font/mr"
    android:padding="8dp"
    android:text="@string/advertisement"
    android:textColor="@color/black"
    android:textSize="@dimen/text_14" />

</androidx.cardview.widget.CardView>

Вид объявления, который я хочу отобразить custom_ad_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_ads_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.facebook.ads.MediaView
    android:id="@+id/card_ad_media"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="start" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <com.facebook.ads.AdIconView
            android:id="@+id/native_ad_icon"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:visibility="visible" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/native_ad_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:ellipsize="end"
                android:lines="1"
                android:textColor="@color/black"
                android:textSize="@dimen/text_15" />

            <TextView
                android:id="@+id/native_ad_sponsored_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="5dp"
                android:ellipsize="end"
                android:lines="1"
                android:textColor="@color/black_25"
                android:textSize="@dimen/text_12" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ad_choices_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:orientation="horizontal" />

    </LinearLayout>


    <TextView
        android:id="@+id/native_ad_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:lines="2"
        android:paddingStart="2dp"
        android:paddingEnd="2dp"
        android:textColor="@color/black_50"
        android:textSize="@dimen/text_12" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:paddingStart="2dp"
        android:paddingTop="3dp"
        android:paddingEnd="2dp">

        <TextView
            android:id="@+id/native_ad_social_context"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="10dp"
            android:layout_toStartOf="@id/native_ad_call_to_action"
            android:ellipsize="end"
            android:gravity="start"
            android:lines="1"
            android:textColor="@color/black_25"
            android:textSize="@dimen/text_12" />

        <Button
            android:id="@+id/native_ad_call_to_action"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:background="#4286F4"
            android:textColor="@color/white"
            android:textSize="@dimen/text_12"
            android:visibility="visible" />

    </RelativeLayout>

</LinearLayout>

А вот метод get view моего базового адаптера

 @Override
public View getView(final int position, View view, ViewGroup parent) {
    View v = view;

v = LayoutInflater.from(myContext).inflate(R.layout.custom_ads_cards, null);
            NativeAdLayout fbAdLayout = v.findViewById(R.id.card_ad_layout);

            com.facebook.ads.NativeAd nativeAd = new com.facebook.ads.NativeAd(myContext, "YOUR_PLACEMENT_ID");

            nativeAd.setAdListener(new NativeAdListener() {
                @Override
                public void onMediaDownloaded(Ad ad) {
                    // Native ad finished downloading all assets
                    Log.e("TAG", "CARD ad finished downloading all assets.");
                }

                @Override
                public void onError(Ad ad, AdError adError) {
                    // Native ad failed to load
                    Log.e("TAG", "CARD ad failed to load: " + adError.getErrorMessage());

                }

                @Override
                public void onAdLoaded(Ad ad) {
                    // Native ad is loaded and ready to be displayed
                    Log.d("TAG", "CARD ad is loaded and ready to be displayed!");
                    if (nativeAd != ad) {
                        return;
                    }

                    RelativeLayout fbAdView = (RelativeLayout) LayoutInflater.from(myContext).
                            inflate(R.layout.custom_ad_layout, fbAdLayout,false);
                    fbAdLayout.addView(fbAdView);

                    LinearLayout adChoicesContainer = fbAdView.findViewById(R.id.ad_choices_container);
                    AdOptionsView adOptionsView = new AdOptionsView(myContext, nativeAd, fbAdLayout);
                    adChoicesContainer.removeAllViews();
                    adChoicesContainer.addView(adOptionsView, 0);

                    // Create native UI using the ad metadata.
                    AdIconView nativeAdIcon = fbAdView.findViewById(R.id.native_ad_icon);
                    TextView nativeAdTitle = fbAView.findViewById(R.id.native_ad_title);
                    MediaView nativeAdMedia = fbAdView.findViewById(R.id.card_ad_media);
                    TextView nativeAdSocialContext = fbAdView.findViewById(R.id.native_ad_social_context);
                    TextView nativeAdBody = fbAdView.findViewById(R.id.native_ad_body);
                    TextView sponsoredLabel = fbAdView.findViewById(R.id.native_ad_sponsored_label);
                    Button nativeAdCallToAction = fbAdView.findViewById(R.id.native_ad_call_to_action);

                    // Set the Text.
                    nativeAdTitle.setText(nativeAd.getAdvertiserName());
                    nativeAdBody.setText(nativeAd.getAdBodyText());
                    nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                    nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
                    nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                    sponsoredLabel.setText(nativeAd.getSponsoredTranslation());

                    // Create a list of clickable views
                    List<View> clickableViews = new ArrayList<>();
                    clickableViews.add(nativeAdTitle);
                    clickableViews.add(nativeAdCallToAction);

                    // Register the Title and CTA button to listen for clicks.
                    nativeAd.registerViewForInteraction(
                            fbAdView,
                            nativeAdMedia,
                            nativeAdIcon,
                            clickableViews);

                }

                @Override
                public void onAdClicked(Ad ad) {
                    // Native ad clicked
                    Log.d("TAG", "CARD ad clicked!");
                }

                @Override
                public void onLoggingImpression(Ad ad) {
                    // Native ad impression
                    Log.d("TAG", "CARD ad impression logged!");
                }
            });

            // Request an ad
            nativeAd.loadAd();
    return v;   
    }
...