프로그래밍/Android

[2018년] 안드로이드 인앱 결제 구현 완벽 정리 소스 파일

Lou Park 2018. 10. 13. 01:11

layout_dialog_heartstore.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
 
    <ListView
        android:id="@+id/lv_skus"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>
cs


view_heartstore.xml (개별 리스트 아이템)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">
 
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:src="@drawable/h1" />
 
    <TextView
        android:id="@+id/tv_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@+id/iv_icon"
        android:text="TextView"
        android:textColor="#222" />
 
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:layout_toLeftOf="@+id/imageView13"
        android:text="TextView" />
 
    <ImageView
        android:id="@+id/imageView13"
        android:layout_width="15dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/arrow_right_dark" />
</RelativeLayout>
cs


PurchaseHeartsAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.pl03.pushlike.app.heartstore.adapter;
 
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.anjlab.android.iab.v3.SkuDetails;
import com.pl03.pushlike.MainActivity;
import com.pl03.pushlike.R;
import com.pl03.pushlike.app.heartstore.HeartStoreFragment;
import com.pl03.pushlike.data.InAppPurchaseItems;
 
import java.util.ArrayList;
 
/**
 * Created by gold24park on 2018. 1. 18..
 */
 
public class PurchaseHeartsAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<SkuDetails> products;
    private InAppPurchaseItems items;
    private MainActivity activity;
 
    public PurchaseHeartsAdapter(Activity activity) {
        this.activity = (MainActivity) activity;
        this.context = activity;
        this.products = new ArrayList<>();
        this.items = new InAppPurchaseItems();
    }
    @Override
    public int getCount() {
        return products.size();
    }
 
    @Override
    public Object getItem(int i) {
        return products.get(i);
    }
 
    @Override
    public long getItemId(int i) {
        return i;
    }
 
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final SkuDetails sku = products.get(i);
        final ViewHolder holder;
 
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.view_heartstore, viewGroup, false);
            holder = new ViewHolder();
            holder.ivIcon = view.findViewById(R.id.iv_icon);
            holder.tvLabel = view.findViewById(R.id.tv_label);
            holder.tvPrice = view.findViewById(R.id.tv_price);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
 
        holder.ivIcon.setImageDrawable(context.getResources().getDrawable(items.getDrawable(sku.productId)));
        holder.tvLabel.setText(sku.title.replaceAll("\\(.*\\)"""));
        holder.tvPrice.setText(sku.priceText);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.purchaseProduct(sku.productId);
            }
        });
 
        return view;
    }
 
    public void update(ArrayList<SkuDetails> products) {
        if (products != null) {
            this.products = products;
            notifyDataSetChanged();
        }
    }
 
    class ViewHolder {
        public ImageView ivIcon;
        public TextView tvLabel, tvPrice;
    }
}
 
cs


HeartStoreFragment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.pl03.pushlike.app.heartstore;
 
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
 
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.pl03.pushlike.MainActivity;
import com.pl03.pushlike.R;
import com.pl03.pushlike.app.heartstore.adapter.HeartsLogAdapter;
import com.pl03.pushlike.app.heartstore.adapter.PurchaseHeartsAdapter;
import com.pl03.pushlike.data.HeartsLog;
 
import java.util.ArrayList;
 
/**
 * Created by gold24park on 2018. 1. 7..
 */
 
public class HeartStoreFragment extends Fragment implements HeartStoreContract.View {
    private HeartStorePresenter presenter;
    private HeartsLogAdapter adapter;
    private View view, headerView, footerView;
    private ListView lvLog;
    private Spinner spFilter;
    private RelativeLayout btnPurchaseHeart;
 
    private MaterialDialog purchaseDialog;
 
    private ArrayAdapter<String> filterAdapter;
    private int page = 0;
    // 결제 관련 변수
    private PurchaseHeartsAdapter skusAdapter;
 
    @Override
    public View getView() {
        return view;
    }
 
    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        view = layoutInflater.inflate(R.layout.fragment_heartstore, viewGroup, false);
        headerView = layoutInflater.inflate(R.layout.view_header_log, null);
        footerView = layoutInflater.inflate(R.layout.view_footer_log, null);
 
        init();
        return view;
    }
 
    private void init() {
        presenter = new HeartStorePresenter(getContext());
        presenter.setView(this);
        presenter.loadHeartsLog(page);
 
        adapter = new HeartsLogAdapter(getContext());
        lvLog = view.findViewById(R.id.lv_log);
        lvLog.setAdapter(adapter);
        lvLog.addHeaderView(headerView);
        lvLog.addFooterView(footerView);
 
        // 기본 필터 설정, 전체
        ArrayList<String> filters = new ArrayList<>();
        filters.add(getString(R.string.all));
 
        spFilter = view.findViewById(R.id.sp_filter);
        filterAdapter = new ArrayAdapter<String>(getContext(),
                R.layout.item_filter, filters);
        filterAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFilter.setAdapter(filterAdapter);
 
        spFilter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                adapter.filter(String.valueOf(adapterView.getItemAtPosition(i)));
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
 
            }
        });
        // 더보기 버튼
        Button btnMore = footerView.findViewById(R.id.btn_more);
        btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.loadHeartsLog(++page);
            }
        });
        // 결제 아이템 다이얼로그
        skusAdapter = new PurchaseHeartsAdapter(getActivity());
        View purchaseView = getLayoutInflater().inflate(R.layout.layout_dialog_heartstore, null);
        ListView lvSkus = purchaseView.findViewById(R.id.lv_skus);
        lvSkus.setAdapter(skusAdapter);
 
        purchaseDialog = new MaterialDialog.Builder(getContext())
                .customView(purchaseView, false)
                .negativeText(R.string.cancel)
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .build();
 
        btnPurchaseHeart = view.findViewById(R.id.btn_purchase_heart);
        btnPurchaseHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                purchaseDialog.show();
            }
        });
 
        skusAdapter.update(MainActivity.products);
    }
 
    @Override
    public void onLoadHeartsLog(ArrayList<HeartsLog> logs) {
        if (logs.size() > 0)
            adapter.addLogs(logs);
        else
            Toast.makeText(getContext(), R.string.end_of_page, Toast.LENGTH_SHORT).show();
    }
 
    @Override
    public void onLoadFilter(ArrayList<String> filters) {
        filterAdapter.addAll(filters);
    }
}
 
cs


관련 포스트

안드로이드 인앱 결제 구현 완벽 정리 (http://jizard.tistory.com/137)