0

我在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(); 
} 

Menu Click上的加號按鈕的方法,我需要增加數量。完全不知道如何實現這一目標

+1

你所說的「徽章」和「徽章數」是什麼意思?用戶在屏幕上實際看到的是什麼?用戶做什麼以及用戶做什麼會在應用中發生什麼?如果您回答這些問題並告訴我們您的應用應該做什麼,我們可以提供更好的幫助。一種方法是在你的問題中提供模擬屏幕。 –

+0

所以徽章在你的活動中。你想要做的是從片段中的用戶交互中更改徽章中的某些內容。是對的嗎...? –

+0

@ Code-Apprentice **徽章** - 它顯示用戶需要顯示和計數的內容顯示項目數 –

回答

0

首先創建一個新的接口類

public interface OnStepCompletedListener { 
    void onStepCompleted(int position); 
} 

現在實現你的Activity類此接口。

public class MainActivity extends AppCompatActivity implements OnStepCompletedListener{ 

和實施(在您的活動)的方法

@Override 
public void onStepCompleted(int position){ 
// 
// invoke method to change the badge number here 
// 
} 

現在你的片段中。要改變徽章添加該代碼。(以下簡稱「+」按鈕,點擊裏面)

try{ 
    ((OnStepCompletedListener) getActivity()).onStepCompleted(0); 
//This will invoke the implemented method in your activity class. You 
//can pass any type of value through to your activity. Just add the 
//parameter in your interface declaration. 
}catch (ClassCastException e){ 
    e.printStackTrace(); 
} 
+0

總是得到例外 –

+0

哪裏有異常..? –

+0

嘗試catch塊'ClassCastException' –

-1

定義計數變量爲靜態,然後從菜單佈局類訪問它,從而把靜態計數的值到TextView中。 我做了從片段添加菜單,這是代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    // TODO Add your menu entries here 
    super.onCreateOptionsMenu(menu, inflater); 
} 
+0

已經使用靜態,但沒有得到成功 –

+0

靜態變量是代碼氣味 –

+0

此代碼用於創建'OptionMenu'不用於徽章增量 –