0

あなたの助けが必要です。Facebook Audience Network https://developers.facebook.com/docs/audience-network/android/native-apiを使用して、記事のリスト内に広告ユニット (NativeAd) を追加する方法。私はプログラマーではなく、すべてのコードが必要です。広告は投稿の間にある必要があります(リストビュー)ありがとう

import com.facebook.ads.*;

public class Posts extends Fragment{

ArrayList<Post> mPosts;
private static final String KEY_CONTENT = "PostsFragment:array";
boolean isLoadMore = false;

int mPageCount = 0;
int mCurrectPage = 1;

PullToRefreshListView mListView;
MyAdapter mListViewAdapter;

View mLoadingFooter;
 private Tracker tracker;


@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.tracker = EasyTracker.getInstance(this.getActivity());
    if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
        mPosts = (ArrayList<Post>) savedInstanceState.getSerializable(KEY_CONTENT);
        mCurrectPage = 1;
        mPosts.clear();
        mLoadingFooter.setVisibility(View.VISIBLE);
        isLoadMore = true;
        loadPostsList();
    }
}

@Override
public void onResume() {
    super.onResume();

    this.tracker.set(Fields.SCREEN_NAME, "Article");
    this.tracker.send( MapBuilder.createAppView().build() );
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(KEY_CONTENT, mPosts);
}

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


    View _result = inflater.inflate(R.layout.list, null);

    mLoadingFooter = inflater.inflate(R.layout.loading_footer, null);
    mLoadingFooter.setVisibility(View.GONE);

    if (mPosts == null)
    {
        mPosts = new ArrayList<Post>();
        loadPostsList();
    }


    mListView = (PullToRefreshListView) _result.findViewById(R.id.listView);

    mListViewAdapter = new MyAdapter(getActivity(), mPosts);

    mListView.addFooterView(mLoadingFooter);
    mListView.setAdapter(mListViewAdapter);

    mListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            mCurrectPage = 1;
            mPosts.clear();
            mLoadingFooter.setVisibility(View.VISIBLE);
            isLoadMore = true;
            loadPostsList();
        }
    });

    mListView.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            int lastInScreen = firstVisibleItem + visibleItemCount;
            if (totalItemCount > Constant.MIN_ITEM_COUNT && lastInScreen >= (totalItemCount - Constant.MIN_ITEM_COUNT_HALF) && !isLoadMore && mCurrectPage <= mPageCount)
            {
                mLoadingFooter.setVisibility(View.VISIBLE);
                isLoadMore = true;
                loadPostsList();
            }
        }
    });



    return _result;
}

public void loadPostsList() {
    isLoadMore = true;
    mLoadingFooter.setVisibility(View.VISIBLE);
    new AsynckRequestGet().execute(getActivity(), TYPE_GET_OPERATION.POSTS, CodeRequestManager.codePosts(TempData.getInstance().getToken(), mCurrectPage));
}



private class AsynckRequestGet extends AbstractAsyncRequestGet {


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        isLoadMore = false;

        if(!isDetached()) {
            mLoadingFooter.setVisibility(View.GONE);
            mListView.onRefreshComplete();
        }

        if(result == null) return;

        int[] t = JsonParser.parsePageCountResponse(result);
        mPageCount = t[0];
        mCurrectPage = t[1] +1;

        ArrayList<Post> temp = JsonParser.parsePostsResponse(result);
        if(temp == null || isDetached())
            return;

        mPosts.addAll(temp);
        mListViewAdapter.notifyDataSetChanged();
    }
}


private class MyAdapter extends ArrayAdapter<Post> {

    private LayoutInflater inflater;

    public MyAdapter(Context context, ArrayList<Post> objects) {
        super(context, R.layout.post_item, objects);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.post_item, null);
        final Post post = getItem(position);
        final TextView postLikes = ((TextView) convertView.findViewById(R.id.post_likes));
        final TextView postComments = ((TextView) convertView.findViewById(R.id.post_comments));

        postComments.setText(post.getCountComment() + "");
        postLikes.setText(post.getCountLike() + "");

        if(post.isMyLike()) {
            postLikes.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_my_icon, 0, 0, 0);
        } else {
            postLikes.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_icon, 0, 0, 0);
        }
        postLikes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new AbstractAsyncRequestGet().execute(getActivity(), TYPE_GET_OPERATION.LIKE, CodeRequestManager.codeLike(TempData.getInstance().getToken(), post.getId(), post.isMyLike()? 0 : 1));
                post.setCountLike((post.getCountLike()) + (post.isMyLike()? -1 : 1));
                post.setMyLike(post.isMyLike()? 0 : 1);
                postLikes.setText(post.getCountLike() + "");
                mListViewAdapter.notifyDataSetChanged();
            }
        });

        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                    ((TrainingFragment) getParentFragment().getParentFragment()).showPostDetails(mPosts.get(arg2-1), new DetailListener() {
                    @Override
                    public void onRefresh() {
                        if(!isDetached())
                            mListViewAdapter.notifyDataSetChanged();
                    }
                })
            ;}
        });

        ((TextView) convertView.findViewById(R.id.post_title)).setText(post.getTitle());
        ((TextView) convertView.findViewById(R.id.post_content)).setText(post.getContent());

        return convertView;

    }

}

}

4

1 に答える 1

2

最新の Facebook Android SDK を入手すると、NativeAdSample プロジェクトが更新され、ネイティブ広告をリスト ビューに表示する方法が更新されていることがわかります。サンプルからコピーしたコード:

package com.facebook.samples.NativeAdSample;

import java.util.ArrayList;
import java.util.List;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.NativeAd;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NativeAdListActivity extends ListActivity implements AdListener {

    private ListView listView;
    private ListViewAdapter adapter;
    private NativeAd listNativeAd;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
        listNativeAd.setAdListener(this);
        listNativeAd.loadAd();

        listView = getListView();
        adapter = new ListViewAdapter(getApplicationContext());
        listView.setAdapter(adapter);
    }

    @Override
    public void onAdClicked(Ad ad) {
        Toast.makeText(this, "Ad Clicked", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAdLoaded(Ad ad) {
        adapter.addNativeAd((NativeAd) ad);
    }

    @Override
    public void onError(Ad ad, AdError error) {
        Toast.makeText(this, "Ad failed to load: " +  error.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }

    class ListViewAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private List<Object> list;

        private NativeAd ad;
        private static final int AD_INDEX = 2;

        public ListViewAdapter(Context context) {
            list = new ArrayList<Object>();
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 1; i <= 35; i++) {
                list.add("ListView Item #" + i);
            }
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == AD_INDEX && ad != null) {
                // Return the native ad view
                return (View) list.get(position);
            } else {
                TextView view; // Default item type (non-ad)
                if (convertView != null && convertView instanceof TextView) {
                    view = (TextView) convertView;
                } else {
                    view = (TextView) inflater.inflate(R.layout.list_item, parent, false);
                }
                view.setText((String) list.get(position));
                return view;
            }
        }

        public synchronized void addNativeAd(NativeAd ad) {
            if (ad == null) {
                return;
            }
            if (this.ad != null) {
                // Clean up the old ad before inserting the new one
                this.ad.unregisterView();
                this.list.remove(AD_INDEX);
                this.ad = null;
                this.notifyDataSetChanged();
            }
            this.ad = ad;
            View adView = inflater.inflate(R.layout.ad_unit, null);
            NativeAdSampleActivity.inflateAd(ad, adView, NativeAdListActivity.this);
            list.add(AD_INDEX, adView);
            this.notifyDataSetChanged();
        }
    }
}
于 2014-10-03T23:47:26.070 に答える