리스트 변경시마다 결제 총액이 계속 변화하는 구조
이벤트 감지 1) 리스트 추가 되었을 때
@Override
public void onFilterFragmentInteractionListener(ProductModel.ODS0 item) {
if (productModels.contains(item)) {
return;
}
item.setCnt(1);
item.setDc_rate(0);
item.setTot_price(item.getSPRICE());
productModels.add(item);
productAdapter.notifyDataSetChanged();
productAdapter.calTotal();
}
이벤트 감지 2) 리스트 제거 되었을 때
String gCode = productModels.get(position).getGCODE();
productAdapter.deleteItem(position);
productAdapter.calTotal();
이벤트 감지 3) 할인율, 수량 변경되었을 때
public class ProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int FOOTER_VIEW = 1;
List<ProductModel.ODS0> productModels;
Context context;
List<Integer> amountList;
List<String> dcList;
private Integer total_sum = 0;
private OnProductListener onProductListener;
public ProductAdapter(List<ProductModel.ODS0> productModels, Context context) {
this.productModels = productModels;
this.context = context;
try {
this.onProductListener = (OnProductListener) context;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.");
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == FOOTER_VIEW) {
return new FooterViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_product_footer, viewGroup, false));
}
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_product, viewGroup, false));
}
@Override
public int getItemViewType(int position) {
if (position == productModels.size()) {
return FOOTER_VIEW;
}
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof FooterViewHolder) {
FooterViewHolder holder = (FooterViewHolder) viewHolder;
holder.product_footer_sum.setText(NumberFormat.getCurrencyInstance().format(total_sum));
calTotal();
} else {
ViewHolder holder = (ViewHolder) viewHolder;
holder.g_code.setText(productModels.get(i).getGCODE());
holder.g_name.setText(productModels.get(i).getGNAME());
holder.d_price.setText(NumberFormat.getCurrencyInstance().format(productModels.get(i).getSPRICE()));
setSpinText(holder.amount, productModels.get(i).getCnt().toString());
setSpinText2(holder.dc_rate, productModels.get(i).getDc_rate().toString());
Integer tot = productModels.get(i).getSPRICE() * productModels.get(i).getCnt() * (100 - productModels.get(i).getDc_rate()) / 100;
holder.tot_price.setText(NumberFormat.getCurrencyInstance().format(tot));
}
}
//결제 총액 계산
public void calTotal() {
total_sum = 0;
for (int k = 0; k < productModels.size(); k++) {
total_sum += productModels.get(k).getTot_price();
}
}
//수량 설정
public void setSpinText(Spinner spin, String text) {
for (int i = 0; i < spin.getAdapter().getCount(); i++) {
if (spin.getAdapter().getItem(i).toString().contains(text)) {
spin.setSelection(i);
break;
}
}
}
//할인율 설정
public void setSpinText2(Spinner spin, String text) {
for (int i = 0; i < spin.getAdapter().getCount(); i++) {
if (spin.getAdapter().getItem(i).toString().contains(text + "]")) {
spin.setSelection(i);
break;
}
}
}
public void deleteItem(int position) {
productModels.remove(position);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (productModels == null) {
return 0;
}
if (productModels.size() == 0) {
return 1;
}
return productModels.size() + 1;
}
public interface OnProductListener {
void onProductListener();
void onDeleteListener(int position);
}
public class FooterViewHolder extends RecyclerView.ViewHolder {
public TextView product_footer_sum;
public Button product_footer_add;
public FooterViewHolder(View itemView) {
super(itemView);
product_footer_sum = itemView.findViewById(R.id.product_footer_sum);
product_footer_add = itemView.findViewById(R.id.product_footer_add);
product_footer_add.setOnClickListener(v -> {
try {
onProductListener.onProductListener();
} catch (ClassCastException exception) {
Log.e("g", exception.getMessage());
}
});
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
public View mView;
public TextView g_code;
public TextView g_name;
public TextView d_price;
public Spinner amount;
public Spinner dc_rate;
public TextView tot_price;
public SwipeRevealLayout swipe_layout;
public LinearLayout bottom_wrapper;
public LinearLayout viewForeground;
public ViewHolder(View view) {
super(view);
mView = itemView;
g_code = itemView.findViewById(R.id.g_code);
g_name = itemView.findViewById(R.id.g_name);
d_price = itemView.findViewById(R.id.d_price);
amount = itemView.findViewById(R.id.amount);
dc_rate = itemView.findViewById(R.id.dc_rate);
tot_price = itemView.findViewById(R.id.tot_price);
viewForeground = itemView.findViewById(R.id.view_foreground);
bottom_wrapper = itemView.findViewById(R.id.bottom_wrapper);
swipe_layout = itemView.findViewById(R.id.swipe_layout);
amountList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
amountList.add(i);
}
ArrayAdapter arrayAdapter = new ArrayAdapter<>(context,
R.layout.spinner_small_item,
amountList);
amount.setAdapter(arrayAdapter);
dcList = new ArrayList<>();
for (int i = 0; i < discountRateModels.size(); i++) {
dcList.add(discountRateModels.get(i).getDISCOUNTRATENM());
}
ArrayAdapter arrayAdapter2 = new ArrayAdapter<>(context,
R.layout.spinner_small_item,
dcList);
dc_rate.setAdapter(arrayAdapter2);
amount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int adapterPosition = getAdapterPosition();
if (adapterPosition == -1)
return;
int cnt = amountList.get(i);
int dc_rate = productModels.get(adapterPosition).getDc_rate();
productModels.get(adapterPosition).setCnt(cnt);
productModels.get(adapterPosition)
.setTot_price((productModels.get(adapterPosition).getSPRICE() * cnt) * (100 - dc_rate) / 100);
notifyItemChanged(adapterPosition);
notifyItemChanged(productModels.size());
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
dc_rate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int adapterPosition = getAdapterPosition();
if (adapterPosition == -1)
return;
int cnt = productModels.get(adapterPosition).getCnt();
int dc_rate = discountRateModels.get(i).getDISCOUNTRATE();
productModels.get(adapterPosition).setDc_rate(dc_rate);
productModels.get(adapterPosition)
.setTot_price(productModels.get(adapterPosition).getSPRICE() * cnt * (100 - dc_rate) / 100);
notifyItemChanged(adapterPosition);
notifyItemChanged(productModels.size());
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
swipe_layout.setDragEdge(SwipeRevealLayout.DRAG_EDGE_RIGHT);
swipe_layout.setSwipeListener(new SwipeRevealLayout.SwipeListener() {
@Override
public void onClosed(SwipeRevealLayout view) {
}
@Override
public void onOpened(SwipeRevealLayout view) {
}
@Override
public void onSlide(SwipeRevealLayout view, float slideOffset) {
}
});
//스와이프 후 삭제 버튼
bottom_wrapper.setOnClickListener(view1 -> {
swipe_layout.postDelayed(() -> swipe_layout.close(true), 50);
try {
onProductListener.onDeleteListener(getAdapterPosition());
} catch (ClassCastException exception) {
Log.e("g", exception.getMessage());
}
});
}
}
}
RecyclerView Row
<com.chauthai.swipereveallayout.SwipeRevealLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_layout"
app:dragEdge="right"
app:mode="same_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_white">
<LinearLayout
android:id="@+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#f00"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/baseline_delete_forever_black_18dp"
android:tint="@color/app_white" />
</LinearLayout>
<LinearLayout
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_white"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:weightSum="10">
<TextView
android:id="@+id/g_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:ellipsize="end"
android:gravity="left|center_vertical"
android:textColor="@color/app_black"
android:textSize="@dimen/list_small_font" />
<TextView
android:id="@+id/g_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/g_code"
android:layout_weight="2.5"
android:gravity="center"
android:textColor="@color/app_black"
android:textSize="10sp" />
<TextView
android:id="@+id/d_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:layout_toRightOf="@id/g_name"
android:layout_weight="1.5"
android:ellipsize="end"
android:gravity="center"
android:paddingLeft="30dp"
android:textColor="@color/app_black"
android:textSize="@dimen/list_small_font" />
<Spinner
android:id="@+id/amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="@drawable/spinner_small_list_bg"
android:gravity="center"
android:textSize="10sp" />
<Spinner
android:id="@+id/dc_rate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:background="@drawable/spinner_small_list_bg"
android:gravity="center"
android:textSize="10sp" />
<TextView
android:id="@+id/tot_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.4"
android:ellipsize="end"
android:gravity="right|center_vertical"
android:textColor="@color/app_black"
android:textSize="@dimen/list_small_font" />
</LinearLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>
fragment_product_footer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_white"
android:orientation="vertical"
android:paddingBottom="@dimen/small_font"
android:paddingTop="@dimen/small_font">
<Button
android:id="@+id/product_footer_add"
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:text="제 품 추 가" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:visibility="invisible" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="right"
android:text="결제 총액"
android:textColor="@color/app_black" />
<TextView
android:paddingRight="22.5dp"
android:id="@+id/product_footer_sum"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:ellipsize="end"
android:gravity="right|center_vertical"
android:textColor="@color/app_black"
android:textSize="@dimen/list_small_font" />
</LinearLayout>
</LinearLayout>
'Android' 카테고리의 다른 글
ScrollView PullToRefreshLayout (0) | 2019.04.19 |
---|---|
Retrofit 통신 (0) | 2019.04.19 |
리스트 선택 : DialogFragment + ViewPager + 리스너연결 (0) | 2019.04.19 |
BottomNavigationView 아이콘 사이즈 수정법 (태블릿 대응) (0) | 2018.10.19 |
Rxjava + MVVM + databinding (0) | 2018.08.21 |