重新选择选项卡时如何刷新片段,MaterialTabHost

how to refresh the fragment when tab is reselected , MaterialTabHost

我正在使用 MaterialtTabHost 和 ViewPager,我希望每当我切换到新标签时,它应该自动更新为新数据。

这是我的activity,我在其中为每个 ViewPager 添加了选项卡和 viewPager,我有不同的片段要显示。

public class FrontPage_Activity extends AppCompatActivity implements MaterialTabListener {
    ViewPager mPager;
    StorageUtil storageUtil;
    User user;
    MaterialTabHost tabHost;
    Toolbar toolbar;
    MyPageAdapter myPageAdapter;

    @Override
    protected void onResume() {
        super.onResume();
        for (int i = 0; i < myPageAdapter.getCount(); i++) {
            tabHost.addTab(tabHost.newTab().setText(myPageAdapter.getPageTitle(i)).setTabListener(this));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_front_page_);
        storageUtil = new StorageUtil(this);
        storageUtil.setUserStatus(1);
        user = storageUtil.getUser();
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (user != null)
            actionBar.setTitle("Welcome " + user.getName());
        mPager = (ViewPager) findViewById(R.id.pager);
        tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                tabHost.setSelectedNavigationItem(position);
            }
        });
        myPageAdapter = new MyPageAdapter(getSupportFragmentManager());
        mPager.setAdapter(myPageAdapter);
        /*for (int i = 0; i < myPageAdapter.getCount(); i++) {
            tabHost.addTab(tabHost.newTab().setText(myPageAdapter.getPageTitle(i)).setTabListener(this));
        }*/
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                } else if (position == 1) {

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onTabSelected(MaterialTab tab) {
        mPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(MaterialTab tab) {

    }

    @Override
    public void onTabUnselected(MaterialTab tab) {

    }

    class MyPageAdapter extends FragmentPagerAdapter {
        String tabs[] = getResources().getStringArray(R.array.tabs);

        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }

        @Override
        public Fragment getItem(int position) {
            return setFragment(position);
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

    private Fragment setFragment(int number) {
        if (number == 0) {
            Chats chats = new Chats();
            return chats;
        } else if (number == 1) {
            Request frag = new Request();
            return frag;
        } else {
            Invite frag = new Invite();
            return frag;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.chatting_activity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.logout:
                storageUtil.clear();
                startActivity(new Intent(this, MainActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        moveTaskToBack(true);
    }
}

首先,这可能有点困难,一种方法也有不同的结果,但最终还是有一些方法可以进行测试。

我通常使用的一种方法是覆盖 getItemPosition

在您的客户适配器 (MyPageAdapter 中,像这样重写 getItemPosition:

class MyPageAdapter extends FragmentPagerAdapter {
 ...
  @Override
      public int getItemPosition(Object object) {
         if (object instanceof Chats ) {
            ((Chats )object).updateMyLogicOrView();
         }
         if (object instanceof Request) {
            ((Request)object).updateMyLogicOrView();
         }

     if (object instanceof Invite) {
            ((Invite)object).updateMyLogicOrView();
         }
         return super.getItemPosition(object);
      }
..
}

然后在属于自己选项卡的每个片段上,您创建 updateMyLogicOrView()。在该方法中,您记下了一些您希望在视图上生成新数据/更新数据的操作。当您移动到选项卡时,此 updateMyLogicOrView() 将始终通过覆盖方法调用。

// put this method on each fragment class
public void updateMyLogicOrView(){
 // do some logics here..
// like component.setText()
// or, adding some new elements to adapter..

}