Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RecyclerView to realize simple Shopping cart function in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use RecyclerView to implement a simple shopping cart in Android. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows

We know that setTag is used to solve the reuse problem of Item in ListView, but reuse has been encapsulated in RecyclerView. How should we solve the reuse problem of RecyclerView in the project?

Let's first take a look at the effect picture:

The picture may be larger and there are no dynamic pictures, but the effect is like this! In a few days, there will be everything you should have. all right, let's get down to business.

Reuse error analysis:

If judgment is added when RecyclerView sets up data source, which leads to item reuse without if judgment. Continue to reuse the originally set UI property. To put it simply, when you slide your sub-item container on the previous page, RecyclerView defaults to using the data of the previous page if it does not set the selected data. OK, now that we know the cause of the problem, let's bare the code.

Specific operations:

/ / define a global variable / / public SparseBooleanArray booleanArray = new SparseBooleanArray (); / / set the selected listener for CheckBox and set the data holder1.mCbx.setOnCheckedChangeListener for the collection (new CompoundButton.OnCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {booleanArray.put (I, isChecked);}}) / / set data holder1.mCbx.setChecked (booleanArray.get (I))

The shopping cart costs:

It's easy to charge for a shopping cart. You don't have to be able to count anything.

Go directly to the code:

/ / set a click event to CheckBox and call back to the Activityholder1.mCbx.setOnClickListener of the interface (new View.OnClickListener () {@ Override public void onClick (View v) {if (mOnCBXOnClickListener! = null) {mOnCBXOnClickListener.onClick (v, I);})

Then we should pay attention to the accuracy problem of double type data, such as 12.0000000002, a series of long numbers, etc., so in order to solve this problem, we introduce the DecimalFormat class.

The / / private double aggregateAmount; / / DecimalFormat class mainly relies on # and 0 placeholders to specify the length of the number. 0 means that if the number of digits is insufficient, it will be filled with 0, and # will omit the last zero by default. / / DecimalFormat df = new DecimalFormat ("0.000"); final DecimalFormat decimalFormat = new DecimalFormat ("#"); mRlvAdapter.setCBXOnClickListener (new RlvAdapter.onCBXOnClickListener () {@ Override public void onClick (View v, int I) {CheckBox cbx = v.findViewById (R.id.cbx)) If (cbx.isChecked ()) {aggregateAmount+=mRlvAdapter.mlist.get (I) .getBuySpeciTotalPrice (); String str = decimalFormat.format (aggregateAmount); money.setText ("Total amount:" + str);} else {aggregateAmount-=mRlvAdapter.mlist.get (I) .getBuySpeciTotalPrice () String sss = decimalFormat.format (aggregateAmount); money.setText ("Total amount:" + sss);}); Thank you for reading! This is the end of the article on "how to use RecyclerView to achieve simple shopping cart function in Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report