如何在 activity 中传递值 Recycler onClicklistener?
How to pass value Recycler onClicklistener in an activity?
我有这个 onClickListener,我想将值 total 传递到它的 Listener 之外
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener(){
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
total = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
}
});
我该怎么做?
我想知道如何从两个 onClicklistener 中获取总值并将它们相加到 activity。
更新 1:
这是我的项目代码
我已经有一个侦听器来获取项目的位置
recycler 视图,但如何实现传值给它?
订单活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
getSupportActionBar().setTitle("Order");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initDatePicker();
dateButton = findViewById(R.id.btn_date);
timeButton = findViewById(R.id.btn_time);
fData = FirebaseDatabase.getInstance();
mOrderDatabase = fData.getReference("order");
mExtraDatabase = fData.getReference("extraService");
orderCategoryModels = new ArrayList<>();
extraServiceModels = new ArrayList<>();
recyclerViewOrder = findViewById(R.id.recycles_order);
recyclerViewExtraService = findViewById(R.id.recycles_extra);
txt_uang_service = findViewById(R.id.txt_uang_service);
txt_uang_extra = findViewById(R.id.txt_uang_extra);
txt_total_uang = findViewById(R.id.txt_total_uang);
arrow_icon = findViewById(R.id.img_btn_next);
recyclerViewOrder.setLayoutManager(new GridLayoutManager(getApplicationContext(), 3));
recyclerViewExtraService.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
// recyclerViewOrder.setAdapter(serviceOrderAdapter);
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener(){
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
total = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
}
});
extraServiceAdapter = new ExtraServiceAdapter(getApplicationContext(), extraServiceModels, new ExtraServiceAdapter.RecyclerItemClickListener() {
@Override
public void onClickListener(ExtraServiceModel extraServiceModel, int position) {
changeSelectedExtra(position);
}
});
valueEventListener = mOrderDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
orderCategoryModels.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
OrderCategoryModel orderCategoryModel = dataSnapshot.getValue(OrderCategoryModel.class);
orderCategoryModel.setmKey(dataSnapshot.getKey());
currentIndex = 0;
final String s = getIntent().getExtras().getString("catName");
if (s.equals(orderCategoryModel.getCatName())) {
orderCategoryModels.add(orderCategoryModel);
}
}
// serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(),orderCategoryModels);
serviceOrderAdapter.setSelectedPosition(-1);
recyclerViewOrder.setAdapter(serviceOrderAdapter);
serviceOrderAdapter.setCatModels(ServiceOrderAdapter.getInstance().getAllOrder());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(TAG, "helooooooooooooo" + error.getMessage());
}
});
valueEventListener = mExtraDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
extraServiceModels.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ExtraServiceModel extraServiceModel = dataSnapshot.getValue(ExtraServiceModel.class);
extraServiceModel.setmKey(dataSnapshot.getKey());
currentIndex = 0;
final String a = getIntent().getExtras().getString("catName");
if (a.equals(extraServiceModel.getCatName())) {
extraServiceModels.add(extraServiceModel);
}
}
extraServiceAdapter.setSelectedPosition(-1);
recyclerViewExtraService.setAdapter(extraServiceAdapter);
extraServiceAdapter.setExtraModels(ExtraServiceAdapter.getInstance().getAllExtra());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(TAG, "helooooooooooooo" + error.getMessage());
}
});
arrow_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Bundle bundle = new Bundle();
// bundle.putString("extraCash" , extraServiceModels.get(position).getExtraPrice());
// Intent intent = new Intent(getApplicationContext(), ContactDetailActivity.class);
// startActivity(intent);
}
});
}
private void initDatePicker() {
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
String date = makeDateString(dayOfMonth, month, year);
dateButton.setText(date);
// dateButton.setText(getTodaysDate());
}
};
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int style = AlertDialog.THEME_HOLO_LIGHT;
datePickerDialog = new DatePickerDialog(this, style, dateSetListener, year, month, day);
// datePickerDialog.getDatePicker().setMinDate(Instant.now().toEpochMilli());
}
private String getTodaysDate() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
month = month + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return makeDateString(day, month, year);
}
private String makeDateString(int dayOfMonth, int month, int year) {
return getMonthFormat(month) + " " + dayOfMonth + " " + year;
}
private String getMonthFormat(int month) {
if (month == 1) {
return "JAN";
}
else if (month == 2) {
return "FEB";
}
else if (month == 3) {
return "MAR";
}
else if (month == 4) {
return "APR";
}
else if (month == 5) {
return "MAY";
}
else if (month == 6) {
return "JUN";
}
else if (month == 7) {
return "JUL";
}
else if (month == 8) {
return "AUG";
}
else if (month == 9) {
return "SEP";
}
else if (month == 10) {
return "OCT";
}
else if (month == 11) {
return "NOV";
}
else if (month == 12) {
return "DEX";
}
return "Jan";
}
public void changePrice(int total){
serviceOrderAdapter.notifyItemChanged(serviceOrderAdapter.getSelectedPosition());
total = new OrderCategoryModel().getOrderPrice();
}
public void changeSelectedService(int index) {
serviceOrderAdapter.notifyItemChanged(serviceOrderAdapter.getSelectedPosition());
currentIndex = index;
serviceOrderAdapter.setSelectedPosition(currentIndex);
serviceOrderAdapter.notifyItemChanged(currentIndex);
}
public void changeSelectedExtra(int index) {
extraServiceAdapter.notifyItemChanged(extraServiceAdapter.getSelectedPosition());
currentIndex = index;
extraServiceAdapter.setSelectedPosition(currentIndex);
extraServiceAdapter.notifyItemChanged(currentIndex);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void openDatePicker(View view) {
datePickerDialog.show();
}
public void openTimerPicker(View view) {
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
minute = minute;
timeButton.setText(String.format(Locale.getDefault(), "%02d:%02d", hourOfDay, minute));
}
};
TimePickerDialog timePickerDialog = new TimePickerDialog(this, onTimeSetListener, hour, minute, true);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
}
ServitOrderAdapter.java
public class ServiceOrderAdapter extends RecyclerView.Adapter<ServiceOrderAdapter.ServiceOrderViewHolder> {
public ServiceOrderAdapter(Context mContext, ArrayList<OrderCategoryModel> orderCategoryModels, RecyclerItemClickListener listener) {
this.mContext = mContext;
this.orderCategoryModels = orderCategoryModels;
this.listener = listener;
}
private ServiceOrderAdapter(){
if (null == orderCategoryModels){
orderCategoryModels = new ArrayList<>();
}
}
@NonNull
@Override
public ServiceOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_card, parent, false);
return new ServiceOrderAdapter.ServiceOrderViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ServiceOrderViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final OrderCategoryModel orderCategoryModel = orderCategoryModels.get(position);
holder.txt_service_name.setText(orderCategoryModel.getServiceName());
if (orderCategoryModel != null){
if (selectedPosition == position){
holder.parent.setCardBackgroundColor(mContext.getResources().getColor(R.color.black));
}else {
holder.parent.setCardBackgroundColor(mContext.getResources().getColor(R.color.white));
}
}
holder.bind(orderCategoryModel, listener);
}
@Override
public int getItemCount() {
return orderCategoryModels.size();
}
public void setCatModels(ArrayList<OrderCategoryModel> orderCategoryModels){
this.orderCategoryModels = orderCategoryModels;
notifyDataSetChanged();
}
public static ServiceOrderAdapter getInstance(){
if (null == instance){
instance = new ServiceOrderAdapter();
}
return instance;
}
public static ArrayList<OrderCategoryModel> getAllOrder(){
return orderCategoryModels;
}
public class ServiceOrderViewHolder extends RecyclerView.ViewHolder {
private CardView parent;
private TextView txt_service_name, txt_price_service;
public ServiceOrderViewHolder(@NonNull View itemView) {
super(itemView);
parent = itemView.findViewById(R.id.parent);
txt_service_name = itemView.findViewById(R.id.txt_service_name);
txt_price_service = itemView.findViewById(R.id.price_service);
}
public void bind(final OrderCategoryModel orderCategoryModel, final RecyclerItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClickListener(orderCategoryModel, getAdapterPosition());
}
});
}
}
public interface RecyclerItemClickListener {
void onClickListener(OrderCategoryModel orderCategoryModel, int position);
}
public int getSelectedPosition() {
return selectedPosition;
}
public void setSelectedPosition(int selectedPosition) {
this.selectedPosition = selectedPosition;
}
}
抱歉阅读时间过长
你可以使用 Interfaces
。
使用方法 onPriceChanged(price: Int)
创建一个 interface
interface PriceChangeListener {
void onPriceChanged(int total);
}
在activity
或fragment
中,包含recyclerView
,实现PriceChangeListener
接口,override
onPriceChanged
方法并使用total
那里的价值。
public class MyActivity extends Activity implements PriceChangedListener {
@Override
public void onPriceChanged(int total) {
}
}
在 recyclerView's
适配器内部,更改其构造函数以接受 PriceChangedListener
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private PriceChangeListener priceChangedListener;
public MyAdapter(PriceChangeListener priceChangedListener) {
this.priceChangeListener = priceChangeListener;
}
}
在onClickListener()
内调用onPriceChanged()
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener(){
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
total = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
priceChangeListener.onPriceChanged(total); // send price to the activity
}
});
只需稍作改动,我的代码就可以满足我的要求。
extraServiceAdapter = new ExtraServiceAdapter(getApplicationContext(), extraServiceModels, new ExtraServiceAdapter.RecyclerItemClickListener() {
@Override
public void onClickListener(ExtraServiceModel extraServiceModel, int position) {
currentExtraPrice = extraServiceModel.getExtraPrice();
changeSelectedExtra(position);
onPriceChanged(currentExtraPrice + currentServicePrice);
}
});
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener() {
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
currentServicePrice = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
onPriceChanged(currentExtraPrice + currentServicePrice);
}
});
public void onPriceChanged(int total) {
txt_total_uang.setText(String.valueOf(total));
}
我有这个 onClickListener,我想将值 total 传递到它的 Listener 之外
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener(){
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
total = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
}
});
我该怎么做?
我想知道如何从两个 onClicklistener 中获取总值并将它们相加到 activity。
更新 1:
这是我的项目代码
我已经有一个侦听器来获取项目的位置 recycler 视图,但如何实现传值给它?
订单活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
getSupportActionBar().setTitle("Order");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initDatePicker();
dateButton = findViewById(R.id.btn_date);
timeButton = findViewById(R.id.btn_time);
fData = FirebaseDatabase.getInstance();
mOrderDatabase = fData.getReference("order");
mExtraDatabase = fData.getReference("extraService");
orderCategoryModels = new ArrayList<>();
extraServiceModels = new ArrayList<>();
recyclerViewOrder = findViewById(R.id.recycles_order);
recyclerViewExtraService = findViewById(R.id.recycles_extra);
txt_uang_service = findViewById(R.id.txt_uang_service);
txt_uang_extra = findViewById(R.id.txt_uang_extra);
txt_total_uang = findViewById(R.id.txt_total_uang);
arrow_icon = findViewById(R.id.img_btn_next);
recyclerViewOrder.setLayoutManager(new GridLayoutManager(getApplicationContext(), 3));
recyclerViewExtraService.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
// recyclerViewOrder.setAdapter(serviceOrderAdapter);
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener(){
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
total = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
}
});
extraServiceAdapter = new ExtraServiceAdapter(getApplicationContext(), extraServiceModels, new ExtraServiceAdapter.RecyclerItemClickListener() {
@Override
public void onClickListener(ExtraServiceModel extraServiceModel, int position) {
changeSelectedExtra(position);
}
});
valueEventListener = mOrderDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
orderCategoryModels.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
OrderCategoryModel orderCategoryModel = dataSnapshot.getValue(OrderCategoryModel.class);
orderCategoryModel.setmKey(dataSnapshot.getKey());
currentIndex = 0;
final String s = getIntent().getExtras().getString("catName");
if (s.equals(orderCategoryModel.getCatName())) {
orderCategoryModels.add(orderCategoryModel);
}
}
// serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(),orderCategoryModels);
serviceOrderAdapter.setSelectedPosition(-1);
recyclerViewOrder.setAdapter(serviceOrderAdapter);
serviceOrderAdapter.setCatModels(ServiceOrderAdapter.getInstance().getAllOrder());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(TAG, "helooooooooooooo" + error.getMessage());
}
});
valueEventListener = mExtraDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
extraServiceModels.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ExtraServiceModel extraServiceModel = dataSnapshot.getValue(ExtraServiceModel.class);
extraServiceModel.setmKey(dataSnapshot.getKey());
currentIndex = 0;
final String a = getIntent().getExtras().getString("catName");
if (a.equals(extraServiceModel.getCatName())) {
extraServiceModels.add(extraServiceModel);
}
}
extraServiceAdapter.setSelectedPosition(-1);
recyclerViewExtraService.setAdapter(extraServiceAdapter);
extraServiceAdapter.setExtraModels(ExtraServiceAdapter.getInstance().getAllExtra());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(TAG, "helooooooooooooo" + error.getMessage());
}
});
arrow_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Bundle bundle = new Bundle();
// bundle.putString("extraCash" , extraServiceModels.get(position).getExtraPrice());
// Intent intent = new Intent(getApplicationContext(), ContactDetailActivity.class);
// startActivity(intent);
}
});
}
private void initDatePicker() {
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
String date = makeDateString(dayOfMonth, month, year);
dateButton.setText(date);
// dateButton.setText(getTodaysDate());
}
};
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int style = AlertDialog.THEME_HOLO_LIGHT;
datePickerDialog = new DatePickerDialog(this, style, dateSetListener, year, month, day);
// datePickerDialog.getDatePicker().setMinDate(Instant.now().toEpochMilli());
}
private String getTodaysDate() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
month = month + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return makeDateString(day, month, year);
}
private String makeDateString(int dayOfMonth, int month, int year) {
return getMonthFormat(month) + " " + dayOfMonth + " " + year;
}
private String getMonthFormat(int month) {
if (month == 1) {
return "JAN";
}
else if (month == 2) {
return "FEB";
}
else if (month == 3) {
return "MAR";
}
else if (month == 4) {
return "APR";
}
else if (month == 5) {
return "MAY";
}
else if (month == 6) {
return "JUN";
}
else if (month == 7) {
return "JUL";
}
else if (month == 8) {
return "AUG";
}
else if (month == 9) {
return "SEP";
}
else if (month == 10) {
return "OCT";
}
else if (month == 11) {
return "NOV";
}
else if (month == 12) {
return "DEX";
}
return "Jan";
}
public void changePrice(int total){
serviceOrderAdapter.notifyItemChanged(serviceOrderAdapter.getSelectedPosition());
total = new OrderCategoryModel().getOrderPrice();
}
public void changeSelectedService(int index) {
serviceOrderAdapter.notifyItemChanged(serviceOrderAdapter.getSelectedPosition());
currentIndex = index;
serviceOrderAdapter.setSelectedPosition(currentIndex);
serviceOrderAdapter.notifyItemChanged(currentIndex);
}
public void changeSelectedExtra(int index) {
extraServiceAdapter.notifyItemChanged(extraServiceAdapter.getSelectedPosition());
currentIndex = index;
extraServiceAdapter.setSelectedPosition(currentIndex);
extraServiceAdapter.notifyItemChanged(currentIndex);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void openDatePicker(View view) {
datePickerDialog.show();
}
public void openTimerPicker(View view) {
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
minute = minute;
timeButton.setText(String.format(Locale.getDefault(), "%02d:%02d", hourOfDay, minute));
}
};
TimePickerDialog timePickerDialog = new TimePickerDialog(this, onTimeSetListener, hour, minute, true);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
}
ServitOrderAdapter.java
public class ServiceOrderAdapter extends RecyclerView.Adapter<ServiceOrderAdapter.ServiceOrderViewHolder> {
public ServiceOrderAdapter(Context mContext, ArrayList<OrderCategoryModel> orderCategoryModels, RecyclerItemClickListener listener) {
this.mContext = mContext;
this.orderCategoryModels = orderCategoryModels;
this.listener = listener;
}
private ServiceOrderAdapter(){
if (null == orderCategoryModels){
orderCategoryModels = new ArrayList<>();
}
}
@NonNull
@Override
public ServiceOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_card, parent, false);
return new ServiceOrderAdapter.ServiceOrderViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ServiceOrderViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final OrderCategoryModel orderCategoryModel = orderCategoryModels.get(position);
holder.txt_service_name.setText(orderCategoryModel.getServiceName());
if (orderCategoryModel != null){
if (selectedPosition == position){
holder.parent.setCardBackgroundColor(mContext.getResources().getColor(R.color.black));
}else {
holder.parent.setCardBackgroundColor(mContext.getResources().getColor(R.color.white));
}
}
holder.bind(orderCategoryModel, listener);
}
@Override
public int getItemCount() {
return orderCategoryModels.size();
}
public void setCatModels(ArrayList<OrderCategoryModel> orderCategoryModels){
this.orderCategoryModels = orderCategoryModels;
notifyDataSetChanged();
}
public static ServiceOrderAdapter getInstance(){
if (null == instance){
instance = new ServiceOrderAdapter();
}
return instance;
}
public static ArrayList<OrderCategoryModel> getAllOrder(){
return orderCategoryModels;
}
public class ServiceOrderViewHolder extends RecyclerView.ViewHolder {
private CardView parent;
private TextView txt_service_name, txt_price_service;
public ServiceOrderViewHolder(@NonNull View itemView) {
super(itemView);
parent = itemView.findViewById(R.id.parent);
txt_service_name = itemView.findViewById(R.id.txt_service_name);
txt_price_service = itemView.findViewById(R.id.price_service);
}
public void bind(final OrderCategoryModel orderCategoryModel, final RecyclerItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClickListener(orderCategoryModel, getAdapterPosition());
}
});
}
}
public interface RecyclerItemClickListener {
void onClickListener(OrderCategoryModel orderCategoryModel, int position);
}
public int getSelectedPosition() {
return selectedPosition;
}
public void setSelectedPosition(int selectedPosition) {
this.selectedPosition = selectedPosition;
}
}
抱歉阅读时间过长
你可以使用 Interfaces
。
使用方法 onPriceChanged(price: Int)
interface
interface PriceChangeListener {
void onPriceChanged(int total);
}
在activity
或fragment
中,包含recyclerView
,实现PriceChangeListener
接口,override
onPriceChanged
方法并使用total
那里的价值。
public class MyActivity extends Activity implements PriceChangedListener {
@Override
public void onPriceChanged(int total) {
}
}
在 recyclerView's
适配器内部,更改其构造函数以接受 PriceChangedListener
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private PriceChangeListener priceChangedListener;
public MyAdapter(PriceChangeListener priceChangedListener) {
this.priceChangeListener = priceChangeListener;
}
}
在onClickListener()
onPriceChanged()
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener(){
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
total = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
priceChangeListener.onPriceChanged(total); // send price to the activity
}
});
只需稍作改动,我的代码就可以满足我的要求。
extraServiceAdapter = new ExtraServiceAdapter(getApplicationContext(), extraServiceModels, new ExtraServiceAdapter.RecyclerItemClickListener() {
@Override
public void onClickListener(ExtraServiceModel extraServiceModel, int position) {
currentExtraPrice = extraServiceModel.getExtraPrice();
changeSelectedExtra(position);
onPriceChanged(currentExtraPrice + currentServicePrice);
}
});
serviceOrderAdapter = new ServiceOrderAdapter(getApplicationContext(), orderCategoryModels, new ServiceOrderAdapter.RecyclerItemClickListener() {
@Override
public void onClickListener(OrderCategoryModel orderCategoryModel, int position) {
currentServicePrice = orderCategoryModel.getOrderPrice();
changeSelectedService(position);
onPriceChanged(currentExtraPrice + currentServicePrice);
}
});
public void onPriceChanged(int total) {
txt_total_uang.setText(String.valueOf(total));
}