Facebook Audience のネイティブ広告を recyclerView 内に実装したいと考えています。広告は、リサイクラー ビューの特定の位置に配置する必要があります。たとえば、2 位から開始し、リサイクラー ビューで 5 番目ごとに配置します。Recyclerview を実装する Adapterclass と、アイテムをユーザーに表示する役割を担うフラグメントがあります。
AdapterClass
public class AdapterBoxOffice extends RecyclerView.Adapter<AdapterBoxOffice.ViewHolderBoxOffice> {
private LayoutInflater layoutInflater;
private ArrayList<Movie> mlistMovies = new ArrayList<>();
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private DateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd");
private int previousPosition=0;
private LinearLayout nativeAdContainer;
private LinearLayout adView;
private AdChoicesView adChoicesView;
private NativeAd nativeAd;
AdapterView.OnItemClickListener mOnItemClickListener;
private ArrayList<Movie> filteredProductResults = new ArrayList<Movie>();
public AdapterBoxOffice(Context context) {
layoutInflater = LayoutInflater.from(context);
volleySingleton = VolleySingleton.getsInstance();
imageLoader = volleySingleton.getImageLoader();
}
public void setMovieList(ArrayList<Movie> listMovies) {
this.mlistMovies = listMovies;
notifyDataSetChanged();
}
public void setFilteredMovies(ArrayList<Movie> filteredProductResults){
this.mlistMovies=filteredProductResults;
}
@Override
public ViewHolderBoxOffice onCreateViewHolder(ViewGroup parent, int viewType) {
View view=null;
if(viewType==10){
view=layoutInflater.inflate(R.layout.custom_ads_facebook, nativeAdContainer, false);
nativeAdContainer.addView(adView);
} else {
view = layoutInflater.inflate(R.layout.custom_movie_box_office, parent, false);
}
ViewHolderBoxOffice viewHolder = new ViewHolderBoxOffice(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolderBoxOffice holder, int position) {
if (position==10){
// Setting the Text.
ImageView nativeAdIcon = null;
holder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
holder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
holder.nativeAdTitle.setText(nativeAd.getAdTitle());
holder.nativeAdBody.setText(nativeAd.getAdBody());
/* nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
nativeAdTitle.setText(nativeAd.getAdTitle())
nativeAdBody.setText(nativeAd.getAdBody());*/
// Downloading and setting the ad icon.
NativeAd.Image adIcon = nativeAd.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);
// Download and setting the cover image.
NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
holder.nativeAdMedia.setNativeAd(nativeAd);
/*nativeAdMedia.setNativeAd(nativeAd);*/
// Add adChoices icon
if (adChoicesView == null) {
adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
adView.addView(adChoicesView, 0);
}
nativeAd.registerViewForInteraction(adView);
}else {
Movie currentMovie = mlistMovies.get(position);
holder.movieTitle.setText(currentMovie.getTitle());
Date movieReleaseDate = currentMovie.getReleaseDateTheater();
if (movieReleaseDate != null) {
String formmattedDate = dateFormat.format(movieReleaseDate);
holder.movieReleaseDate.setText(formmattedDate);
} else {
holder.movieReleaseDate.setText(Constants.NA);
}
int audienceScore=currentMovie.getAudienceScore();
if (audienceScore==-1){
holder.movieAudienceScore.setRating(0.0F);
holder.movieAudienceScore.setAlpha(0.5F);
}
else
{
holder.movieAudienceScore.setRating(currentMovie.getAudienceScore() / 20.0F);
holder.movieAudienceScore.setAlpha(1.0F);
}
String urlThumbnail = currentMovie.getUrlThumbnail();
loadImages(urlThumbnail, holder);
}
if(position>previousPosition)
{
comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
}else {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
}
previousPosition=position;
}
private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
if (urlThumbnail != null) {
imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.movieThumbnail.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
@Override
public int getItemCount() {
return mlistMovies.size();
}
class ViewHolderBoxOffice extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView movieThumbnail;
private TextView movieTitle;
private TextView movieReleaseDate;
private RatingBar movieAudienceScore;
private ImageView nativeAdIcon;
private TextView nativeAdTitle;
private TextView nativeAdBody;
private MediaView nativeAdMedia;
private TextView nativeAdSocialContext;
private Button nativeAdCallToAction;
public ViewHolderBoxOffice(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
movieThumbnail = (ImageView) itemView.findViewById(R.id.movieThumbnail);
movieTitle = (TextView) itemView.findViewById(R.id.movieTitle);
movieReleaseDate = (TextView) itemView.findViewById(R.id.movieReleaseDate);
movieAudienceScore = (RatingBar) itemView.findViewById(R.id.movieAudienceScore);
// Create native UI using the ad metadata.
nativeAdIcon = (ImageView)itemView.findViewById(R.id.native_ad_icon);
nativeAdTitle = (TextView)itemView.findViewById(R.id.native_ad_title);
nativeAdBody = (TextView)itemView.findViewById(R.id.native_ad_body);
nativeAdMedia = (MediaView)itemView.findViewById(R.id.native_ad_media);
nativeAdSocialContext = (TextView)itemView.findViewById(R.id.native_ad_social_context);
nativeAdCallToAction = (Button)itemView.findViewById(R.id.native_ad_call_to_action);
}
FragmentClass
public class FragmentBoxOffice extends Fragment implements SortListener, BoxOfficeLoadedListener, AdListener, NativeAdsManager.Listener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static final String STATE_MOVIES ="state_movies" ;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private ArrayList<Movie> mlistMovies = new ArrayList<>();
private DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
private RecyclerView listMovieHits;
private AdapterBoxOffice adapterBoxOffice;
private TextView textVolleyError;
private MovieSorter movieSorter;
private NativeAd nativeAd;
private LinearLayout nativeAdContainer;
private LinearLayout adView;
private AdChoicesView adChoicesView;
private NativeAdsManager manager;
/*private SwipeRefreshLayout mswipeRefreshLayout;*/
public FragmentBoxOffice() {
movieSorter=new MovieSorter();
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentBoxOffice.
*/
// TODO: Rename and change types and number of parameters
public static FragmentBoxOffice newInstance(String param1, String param2) {
FragmentBoxOffice fragment = new FragmentBoxOffice();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public void onMoviesLoaded(){
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_box_office, container, false);
setHasOptionsMenu(true);
textVolleyError = (TextView) layout.findViewById(R.id.textVolleyError);
/* mswipeRefreshLayout=(SwipeRefreshLayout) layout.findViewById(R.id.swipeMoviesHits);
mswipeRefreshLayout.setOnRefreshListener(this);
*/
nativeAd=new NativeAd(getActivity(),"753251851443530_753848588050523");
nativeAd.setAdListener(this);
AdSettings.addTestDevice("HASHED ID");
nativeAd.loadAd();
manager=new NativeAdsManager(getActivity(),"753251851443530_753848588050523", 5);
manager.setListener(this);
manager.loadAds();
listMovieHits = (RecyclerView) layout.findViewById(R.id.listMovieHits);
listMovieHits.setLayoutManager(new LinearLayoutManager(getActivity()));
adapterBoxOffice = new AdapterBoxOffice(getActivity());
listMovieHits.setAdapter(adapterBoxOffice);
if (savedInstanceState!=null)
{
mlistMovies=savedInstanceState.getParcelableArrayList(STATE_MOVIES);
}else {
mlistMovies=MyApplication.getWritableDatabase().readMovies(MovieDatabase.BOX_OFFICE);
if (mlistMovies.isEmpty()){
L.t(getActivity(),"executing task from the fragment");
new TaskLoadMoviesBoxOffice(this).execute();
}else{
}
}
adapterBoxOffice.setMovieList(mlistMovies);
adapterBoxOffice.SetOnclickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
container.getContext().startActivity(new Intent(container.getContext(), Details_Activity.class));
}
});
return layout;
}
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onAdsLoaded() {
System.out.println("Loaded in fragment");
nativeAd = manager.nextNativeAd();
nativeAd.setAdListener(this);
adapterBoxOffice.notifyDataSetChanged();
}
@Override
public void onAdError(AdError adError) {
}
アプリを実行するたびに、これが LogCat から得られるものです。
01-28 17:38:27.508 14778-14778/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
Process: comeagain.materialdesign, PID: 14778
java.lang.NullPointerException
at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:99)
at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:47)
私は今、これに苦労しています。親切に助けてください..