您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在Android應用中添加一個添加物品動畫,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
開發環境:AndroidStudio2.1.2+gradle-2.10
涉及知識:1.沉浸式狀態欄,2.單位精度計算(價格),3.List之Iterator。
部分代碼:
public class MainActivity extends AppCompatActivity implements FoodAdapter.FoodActionCallback { private ListView listView; private TextView goods_all_pricetv; private TextView good_numtv; private List<FoodModel> list; private List<FoodModel> selectList = new ArrayList<>(); private FoodAdapter adapter = null; @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //沉浸式狀態欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } listView = (ListView) findViewById(R.id.listView); goods_all_pricetv = (TextView) findViewById(R.id.goods_all_pricetv); good_numtv = (TextView) findViewById(R.id.good_numtv); findViewById(R.id.goods_submit_tv).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != selectList && selectList.size() > 0) { Toast.makeText(MainActivity.this, "你一共加入購物車" + good_numtv.getText() + "份商品,總價格為:" + goods_all_pricetv.getText(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "你的購物車為空", Toast.LENGTH_SHORT).show(); } } }); initData(); } private void initData() { list = AppConfig.factoryFoods(); adapter = new FoodAdapter(this, list, this); listView.setAdapter(adapter); } //添加商品動畫 @Override public void addAction(View view, int item) { ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(this); int position[] = new int[2]; view.getLocationInWindow(position); shoppingCartAnimationView.setStartPosition(new Point(position[0], position[1])); ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView(); rootView.addView(shoppingCartAnimationView); int endPosition[] = new int[2]; good_numtv.getLocationInWindow(endPosition); shoppingCartAnimationView.setEndPosition(new Point(endPosition[0], endPosition[1])); shoppingCartAnimationView.startBeizerAnimation(); FoodModel model = list.get(item); model.setNum(model.getNum() + 1); adapter.notifyDataSetChanged(); calculatePrice(); } //減少商品 @Override public void reduceGood(int position) { FoodModel model = list.get(position); model.setNum(model.getNum() - 1); adapter.notifyDataSetChanged(); calculatePrice(); } //購物車份數+總價格計算 private void calculatePrice() { selectList.clear(); double price = 0; int num = 0; Iterator<FoodModel> iterator = list.iterator(); while (iterator.hasNext()) { FoodModel model = iterator.next(); if (model.getNum() != 0) { selectList.add(model); price += model.getPrice() * model.getNum(); num += model.getNum(); } } goods_all_pricetv.setText("¥" + adapter.priceResult(price) + "元"); good_numtv.setText(num + ""); } }
關于怎么在Android應用中添加一個添加物品動畫就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。