我正在尝试在对话框中显示选项卡式寻呼机视图,但我无法在我的适配器类中初始化getSupportFragmentManager
extends BaseAdapter
。如果我使用MainActivity extends AppCompatActivity
,则getSupportFragmentManager
未显示错误。这是我的代码。请指导我。
public class ContactListAdapter extends BaseAdapter {
------------
------------
slidingListItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TabDialogFragment.createBuilder(ContactListAdapter.this, getSupportFragmentManager())
.setTitle("Title")
.setSubTitle("Subtitle")
.setTabButtonText(new CharSequence[]{"Tab1", "Tab2"})
.setPositiveButtonText("Ok")
.setNegativeButtonText("Cancel")
.setNeutralButtonText("Neutral")
.setRequestCode(REQUEST_TABBED_DIALOG)
.show();
}
});
如何在我的适配器类中初始化getSupportFragmentManager()
?
答案 0 :(得分:0)
您可以在适配器的类中创建一个字段,并通过将其传递到适配器的构造函数中来保存FragmentManager
。类似的东西:
public class ContactListAdapter extends BaseAdapter {
private FragmentManager mFragmentManager;
public ContactListAdapter(FragmentManager fragmentManager)
mFragmentManager = fragmentManager;
}
------------
------------
slidingListItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TabDialogFragment.createBuilder(ContactListAdapter.this, mFragmentManager)
.setTitle("Title")
.setSubTitle("Subtitle")
.setTabButtonText(new CharSequence[]{"Tab1", "Tab2"})
.setPositiveButtonText("Ok")
.setNegativeButtonText("Cancel")
.setNeutralButtonText("Neutral")
.setRequestCode(REQUEST_TABBED_DIALOG)
.show();
}
});
在您的活动中创建此适配器时,请按以下方式创建:
ContactListAdapter mAdapter = new ContactListAdapter(getSupportFragmentManager());
答案 1 :(得分:0)
最简单的方法是将FragmentManager实例传递给适配器,然后使用该实例。
然而,最好的方法是给予对话的负担 创造活动本身。
答案 2 :(得分:0)
private View.OnClickListener mOnClickListener;
public ContactListAdapter(View.OnClickListener clickListener)
mOnClickListener = clickListener;
}
------------
------------
slidingListItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnClickListener.onClick(view);
});
在你的activity / fragment中实现mOnClickListener,并在onClick中添加与对话框相关的代码。
public void onClick(view view){
TabDialogFragment.createBuilder(this, getSupportFragmentManager())
.setTitle("Title")
.setSubTitle("Subtitle")
.setTabButtonText(new CharSequence[]{"Tab1", "Tab2"})
.setPositiveButtonText("Ok")
.setNegativeButtonText("Cancel")
.setNeutralButtonText("Neutral")
.setRequestCode(REQUEST_TABBED_DIALOG)
.show();
}