gpt4 book ai didi

java - 如何初始化在 Activity 中定义的界面,该界面是通过 TabLayout 使用 ViewPager 设置的 3 个 fragment 实现的?

转载 作者:行者123 更新时间:2023-12-02 08:57:27 26 4
gpt4 key购买 nike

我有一个 Activity ,其中包含带有 3 个 fragment 的 ViewPager 的 TabLayout 设置这 3 个 fragment 包含一些各自独特的方法,需要在单击按钮时调用它们,该按钮是菜单中的菜单项,将为这 3 个 fragment 的父 Activity 进行扩展。

现在我无法初始化名为 OnMenuSaveButonClickListener 的 Activity 中定义的界面,因为我收到错误:

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 的父 Activity 代码(在 TabLayout 中包含 fragment ):

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


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

}

fragment 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();

}

}

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

Tab 1

Tab 2 Tab 3

最佳答案

  1. 从 TabLayout 获取选定的选项卡位置,以便您可以定位当前可见的 fragment

    int selectedTab = tabLayout.getSelectedTabPosition();

  2. 从该 fragment 调用OnMenuSaveButonClickListener实现方法

    ((OnMenuSaveButonClickListener)fragments.get(selectedTab)).onMenuButonClick();

此代码位于您的 Activity 中的onOptionsItemSelected您可以将该逻辑放入函数中,以避免对其他菜单项选项重复相同的行。

确保从代码中删除 mCallback 及其分配。

我建议您还从 Activity 中删除 OnMenuSaveButonClickListener 接口(interface)定义,并将其放在它自己的 .java (OnMenuSaveButonClickListener.java) > 松散耦合中。

关于java - 如何初始化在 Activity 中定义的界面,该界面是通过 TabLayout 使用 ViewPager 设置的 3 个 fragment 实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60411625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com