gr33tx
@gr33tx

Как сделать так, чтобы нативный блок Admob не перехватывал жесты?

Здравствуйте! У меня в приложении есть фрагмент, который появлется над активити. Я его могу скрыть с помощью свайпа, то есть смахиванием в сторону. Gestures на нем настроил, все ОК. Но во фрагменте должен показывать рекламный блок (нативный Admob). Когда свайпаешь по блоку, то жесты игнорируются. Жесты работают только за пределами блока, т.е. если свайпать по пространству фрагмента. Получается натвный блок перехватывает все события касаний.
Подскажите пожалуйста, как сделать так, чтобы я мог сделать свайп по рекламному блоку?

Рекламный блок использует шаблон
AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
                .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
                    @Override
                    public void onNativeAdLoaded(NativeAd nativeAd) {
                        // Show the ad.
                        Log.d("serg", "loaded");
                        NativeTemplateStyle styles = new
                                NativeTemplateStyle.Builder().withMainBackgroundColor(background).build();
                        TemplateView template = findViewById(R.id.my_template);
                        template.setStyles(styles);
                        template.setNativeAd(nativeAd);
                    }
                })
                .withAdListener(new AdListener() {
                    @Override
                    public void onAdFailedToLoad(LoadAdError adError) {
                        // Handle the failure by logging, altering the UI, and so on.
                        Log.d("serg", "fail"+adError.toString());
                    }
                })
                .withNativeAdOptions(new NativeAdOptions.Builder()
                        // Methods in the NativeAdOptions.Builder class can be
                        // used here to specify individual options settings.
                        .build())
                .build();

        // Creating  an Ad Request
        AdRequest adRequest = new AdRequest.Builder().build() ;

        // load Native Ad with the Request
        adLoader.loadAd(adRequest);


Слушатель тач для фрагмента
// listening for touch events
View ad_fragment = findViewById(R.id.ad_fragment);
        ad_fragment.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
          
            public void onSwipeRight() {                
                fragmentManager.beginTransaction()
                        .setCustomAnimations(
                                R.anim.slide_in,  // enter
                                R.anim.slide_right_out  // exit
                        )
                        .hide(fragment1)
                        .disallowAddToBackStack()
                        .commit();
                toggleShowed = 0;

            }
            public void onSwipeLeft() {
                //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
                if (toggleShowed == 0) {                   
                } else {                    
                    fragmentManager.beginTransaction()
                            .setCustomAnimations(
                                    R.anim.slide_in,  // enter
                                    R.anim.slide_left_out  // exit
                            )
                            .hide(fragment1)
                            .disallowAddToBackStack()
                            .commit();
                    toggleShowed = 0;
                }
            }         
        });

Фрагмент
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:background="#000000"
    tools:context=".AdFragment">
    <LinearLayout
        android:id="@+id/adLayout"
        android:layout_gravity="center"
        android:text="gravity = center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:showIn="@layout/activity_main" >
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Ad"
            android:textColor="#FFDD00"
            android:textSize="19sp" />
        <!--  This is your template view -->
        <com.google.android.ads.nativetemplates.TemplateView
            android:id="@+id/my_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:gnt_template_type="@layout/gnt_medium_template_view" >
        </com.google.android.ads.nativetemplates.TemplateView>
    </LinearLayout>
</FrameLayout>

Main activity
<?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:id="@+id/activityMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".MainActivity">
    <androidx.fragment.app.FragmentContainerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/ad_fragment"
            android:name="ru.shutochki.AdFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="MissingConstraints" />
    </androidx.fragment.app.FragmentContainerView>
</androidx.constraintlayout.widget.ConstraintLayout>
  • Вопрос задан
  • 40 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы