5

我有一个活动,其中广告填充在从 java 代码填充的动态表上,该表具有从网络异步加载的图像。当我不包括广告时,一切都按我的预期工作。但是,当我包含 adview 以及从网络异步加载的图像时,它会挂起 UI,直到从网络加载图像。

我无法理解这种行为,您能否给我解决方案,图像应与 adview 一起异步加载。

以下是我的活动代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="********"
        ads:loadAdOnCreate="true" 
         />

    <ScrollView
        android:id="@+id/scroll1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableLayout
            android:id="@+id/table_layout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableRow>

                <TextView
                    android:id="@+id/left_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:gravity="left|center_vertical"
                    android:padding="5dip"
                    android:text="Code" />

                <TextView
                    android:id="@+id/middle_text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="right|center_vertical"
                    android:padding="5dip"
                    android:text="Name of Company" />

                <TextView
                    android:id="@+id/right_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:gravity="right|center_vertical"
                    android:padding="5dip"
                    android:text="1.3" />
            </TableRow>
        </TableLayout>
    </ScrollView>

</LinearLayout>

谢谢

阿曼迪普

4

2 回答 2

1

也许您需要将 adView 从主 LinearLayout 中分离出来,而不是将其加载到将完成网络(异步)活动的 Linear Layout 中。

只需尝试使用 Frame Layout,adView 位于顶部,margin_top 为“55dp”,用于 LinearLayout。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">

<com.google.ads.AdView xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/main_adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center"
    ads:adUnitId = "******"
    ads:adSize = "BANNER"
    >

</com.google.ads.AdView>
</LinearLayout>

<LinearLayout 
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:orientation="vertical">


<ScrollView
    android:id="@+id/scroll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/table_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow>

            <TextView
                android:id="@+id/left_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:gravity="left|center_vertical"
                android:padding="5dip"
                android:text="Code" />

            <TextView
                android:id="@+id/middle_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right|center_vertical"
                android:padding="5dip"
                android:text="Name of Company" />

            <TextView
                android:id="@+id/right_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:gravity="right|center_vertical"
                android:padding="5dip"
                android:text="1.3" />
        </TableRow>
    </TableLayout>
</ScrollView>

</LinearLayout>
</FrameLayout>

请让我知道状态。

于 2013-09-19T05:25:04.027 回答
0

Admob 原生广告也有类似的问题。我想在回收站视图中放置广告。滚动浏览,当我点击原生广告时 UI 会冻结,并且只有在广告加载后才会继续滚动

我做了一些计时,发现 loadAd() 大约需要 1400 毫秒,随后对 loadAd() 的调用时间下降到 150 毫秒,所以我的解决方法是在我的片段创建后创建一个原生广告:

private static boolean preLoadAd = true;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_help, container, false);
    cardsAdapter = new CardsAdapter(Cards.cards, this);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.card_collection);

    //Pre-load ad to allow smoother scrolling
    //First time loading a native ad takes 800-1500ms, afterwards around 150ms
    if (preLoadAd) {
        //only do this once
        preLoadAd=false;
        //add the task to the message cue to run after the help fragment has shown
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    CardVariations.createNativeAdViewHolder(recyclerView);
                }catch(Exception e){e.printStackTrace();}
            }},200L);
    }
    return rootView;
}
于 2016-09-19T10:54:04.607 回答