0

在我的代碼中,我有一個包含5個片段的viewpager。當我嘗試用新片段替換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

是新的重疊舊還是沒有發生在滾動? – Sandeep

+0

新的永遠不會被召喚。老人正在叫。 –

+0

你想更改片段的事件? – Sandeep

回答

0

要片段之間切換,你必須調用:

viewPager.setCurrentItem(position) 

其中位置是你mFragmentList位置的片段(我看不出聲明哪裏)

1

每次當你修改適配器的數據,你應該叫

adapter.notifyDataSetChanged();

使改變發生,但你只能在你的其中一個if/else分支中調用它。 嘗試在if/else塊的末尾調用此方法。