ListView Sample

아래 예제는 더 이상 유효하지 않습니다. NativeAdManager 를 사용하여 구현하십시요.

ListView에서 Native Ad를 구현한 Sample Code 입니다.

mypage_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/main_layout"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@android:color/white"

>

<ListView

android:id="@+id/friend_list"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_below="@android:id/widget_frame"

/>

</LinearLayout>

friend_list_item.xml

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/contents_item"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="8dp"

android:background="#ffffffff">

<TextView

android:id="@+id/list_name_txt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:singleLine="true"

/>

<TextView

android:id="@+id/list_desc_txt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/list_name_txt"

android:singleLine="true"

/>

</RelativeLayout>

native_ad_list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/ad_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="6dp"

android:background="#ffffffff">


<ImageView

android:id="@+id/ad_icon"

android:layout_width="72dp"

android:layout_height="72dp"

android:layout_alignParentTop="true"

android:layout_alignParentLeft="true"

android:padding="4dp"

android:scaleType="fitXY" />

<TextView

android:id="@+id/ad_title"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/ad_icon"

android:layout_alignParentTop="true"

android:layout_marginTop="3dp"

android:layout_marginLeft="8dp"

android:gravity="center_vertical"

android:textColor="#ff020202"

android:textSize="17sp"

/>

<TextView

android:id="@+id/ad_desc"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/ad_icon"

android:layout_below="@id/ad_title"

android:layout_marginLeft="8dp"

android:layout_marginTop="8dp"

android:gravity="center_vertical"

android:textColor="#ff179dce"

android:textSize="13sp"

/>

</RelativeLayout>

MyPageActivity.java

public class MyPageActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.mypage_layout);

ListView list = (ListView)findViewById(R.id.friend_list);

list.setAdapter(new MyPageAdapter(this));

}

}

MyPageAdapter.java

public class MyPageAdapter extends BaseAdapter {

private List<Object> items = new ArrayList<Object>();

private Context context = null;


public MyPageAdapter(Context context){

this.context = context;

initItems();

}


private void initItems(){

items.add(new MyPageItem("제목-1","내용-1"));

items.add(new MyPageItem("제목-2","내용-2"));

items.add(new MyPageItem("제목-3","내용-3"));

items.add(new MyPageItem("제목-4","내용-4"));

items.add(new MyPageItem("제목-5","내용-5"));

items.add(new MyPageItem("제목-6","내용-6"));

items.add(new NativeAdItem(context, NativeAdItem.STYLE_ICON, adListener));

items.add(new MyPageItem("제목-7","내용-7"));

items.add(new MyPageItem("제목-8","내용-8"));

items.add(new MyPageItem("제목-9","내용-9"));

items.add(new MyPageItem("제목-10","내용-10"));

items.add(new MyPageItem("제목-11","내용-11"));

items.add(new MyPageItem("제목-12","내용-12"));

items.add(new MyPageItem("제목-13","내용-13"));

items.add(new MyPageItem("제목-14","내용-14"));

items.add(new NativeAdItem(context, NativeAdItem.STYLE_ICON, adListener));

items.add(new MyPageItem("제목-15","내용-15"));

items.add(new MyPageItem("제목-16","내용-16"));

items.add(new MyPageItem("제목-17","내용-17"));

items.add(new MyPageItem("제목-18","내용-18"));

items.add(new MyPageItem("제목-19","내용-19"));

}


@Override

public int getCount() {

return items.size();

}

@Override

public Object getItem(int position) {

return items.get(position);

}


@Override

public long getItemId(int position) {

return position;

}


@Override

public int getViewTypeCount() {

return 2;

}


@Override

public int getItemViewType (int position) {

Object item = items.get(position);

if (item instanceof NativeAdItem) {

return 0;

}

else {

return 1;

}

}


@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


Object item = items.get(position);


if (item instanceof NativeAdItem) {

NativeAdItem adItem = (NativeAdItem)item;

View adLayout = adItem.getAttachedLayout();

if (adLayout == null) {

// not requested yet

adLayout = inflater.inflate(R.layout.native_ad_list_item, null);

adItem.attachLayout((ViewGroup)adLayout);

adItem.prepareAd();

}


return adLayout;

}

else {


if (convertView == null ){

convertView = inflater.inflate(R.layout.friend_list_item, null);

}


// contents data

MyPageItem pageItem = (MyPageItem)item;


TextView titleView = (TextView)convertView.findViewById(R.id.list_name_txt);

titleView.setText(pageItem.title);


TextView descView = (TextView)convertView.findViewById(R.id.list_desc_txt);

descView.setText(pageItem.description);


return convertView;

}

}


private NativeAdListener adListener = new NativeAdListener() {

@Override

public void onFailure(int errCode) {

Log.d("tnkad", "MyPageAdapter : onFailure " + errCode);

}

@Override

public void onLoad(NativeAdItem adItem) {

Log.d("tnkad", "MyPageAdapter : onLoad");


View view = adItem.getAttachedLayout();


ImageView iconView = (ImageView)view.findViewById(R.id.ad_icon);

iconView.setImageBitmap(adItem.getIconImage());


TextView titleView = (TextView)view.findViewById(R.id.ad_title);

titleView.setText(adItem.getTitle());


TextView descView = (TextView)view.findViewById(R.id.ad_desc);

descView.setText(adItem.getDescription());

}

@Override

public void onClick() {

Log.d("tnkad", "MyPageAdapter : onClick");

}

@Override

public void onShow() {

Log.d("tnkad", "MyPageAdapter : onShow");

}


};


private class MyPageItem {

public String title = null;

public String description = null;


public MyPageItem(String title, String desc){

this.title = title;

this.description = desc;

}

}

}