获取RecyclerView项目点击计数

时间:2019-05-19 10:56:27

标签: android android-recyclerview admob cursor recycler-adapter

我在我的Android应用中使用recyclerview,我想在我的应用中展示广告,我知道如何展示广告,但是我想做的就是获取recyclerview项目数,因此,如果用户点击说,例如我想要两个项目显示admob非页内广告。我搜索了很多,但找不到 这是我的代码

    public class DictionaryAdapter extends RecyclerView.Adapter <DictionaryAdapter.ViewHolder> {
     private Cursor cursor;

    public DictionaryAdapter() {

    }

    public void setCursor(Cursor cursor) {
        this.cursor = cursor;
    }

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

        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View v = inflater.inflate(R.layout.dictionary_item_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(v, context);
        return viewHolder;
    }

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

        cursor.moveToPosition(position);
        holder.word.setText(cursor.getString(1));
    }

    @Override
    public int getItemCount() {
        if (cursor == null) return 0;
        return cursor.getCount();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        Context context;
        TextView word;

        public ViewHolder(@NonNull View itemView, final Context context) {
            super(itemView);

            this.context = context;
            word = itemView.findViewById(R.id.wordText);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int position = getAdapterPosition();
                    cursor.moveToPosition(position);

                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra("ID", cursor.getString(0));
                    intent.putExtra("WORD", cursor.getString(1));
                    intent.putExtra("DEFINITION", cursor.getString(2));
                    context.startActivity(intent);


                }
            });

        }
    }
}

Example image

这是我主要活动的获得方式

 preferences = getSharedPreferences("click_count",MODE_PRIVATE);
        if (preferences.getInt("count", 0) > 2) {
            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            });


            editor = preferences.edit();
            editor.clear();
            editor.apply();
        }

2 个答案:

答案 0 :(得分:1)

您只需将点击计数保存在SharedPreference中。首先将context传递到您的适配器。

private Context mContext;

 public DictionaryAdapter(Context mContext) {
    this.mContext = mContext;
    }

然后将以下方法添加到适配器:

private void addClick(Context mContext) {
    int click = getClicksCount(mContext)+1;
    SharedPreferences preferences = mContext.getSharedPreferences("click_count", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("count", click);
    editor.apply();
}

private int getClicksCount(Context mContext){
    SharedPreferences preferences = mContext.getSharedPreferences("click_count",Context.MODE_PRIVATE);
    return preferences.getInt("count",0);
}

最后,使用它们。您的itemView clickListener将类似于:

itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addClick(mContext);
                    int position = getAdapterPosition();
                    cursor.moveToPosition(position);

                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra("ID", cursor.getString(0));
                    intent.putExtra("WORD", cursor.getString(1));
                    intent.putExtra("DEFINITION", cursor.getString(2));
                    context.startActivity(intent);


                }
            });

您可以通过调用int count = getClicksCount(mContext);方法来获得点击计数。

答案 1 :(得分:0)

首先添加一个界面,该界面定义了回收站视图项目的单击侦听器

  public interface ItemClickListener {
        void onItemClicked();
    }

//update adapter and view holder code as the following

    public class DictionaryAdapter extends RecyclerView.Adapter <DictionaryAdapter.ViewHolder> implements ItemClickListener {
    private Cursor cursor;
    private int clickCount = 0;

    public DictionaryAdapter() {

    }

    public void setCursor(Cursor cursor) {
        this.cursor = cursor;
    }

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

        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View v = inflater.inflate(R.layout.dictionary_item_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(v, context, this);
        return viewHolder;
    }

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

        cursor.moveToPosition(position);
        holder.word.setText(cursor.getString(1));
    }

    @Override
    public int getItemCount() {
        if (cursor == null) return 0;
        return cursor.getCount();
    }

    @Override
    public void onItemClicked() {
        clickCount++;

        //Do whatever you want based on the click count.
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        Context context;
        TextView word;

        public ViewHolder(@NonNull View itemView, final Context context, ItemClickListener itemClickListener) {
            super(itemView);

            this.context = context;
            word = itemView.findViewById(R.id.wordText);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int position = getAdapterPosition();
                    cursor.moveToPosition(position);

                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra("ID", cursor.getString(0));
                    intent.putExtra("WORD", cursor.getString(1));
                    intent.putExtra("DEFINITION", cursor.getString(2));
                    context.startActivity(intent);

                    if(itemClickListener != null) {
                        itemClickListener.onItemClicked();
                    }

                }
            });

        }
    }
}