我在ViewPager中使用FragmentActivity,在onCreate(...)方法中我将FragmentPagerAdapter适配器设置为ViewPager它在这里正常工作但是当我尝试在onActivityResult(...)方法中将新的FragmentPagerAdapter重置为ViewPager时只有适配器的构造函数正在调用和公共Fragment getItem(int position);没有被调用,因为我得到片段null。因此,当我在onActivityResult(...)中重置适配器后尝试获取当前可见片段时,我将变为空。
这就是我在onCreate中所做的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.blocked_list);
lable = getIntent().getStringExtra("ItemName");
blocked_list_lable = (TextView) findViewById(R.id.blocked_list_lable);
add_Contact = (Button) findViewById(R.id.add);
mAdapter = new CallManagerFragmentAdapter(getSupportFragmentManager(), lable);
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
addNewContact = new AddNewContactPopup(this);
addNewContact.setPopDimentions((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 320, getResources()
.getDisplayMetrics()), (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 280, BlackList.this.getResources()
.getDisplayMetrics()));
blocked_list_lable.setText(lable);
setListener(add_Contact);
}
这就是我在onActivityResult中所做的事情:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CALLS_SMS_LOGS || requestCode == CONTACTS_LIST) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("dbUpdateSucess");
if (result.equals("Success")) {
Logger.log("Info", TAG,
"Update from call logs is Sucess...");
//Cursor cursor = getCursor(lable);
mAdapter = new CallManagerFragmentAdapter(getSupportFragmentManager(), lable);
mPager.setAdapter(mAdapter);
//adapter = new BlackListAdapter(this, cursor);
//blocked_List.setAdapter(adapter);
return;
} else {
Logger.log("Error", TAG, "Update from call logs failed...");
}
}
}
}
这是我的FragmentAdapter代码;
class CallManagerFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
protected static final String[] CONTENT = new String[] { "Call Logs", "Blacklist No"};
protected static final String[] BLACK_LIST_SMS_CONTENT = new String[] { "SMS Logs", "Blacklist No"};
protected static final String[] WHITELIST_CALLS_CONTENT = new String[] { "Call Logs", "Whitelist No"};
protected static final String[] WHITELIST_SMS_CONTENT = new String[] { "SMS Logs", "Whitelist No"};
protected static final String[] PRIVATE = new String[] { "SMS Logs", "Call Logs", "Private No"};
protected static final int[] ICONS = new int[] {
R.drawable.perm_group_calendar,
R.drawable.perm_group_camera/*,
R.drawable.perm_group_device_alarms,
R.drawable.perm_group_location*/
};
private int mCount = CONTENT.length;
private String lable;
public HashMap<Integer, CallManagerFragment> fragmentMap;
public CallManagerFragmentAdapter(FragmentManager fm, String lable) {
super(fm);
this.lable = lable;
fragmentMap = new HashMap<Integer, CallManagerFragment>();
}
@Override
public Fragment getItem(int position) {
String content;
CallManagerFragment fragment = CallManagerFragment.newInstance(content, lable);
fragmentMap.put(position, fragment);
return fragment;
}
public CallManagerFragment getFragment(int key) {
return fragmentMap.get(key);
}
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
fragmentMap.remove(position);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
if(lable.equals("Whitelist Calls")) {
return CallManagerFragmentAdapter.WHITELIST_CALLS_CONTENT[position % WHITELIST_CALLS_CONTENT.length];
} else if (lable.equals("Whitelist SMS")) {
return WHITELIST_SMS_CONTENT[position % WHITELIST_SMS_CONTENT.length];
} else if(lable.equals("Private")) {
return PRIVATE[position % PRIVATE.length];
} else if(lable.equals("Blacklist SMS")) {
return BLACK_LIST_SMS_CONTENT[position % BLACK_LIST_SMS_CONTENT.length];
}
return CallManagerFragmentAdapter.CONTENT[position % CONTENT.length];
}
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}