我有一个活动,其中包含带有3个片段的ViewPager的TabLayout设置
这3个片段包含每种方法各自独有的一些方法,它们需要在单击按钮时调用,这些按钮是菜单中的Menu项,要针对这3个片段的父级活动进行充气。
现在,我无法初始化在名为OnMenuSaveButonClickListener的活动中定义的接口,因为出现错误:
Caused by: java.lang.ClassCastException: com.blah.blah.EditProductInfoActivity cannot be cast to com.blah.blah.EditProductInfoActivity$OnMenuSaveButonClickListener
at com.blah.blah.EditProductInfoActivity.onCreate(EditProductInfoActivity.java:53)
ViewPager的父活动代码(TabLayout中带有片段):
public class EditProductInfoActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager mViewPager;
OnMenuSaveButonClickListener mCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_product_info);
tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager = (ViewPager) findViewById(R.id.v_pager_prod);
mCallback = (OnMenuSaveButonClickListener) this; //line:53 with error
initViewPager();
private void initViewPager() {
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(new CatalogueItemInfoFragment());
fragments.add(new CatalogueItemInventoryFragment());
fragments.add(new CatalogueItemNotesFragment());
ProductTabsViewPagerAdapter productTabs = new ProductTabsViewPagerAdapter(
getSupportFragmentManager(),this, fragments);
mViewPager.setAdapter(productTabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setText("Product Info");
tabLayout.getTabAt(1).setText("Inventory");
tabLayout.getTabAt(2).setText("Notes");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.item_info_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_check:
mCallback.onMenuButonClick();
return true;
default: return super.onOptionsItemSelected(item);
}
}
public interface OnMenuSaveButonClickListener{
void onMenuButonClick();
}
}
ViewPager Adpater类的代码:
public class ProductTabsViewPagerAdapter extends FragmentPagerAdapter {
private Context context;
private List<Fragment> fragments;
public ProductTabsViewPagerAdapter(FragmentManager fm, Context context, List<Fragment> fragments) {
super(fm);
this.context = context;
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
片段1的代码:
public class CatalogueItemInfoFragment extends Fragment implements EditProductInfoActivity.OnMenuSaveButonClickListener {
...
...
public void setItemFromItemForm(){
productViewModel.setSize(productSizeText.getText().toString());
productViewModel.setSizeSelection(productSizeSelectionText.getText().toString());
productViewModel.setColor(productColorText.getText().toString());
}
@Override
public void onMenuButonClick() {
setItemFromItemForm();
}
}
片段2的代码:
public class CatalogueItemInventoryFragment extends Fragment implements EditProductInfoActivity.OnMenuSaveButonClickListener {
...
...
private void setItemInventoryInfo() {
productViewModel.setAvailableQuantity(Integer.parseInt(availableQtyText.getText().toString()));
productViewModel.setIsOutOfStock(outOfStockSwitch.isChecked());
productViewModel.setIsShowOutOfStock(showOutOfStockSwitch.isChecked());
productViewModel.setIsForceAllowOrder(forceAllowOrderSwitch.isChecked());
}
@Override
public void onMenuButonClick() {
setItemInventoryInfo();
}
}
片段3的代码:
public class CatalogueItemNotesFragment extends Fragment implements EditProductInfoActivity.OnMenuSaveButonClickListener {
...
...
private void setItemMoreDetails() {
mStaticProduct.setNotes(productNotesText.getText().toString());
productViewModel.setNotes(productNotesText.getText().toString());
}
@Override
public void onMenuButonClick() {
setItemMoreDetails();
}
}
最佳答案
从TabLayout获取选定的选项卡位置,以便可以将当前片段作为目标
int selectedTab = tabLayout.getSelectedTabPosition();
从该片段调用OnMenuSaveButonClickListener实现方法
(((OnMenuSaveButonClickListener)fragment.get(selectedTab))。onMenuButonClick();
此代码会在onOptionsItemSelected中进入您的活动
您可以将该逻辑放在函数中,以避免对其他menuitem选项重复相同的行。
确保从代码中删除mCallback及其分配。
我建议您还从活动中删除OnMenuSaveButonClickListener接口定义,并将其包含在它自己的.java(OnMenuSaveButonClickListener.java)>松散耦合中。