Recyclerview广告

时间:2019-03-15 16:37:23

标签: android android-recyclerview admob

我是Android新手。我正在制作一个简单的应用程序,在其中我要在RecyclerView上显示横幅广告。在几项之后,它应该显示横幅广告。我有1个recylerview,1个适配器并从放置在资产文件夹中的文本文件中检索数据。我看过Google上的所有教程,但不了解需要帮助。

MainActivity

        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_sad);

                RecyclerView quoteList = findViewById(R.id.quoteList);
                quoteList.setLayoutManager(new LinearLayoutManager(this));
                quoteList.setAdapter(new QuotesAdapter(getQuotes(),this));


            }

            private List<String> getQuotes() {
                List<String> quotes = new ArrayList<>();
                BufferedReader bufferedReader = null;

                try {
                    bufferedReader = new BufferedReader(new InputStreamReader(this.getAssets().open("text1.txt"), StandardCharsets.UTF_8));
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        quotes.add(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedReader != null) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return quotes;
            }

xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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/li"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="#9c27B0"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="#fff"
            android:textSize="24sp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/quoteList"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

customlayout

<LinearLayout
        android:padding="16dp"
        android:id="@+id/quoteContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="38dp"
            android:layout_height="38dp"
            android:src="@drawable/quote"
            android:tint="#fff"/>

        <TextView
            android:id="@+id/txtQuote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a Sample quote. You can write anything you want."
            android:textColor="#fff"
            android:gravity="center"
            android:textSize="22sp" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

如果理解正确,则希望每隔一定时间在recyclerview中展示广告。如果是这种情况,则必须在适配器中进行一些更改。

首先在适配器中定义这些变量。

static final int VIEW_TYPE_NEWS = 0;
static final int VIEW_TYPE_AD = 1;
boolean isAd = false;

接下来,在onCreateViewHolder方法上,您必须执行类似的操作。看到有两种不同的list_item布局,请不要忘记在布局中创建它们。

   @Override
    public ViewHolderNews onCreateViewHolder(ViewGroup parent, int viewType) {
        int layoutId = -1;

        switch (viewType) {
            case VIEW_TYPE_AD:
                layoutId = R.layout.list_ad_item;
                break;
            case VIEW_TYPE_NEWS:
                layoutId = R.layout.list_news_item;
                break;
        }

        View view = LayoutInflater.from(parent.getContext())
                .inflate(layoutId, parent, false);

        return new ViewHolderNews(view);
}

然后onBindViewHolder:

    @Override
    public void onBindViewHolder(ViewHolderNews holder, int position) {

        switch (getItemViewType(position)) {
            case VIEW_TYPE_AD:
                // Create an ad request. Check logcat output for the hashed device ID to
                // get test ads on a physical device. e.g.
                // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."

                // Load your ad here.

                break;

            case VIEW_TYPE_NEWS:

               // Load your normal content here.

                break;
        }
}

最后一个要覆盖的方法是:

    @Override
    public int getItemViewType(int position) {
        int positionNew = position + 1;
        if ((positionNew % ITEMS_PER_AD == 0) && (position > 0)) {
            isAd = true;
            return VIEW_TYPE_AD;
        } else {
            isAd = false;
            return VIEW_TYPE_NEWS;
        }
}

我希望这可以解决您的问题。