如何在recyclerview中添加广告?

时间:2020-07-01 05:10:12

标签: java android android-studio kotlin admob

我想在recyclerview中添加横幅广告和插页式广告,我已经看过该教程,并且在应用程序“应用程序停止工作”中出现错误,例如在哪里给出了正确的鱼码?谢谢您的帮助

class DetailLaguActivity : AppCompatActivity() {

var idLagu: String? = null
var modelListLagu: ModelListLagu? = null
var progressDialog: ProgressDialog? = null
var mHandler: Handler? = null
var mRunnable: Runnable? = null
var rotate: RotateAnimation? = null
private lateinit var mInterstitialAd: InterstitialAd



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_detail_lagu)


    //set Transparent Statusbar
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
    }

    if (Build.VERSION.SDK_INT >= 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
        window.statusBarColor = Color.TRANSPARENT
    }

    toolbar_detail.setTitle(null)
    setSupportActionBar(toolbar_detail)
    assert(supportActionBar != null)
    supportActionBar!!.setDisplayHomeAsUpEnabled(true)

    progressDialog = ProgressDialog(this)
    progressDialog!!.setTitle("Mohon Tunggu")
    progressDialog!!.setCancelable(false)
    progressDialog!!.setMessage("Sedang menampilkan data...")

    mHandler = Handler()

    modelListLagu = intent.getSerializableExtra("detailLagu") as ModelListLagu
    if (modelListLagu != null) {
        idLagu = modelListLagu!!.strId

        //Get image source
        Glide.with(this)
                .load(modelListLagu!!.strCoverLagu)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imgCover)

        imgPause.setVisibility(View.GONE)
        imgPlay.setVisibility(View.VISIBLE)

        //Method get data
        detailLagu

        mInterstitialAd = InterstitialAd(this)
        mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
        mInterstitialAd.loadAd(AdRequest.Builder().build())






    }
}

private val detailLagu: Unit
    private get() {
        progressDialog!!.show()
        AndroidNetworking.get(Api.DetailMusic)
                .addPathParameter("id", idLagu)
                .setPriority(Priority.MEDIUM)
                .build()
                .getAsJSONObject(object : JSONObjectRequestListener {
                    override fun onResponse(response: JSONObject) {
                        try {
                            progressDialog!!.dismiss()
                            val playerArray = response.getJSONArray("data")
                            for (i in 0 until playerArray.length()) {

                                val temp = playerArray.getJSONObject(i)

                                val judulMusic = temp.getString("judulmusic")
                                tvTitleMusic!!.text = judulMusic

                                val namaBand = temp.getString("namaband")
                                tvBand!!.text = namaBand

                                val urlMusic = temp.getString("linkmp3")
                                val mediaPlayer = MediaPlayer()

                                imgPlay!!.setOnClickListener {



                                    //set Rotate Cover Album
                                    rotate = RotateAnimation(0F, 360F,
                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                            Animation.RELATIVE_TO_SELF, 0.5f)
                                    rotate!!.duration = 15000
                                    rotate!!.interpolator = LinearInterpolator()
                                    rotate!!.repeatCount = Animation.INFINITE

                                    imgCover!!.startAnimation(rotate)

                                    try {
                                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
                                        mediaPlayer.setDataSource(urlMusic)
                                        mediaPlayer.prepare()
                                        mediaPlayer.start()
                                    } catch (e: IOException) {
                                        e.printStackTrace()
                                    }

                                    imgPlay!!.visibility = View.GONE
                                    imgPause!!.visibility = View.VISIBLE
                                    seekBar!!.maxProgress = mediaPlayer.duration / 1000

                                    mRunnable = Runnable {
                                        val mCurrentPosition = mediaPlayer.currentPosition / 1000
                                        val duration = mediaPlayer.duration
                                        @SuppressLint("DefaultLocale") val time = String.format("%02d min, %02d sec",
                                                TimeUnit.MILLISECONDS.toMinutes(duration.toLong()),
                                                TimeUnit.MILLISECONDS.toSeconds(duration.toLong()) -
                                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration.toLong()))
                                        )
                                        seekBar!!.progress = mCurrentPosition
                                        mRunnable?.let { it1 -> mHandler!!.postDelayed(it1, 1000) }
                                        tvTime!!.text = time
                                    }
                                    mHandler!!.postDelayed(mRunnable!!, 1000)
                                }
                                imgPause!!.setOnClickListener {
                                    rotate!!.cancel()
                                    mediaPlayer.stop()
                                    mediaPlayer.reset()
                                    imgPlay!!.visibility = View.VISIBLE
                                    imgPause!!.visibility = View.GONE



                                }
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                            Toast.makeText(this@DetailLaguActivity,
                                    "Gagal menampilkan data!", Toast.LENGTH_SHORT).show()
                        }
                    }

                    override fun onError(anError: ANError) {
                        progressDialog!!.dismiss()
                        Toast.makeText(this@DetailLaguActivity,
                                "Tidak ada jaringan internet!", Toast.LENGTH_SHORT).show()
                    }
                })
    }

这是我的爱慕者

public class ListLaguAdapter extends RecyclerView.Adapter<ListLaguAdapter.ViewHolder> {

private List<ModelListLagu> items;
private ListLaguAdapter.onSelectData onSelectData;
private Context mContext;

public interface onSelectData {
    void onSelected(ModelListLagu modelListLagu);
}

public ListLaguAdapter(Context context, List<ModelListLagu> items, ListLaguAdapter.onSelectData xSelectData) {
    this.mContext = context;
    this.items = items;
    this.onSelectData = xSelectData;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_music, parent, false);
    return new ViewHolder(v);


}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ModelListLagu data = items.get(position);

    //Get Image
    Glide.with(mContext)
            .load(data.strCoverLagu)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .placeholder(R.drawable.ic_no_image)
            .into(holder.imgCover);

    holder.tvBand.setText(data.strNamaBand);
    holder.tvTitleMusic.setText(data.strJudulMusic);
    holder.cvListMusic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onSelectData.onSelected(data);
        }
    });
}

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

//Class Holder
class ViewHolder extends RecyclerView.ViewHolder {

    public TextView tvBand;
    public TextView tvTitleMusic;
    public CardView cvListMusic;
    public ImageView imgCover;

    public ViewHolder(View itemView) {
        super(itemView);
        cvListMusic = itemView.findViewById(R.id.cvListMusic);
        imgCover = itemView.findViewById(R.id.imgCover);
        tvBand = itemView.findViewById(R.id.tvBand);
        tvTitleMusic = itemView.findViewById(R.id.tvTitleMusic);
    }
}

}

如何使用此代码提供广告?

0 个答案:

没有答案