我想用RecyclerView显示一个数据列表,并在显示3个数据后添加一个AdMob。我遵循了 这个 stackoverflow Q&A 因为它和我的代码几乎一样,但我用的是Retrofit。
但我的结果只是显示测试AdMob行......。它没有显示我的数据的 name
字符串。
这是我的MainActivity。
public class Nilai extends AppCompatActivity implements NilaiView {
RecyclerView recyclerView;
SwipeRefreshLayout swipeRefresh;
NilaiPresenter presenter;
RecyclerViewAdopter adapter;
List<modelNilai> score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nilai);
MobileAds.initialize(this, "ID_APP");
recyclerView = findViewById(R.id.recyclerView);
swipeRefresh = findViewById(R.id.swipe_refresh);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
presenter = new NilaiPresenter(this);
presenter.getNilai();
swipeRefresh.setOnRefreshListener(
() -> presenter.getNilai()
);
}
@Override
public void showLoading() {
swipeRefresh.setRefreshing(true);
}
@Override
public void hideLoading() {
swipeRefresh.setRefreshing(false);
}
@Override
public void onGetNilai(List<modelNilai> scores) {
adapter = new RecyclerViewAdopter(Nilai.this, scores);
adapter.notifyDataSetChanged();
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
score = scores;
for (int i = 0; i <= scores.size(); i += 3)
{
modelNilai myString2 = new modelNilai();
myString.setId(i);
scores.add(i,myString);
}
}
}
我的适配器。
public class RecyclerViewAdopter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Context mContext;
private List<modelNilai> mList;
public RecyclerViewAdopter(Context mContext, List<modelNilai> mList) {
this.mList = mList;
this.mContext = mContext;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.namaSoal);
}
}
public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view) {
super(view);
mAdView = view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("CEE26DC1A5BBF0E603B08A5460483046")
.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.list_nilai_ad, parent, false);
viewHolder = new MyViewHolder(v);
break;
}
case 2:
default:
{
View v = inflater.inflate(R.layout.banner_ad_row, parent, false);
viewHolder = new ViewHolderAdMob(v);
break;
}
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
modelNilai model = mList.get(holder.getAdapterPosition());
switch(holder.getItemViewType()){
case 1:{
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.name.setText(model.getName());
break;
}
case 2:{
break;
}
}
}
@Override
public int getItemViewType(int position)
{
return mList.get(position).getId();
}
@Override
public int getItemCount() {
return mList.size();
}
}
我的主持人:
public class NilaiPresenter {
private NilaiView view;
public NilaiPresenter(NilaiView view) {
this.view = view;
}
void getNilai() {
view.showLoading();
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<modelNilai>> call = apiInterface.getNilai();
call.enqueue(new Callback<List<modelNilai>>() {
@Override
public void onResponse(@NonNull Call<List<modelNilai>> call, @NonNull Response<List<modelNilai>> response) {
view.hideLoading();
if (response.isSuccessful() && response.body() != null) {
view.onGetNilai(response.body());
}
}
@Override
public void onFailure(@NonNull Call<List<modelNilai>> call, @NonNull Throwable t) {
view.hideLoading();
view.onRequestError(t.getLocalizedMessage());
}
});
}
}
我的模型类:
public class modelNilai {
@Expose
@SerializedName("id") private int id_nilai;
@Expose
@SerializedName("name") private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
还有我的视图界面:
public interface NilaiView {
void showLoading();
void hideLoading();
void onGetNilai(List<modelNilai> scores);
}
Result on phone:
应该显示类似3行名称和1行Admob,每4行再重复一次。
也许, getItemViewType()
方法应该是这样的。
@Override
public int getItemViewType(int position)
{
return (position + 1) % 4 == 0 ? 2 : 1;
}
你还必须修改 getItemCount()
方法。因为它应该返回你所有的 mList
行数 加上 的Admob行。
@Override
public int getItemCount() {
return mList.size() + mList.size / 4;
}
除此之外 onBindViewHolder
方法受到影响。
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
modelNilai model = mList.get(holder.getAdapterPosition() - holder.getAdapterPosition() / 4);
if ((position + 1) % 4 != 0) {
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.name.setText(model.getName());
}
}