如何在每5行之后在recycleview中加载admobs?

bpzcxfmw  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(393)

我正在尝试实现admobs加载广告,每5行,但我有一个困难的时候,你可以从我的截图广告被成功加载,但他们被加载后,每一行。有人能帮我完成吗?我已经试过阅读post-on-stack-overflow,但似乎没有任何帮助。先谢谢你。图片截图
片段

public class SectorPerformanceFragment extends Fragment {
    private RecyclerView newsFeedRecycleView;
    private SectorPerformanceAdapter mostActiveAdapter;

    private RecyclerViewAdopter recyclerViewAdopter;

    public static final int ITEMS_PER_AD = 9;

    private RecyclerView.LayoutManager layoutManager;

    private RequestQueue requestQueue;
    private JSONObject stockDetails;

    public static ArrayList<SectorPerformanceModel> mostActiveArrayList;

    private RequestQueue requestQueues;

    private ImageView backImage;

    private View rootView;
    private ProgressBar loadingProgressBar;

    private TextView sectorNameTv;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.recycle_view_list_ui, container, false);

        sectorNameTv = rootView.findViewById(R.id.sectorNameTv);

        sectorNameTv.setText("Sector Performance");

        loadingProgressBar = rootView.findViewById(R.id.loadingProgressBar);

        backImage = rootView.findViewById(R.id.backImageView);

        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getActivity().getSupportFragmentManager().popBackStack();

//
//                Fragment generalMarketFragment = new BrowseFragment();
//                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
//                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//                fragmentTransaction.replace(R.id.fragment_container, generalMarketFragment);
//                fragmentTransaction.addToBackStack(null);
//                fragmentTransaction.commit();

            }
        });

        requestQueues = Volley.newRequestQueue(getActivity());
        findViewById();
        mostActiveArrayList = new ArrayList<>();
        initRecycleView();

        retreiveBundleInformation();

        return rootView;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void findViewById() {

        newsFeedRecycleView = rootView.findViewById(R.id.recylcle_view_list);

    }

    //set up RecycleVIew/listener to detect taps layout manager tels recycle view how to display the list.. without it, nothing shows up
    public void initRecycleView() {

        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        newsFeedRecycleView.setHasFixedSize(true);
        newsFeedRecycleView.setLayoutManager(layoutManager);

        //mostActiveAdapter = new SectorPerformanceAdapter(getContext(), mostActiveArrayList);

//        recyclerViewAdopter = new RecyclerViewAdopter(getContext(),mostActiveArrayList);
//
//        //newsFeedRecycleView.setAdapter(mostActiveAdapter);
//        newsFeedRecycleView.setAdapter(recyclerViewAdopter);

    }

    //download news data stocknewsapi..
    private void downloadStockList(String api) {

        // Initialize a new JsonArrayRequest instance
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                Request.Method.GET,
                api,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        // Do something with response
                        //mTextView.setText(response.toString());

                        loadingProgressBar.setVisibility(View.INVISIBLE);

                        //Log.i("jsonResponse", response.toString());

                        // Process the JSON
                        // Loop through the array elements
                        for (int i = 0; i < response.length(); i++) {
                            // Get current json object

                            try {
                                stockDetails = response.getJSONObject(i);

                                Log.i("stockDetails", stockDetails.toString());

                                SectorPerformanceModel sectorPerformanceModel2 = new SectorPerformanceModel(stockDetails.getString("sector"),stockDetails.getString("changesPercentage"),1);

                                mostActiveArrayList.add((sectorPerformanceModel2));

                                SectorPerformanceModel sectorPerformanceModel3 = new SectorPerformanceModel(stockDetails.getString("sector"),stockDetails.getString("changesPercentage"),2);

                                mostActiveArrayList.add(mostActiveArrayList.size(),sectorPerformanceModel3);

                                recyclerViewAdopter = new RecyclerViewAdopter(getContext(),mostActiveArrayList);

                                //newsFeedRecycleView.setAdapter(mostActiveAdapter);
                                newsFeedRecycleView.setAdapter(recyclerViewAdopter);

                                //recyclerViewAdopter.notifyDataSetChanged();

//                                mostActiveArrayList.add(sectorPerformanceModel);
//                                mostActiveAdapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Log.i("error", error.toString());

                    }
                }
        );

        // Add JsonArrayRequest to the RequestQueue
        requestQueues.add(jsonArrayRequest);
    }

    public void retreiveBundleInformation() {
        downloadStockList("APIKEY");

    }

    }

