我在MainActivity
中創建了徽章。它從活動中完美地工作。但在MainActivity
,我已採取ViewPager
,我需要從片段中增加徽章數。任何想法如何實現這一目標?Android:如何從MainActivity的視圖分頁器中增加徽章?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.cart_menu, menu);
menuItem = menu.findItem(R.id.action_cart);
menuItem.setIcon(buildCounterDrawable(count, R.drawable.ic_cart));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_cart) {
Intent intent = new Intent(this, CartActivity.class);
intent.putExtra("id", orderid);
startActivityForResult(intent, PICK_REQUEST);
return true;
}
return super.onOptionsItemSelected(item);
}
這是徽章計數增量
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText(String.valueOf(count));
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
public void doIncrease(int val) {
count = val;
invalidateOptionsMenu();
}
Click上的加號按鈕的方法,我需要增加數量。完全不知道如何實現這一目標
你所說的「徽章」和「徽章數」是什麼意思?用戶在屏幕上實際看到的是什麼?用戶做什麼以及用戶做什麼會在應用中發生什麼?如果您回答這些問題並告訴我們您的應用應該做什麼,我們可以提供更好的幫助。一種方法是在你的問題中提供模擬屏幕。 –
所以徽章在你的活動中。你想要做的是從片段中的用戶交互中更改徽章中的某些內容。是對的嗎...? –
@ Code-Apprentice **徽章** - 它顯示用戶需要顯示和計數的內容顯示項目數 –