在我的代码中,我有一个包含5个片段的viewpager。当我尝试用新片段替换viewpager中的第一个片段时。年纪较大的人仍在装货。
我无法用新的片段替换旧片段。这是我的代码。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
viewPager.getAdapter().notifyDataSetChanged();
if (id == R.id.menu_leave_raise_req) {
// adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new ActionItemLeaveFragment("0"), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
adapter.addFrag(new FourFragment(), "FOUR");
adapter.addFrag(new FifthFragment(), "FIVE");
viewPager.setAdapter(adapter);
customlayout();
return true;
} else if (id == R.id.menu_leave_apply_aprove) {
//adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new ActionItemLeaveFragment("2"), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
adapter.addFrag(new FourFragment(), "FOUR");
adapter.addFrag(new FifthFragment(), "FIVE");
viewPager.setAdapter(adapter);
customlayout();
return true;
} else if (id == R.id.menu_leave_lev_bal) {
/* getSupportFragmentManager().beginTransaction()
.replace(R.id.viewpager, new TodayLeaveStatusFragment("0"))
.commit();
customlayout();*/
//adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new TodayLeaveStatusFragment("0"), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
adapter.addFrag(new FourFragment(), "FOUR");
adapter.addFrag(new FifthFragment(), "FIVE");
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
customlayout();
return true;
} else if (id == R.id.attendance) {
// Intent intent=new Intent(Levdetail.this, Attendance.class);
// startActivity(intent);
return true;
} else if (id == R.id.leave_apply) {
//Intent intent=new Intent(Levdetail.this, LeaveApply.class);
// startActivity(intent);
return true;
}
//viewPager.getAdapter().notifyDataSetChanged();
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.leave_menu, menu);
return true;
}
private void customlayout() {
int[] tabIcons = {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
String[] notifications = {"10", "21", "52", "30", "37"};
String[] titleName = {"0", "1", "2", "3", "4"};
for (int i = 0; i < tabIcons.length; i++) {
View v = LayoutInflater.from(this).inflate(R.layout.sample, null);
ImageView img = (ImageView) v.findViewById(R.id.img_header);
img.setImageResource(tabIcons[i]);
TextView tv = (TextView) v.findViewById(R.id.notifications);
tv.setText(notifications[i]);
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(titleName[i]);
title.setSelected(true);
v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
System.out.println("count::" + tabIcons[i]);
tabLayout.getTabAt(i).setCustomView(v);
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new ActionItemLeaveFragment("0"), "1");
adapter.addFrag(new TwoFragment(), "2");
adapter.addFrag(new ThreeFragment(), "3");
adapter.addFrag(new FourFragment(), "4");
adapter.addFrag(new FifthFragment(), "5");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment)object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment)object);
trans.commit();
}
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public void onBackPressed() {
finish();
}
任何帮助都应该对我有很大帮助。
答案 0 :(得分:1)
每次修改适配器中的数据时,都应该调用
adapter.notifyDataSetChanged();
进行更改,但你只在其中一个if / else分支中调用它。 尝试在if / else块的末尾调用此方法。
答案 1 :(得分:0)
要在您必须致电的片段之间切换:
viewPager.setCurrentItem(position)
其中 position 是mFragmentList中的位置片段(我无法看到声明的位置)