适配器

[public class RecyclerViewAdopter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<SectorPerformanceModel> mList;

    public RecyclerViewAdopter(Context mContext, List<SectorPerformanceModel> mList) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        private TextView newsTitle, sourceName, epsEstiatmate;
        private CircleImageView newsThumbImage;

        public MyViewHolder(View view) {
            super(view);

            epsEstiatmate = itemView.findViewById(R.id.formTypeTv);

            newsTitle = itemView.findViewById(R.id.tickerTv);
            sourceName = itemView.findViewById(R.id.dateTv);
            newsThumbImage= itemView.findViewById(R.id.defaultImageColor);

            newsThumbImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Log.i("tapped","TAPPED");
                }
            });
        }
    }

    public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
        public AdView mAdView;
        public ViewHolderAdMob(View view) {
            super(view);

            mAdView = (AdView) view.findViewById(R.id.adView);

            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .build();
            mAdView.loadAd(adRequest);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch(viewType){
            case 1:{
                View v = inflater.inflate(R.layout.trending_stock_row, parent, false);
                viewHolder = new MyViewHolder(v);
                break;
            }
            case 2:{
                View v = inflater.inflate(R.layout.ads_row, parent, false);
                viewHolder = new ViewHolderAdMob(v);
                break;
            }
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        SectorPerformanceModel model= mList.get(holder.getAdapterPosition());

        switch(holder.getItemViewType()){
            case 1:{
                MyViewHolder viewHolder = (MyViewHolder) holder;

                Random r = new Random();
                int red = r.nextInt(255 - 0 + 1) + 0;
                int green = r.nextInt(255 - 0 + 1) + 0;
                int blue = r.nextInt(255 - 0 + 1) + 0;

                GradientDrawable draw = new GradientDrawable();
                draw.setShape(GradientDrawable.OVAL);
                draw.setColor(Color.rgb(red, green, blue));

                viewHolder.newsThumbImage.setBackground(draw);

                viewHolder.newsTitle.setText(model.getSector());
                viewHolder.sourceName.setText(model.getChangesPercentage());

                break;
            }
            case 2:{
                break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getViewType();
    }

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

}][1]
nhaq1z21

nhaq1z211#

看起来您正在为api响应中的每个元素向适配器的数据源添加两个项:一个元素具有viewtype 1,另一个viewtype 2:

SectorPerformanceModel sectorPerformanceModel2 = new SectorPerformanceModel(stockDetails.getString("sector"),stockDetails.getString("changesPercentage"),1);
mostActiveArrayList.add((sectorPerformanceModel2));

SectorPerformanceModel sectorPerformanceModel3 = new SectorPerformanceModel(stockDetails.getString("sector"),stockDetails.getString("changesPercentage"),2);
mostActiveArrayList.add(mostActiveArrayList.size(),sectorPerformanceModel3);

适配器正在将viewtype为2的项目呈现为广告行,因此如果要每5行显示一个广告行,则需要更新 downloadStockList 方法来执行此操作

SectorPerformanceModel sectorPerformanceModel3 = new SectorPerformanceModel(stockDetails.getString("sector"),stockDetails.getString("changesPercentage"),2);
mostActiveArrayList.add(mostActiveArrayList.size(),sectorPerformanceModel3);

仅在添加了4个viewtype为1的项之后。

相关问题