# 多重广告

<div align="left"><figure><img src="/files/XtuXLt3yWrHh5VMI568b" alt=""><figcaption></figcaption></figure></div>

多重广告是适用于系统ListView或者RecyclerView列表中穿插广告内容，保持风格与布局依然和用户一致，做到完美融入列表布局的广告类型

## 前提

用户完成广告初始化步骤[入门指南](/copy-of-cn/jie-kou-wen-dang/android/kai-shi/ru-men-zhi-nan.md)

{% hint style="info" %}
本指南采用RecyclerView做为展示示例，ListView相关展示流程相同
{% endhint %}

## 控件初始化

首先，基于多重广告展示原理，在列表数据填充前，需要完成控件初始化，示例如下

```java
public class CustomActivity extends AppCompatActivity {

    RecyclerView recyview;
    QuickAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xml);
        initView();
        setCustomView();
    }


    private void initView() {
     recyview = findViewById(R.id.recyview);
    }

    private void setCustomView() {
        Adx3CustomAdView.getInstance().loadData(getApplicationContext(), "UnitId");
    }

}
 
```

## 列表适配器添加

广告控件初始化完成后，根据示例，用户完成列表控件初始化与数据填充，示例如下

```java
LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyview.setLayoutManager(manager);

        adapter = new QuickAdapter(getStrings());
        recyview.setAdapter(adapter);
```

{% hint style="warning" %}
根据RecyclerView模式当前示例展示为横向队列，**QuickAdapter**为RecyclerView默认数据适配器，请用户根据自身业务需求做自身调整
{% endhint %}

## 布局

{% hint style="info" %}
当前布局配置为列表布局单个item布局，根据业务实际要求，布局样式布局可能不同，参考示例如下
{% endhint %}

```java
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/layout_item"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <io.adx3.library.view.Adx3CustomLayoutAdview
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <io.adx3.library.view.Adx3ImageItemView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <io.adx3.library.view.Adx3TextItemView
            android:id="@+id/textview"
            android:layout_below="@+id/image"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="30dp" />

    </io.adx3.library.view.Adx3CustomLayoutAdview>

</androidx.constraintlayout.widget.ConstraintLayout>
```

当前布局采用图片+文本的形式进行页面展示

## 数据适配器

完成布局与控件初始化后，针对列表控件，以下示例，展示适配器填充与控件相关事件操作

```java
public class QuickAdapter extends RecyclerView.Adapter<QuickAdapter.ViewHolder> {

    List<String> list;

    public QuickAdapter(List<String> strings) {
        list = strings;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclview, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull QuickAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.view.setData(position, new CustomItemListener() {
            @Override
            public void loadByUser() {
                
            }

            @Override
            public void loadUrl(String url) {
                
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        Adx3CustomLayoutAdview view;
        Adx3ImageItemView imageItemView;
        Adx3TextItemView textItemView;

        ConstraintLayout layout_item;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.layout);
            imageItemView = itemView.findViewById(R.id.image);
            textItemView = itemView.findViewById(R.id.textview);
            layout_item = itemView.findViewById(R.id.layout_item);
        }
    }
}
```

* **loadByUser - 参数回调方法，当前回调方法用于用户自身数据填充，用户可进行非广告位内容配置，当前配置为页面文本框内容**
* **loadUrl - 点击回调方法，当前回调方法可区分用户自身控件或广告位，用户可根据当前回调，完成自身操作逻辑，广告位操作不影响用户业务流程**

{% hint style="success" %}
至此，多重广告流程添加完成，用户列表展示逻辑，与多重广告已完成隔离，不会因广告相关展示与配置影响用户自定流程
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.adx3.io/copy-of-cn/jie-kou-wen-dang/android/guang-gao-ge-shi/duo-chong-guang-gao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
